You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
10 KiB

2 years ago
  1. using System;
  2. using Microsoft.Office.Interop.Excel;
  3. namespace EasyBL
  4. {
  5. /// <SUMMARY>
  6. /// Microsoft.Office.Interop.ExcelEdit 的摘要说明
  7. /// </SUMMARY>
  8. public class ExcelEdit
  9. {
  10. public string mFilename;
  11. public Application app;
  12. public Workbooks wbs;
  13. public Workbook wb;
  14. public Worksheets wss;
  15. public Worksheet ws;
  16. public ExcelEdit()
  17. {
  18. // TODO: 在此处添加构造函数逻辑
  19. }
  20. public void Create()//创建一个Microsoft.Office.Interop.Excel对象
  21. {
  22. app = new Application();
  23. wbs = app.Workbooks;
  24. wb = wbs.Add(true);
  25. }
  26. public void Open(string FileName)//打开一个Microsoft.Office.Interop.Excel文件
  27. {
  28. app = new Application();
  29. wbs = app.Workbooks;
  30. wb = wbs.Open(FileName);
  31. //wb = wbs.Open(FileName, 0, true, 5,"", "", true, XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);
  32. //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
  33. mFilename = FileName;
  34. }
  35. public Worksheet GetSheet(string SheetName)
  36. //获取一个工作表
  37. {
  38. var s = (Worksheet)wb.Worksheets[SheetName];
  39. return s;
  40. }
  41. public Worksheet AddSheet(string SheetName)
  42. //添加一个工作表
  43. {
  44. var s = (Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  45. s.Name = SheetName;
  46. return s;
  47. }
  48. public void DelSheet(string SheetName)//删除一个工作表
  49. {
  50. ((Worksheet)wb.Worksheets[SheetName]).Delete();
  51. }
  52. public Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一
  53. {
  54. var s = (Worksheet)wb.Worksheets[OldSheetName];
  55. s.Name = NewSheetName;
  56. return s;
  57. }
  58. public Worksheet ReNameSheet(Worksheet Sheet, string NewSheetName)//重命名一个工作表二
  59. {
  60. Sheet.Name = NewSheetName;
  61. return Sheet;
  62. }
  63. public void SetCellValue(Worksheet ws, int x, int y, object value)
  64. //ws:要设值的工作表 X行Y列 value 值
  65. {
  66. ws.Cells[x, y] = value;
  67. }
  68. public void SetCellValue(string ws, int x, int y, object value)
  69. //ws:要设值的工作表的名称 X行Y列 value 值
  70. {
  71. GetSheet(ws).Cells[x, y] = value;
  72. }
  73. public void SetCellProperty(Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Constants color, Constants HorizontalAlignment)
  74. //设置一个单元格的属性 字体, 大小,颜色 ,对齐方式
  75. {
  76. name = "宋体";
  77. size = 12;
  78. color = Constants.xlAutomatic;
  79. HorizontalAlignment = Constants.xlRight;
  80. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
  81. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
  82. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
  83. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
  84. }
  85. public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Constants color, Constants HorizontalAlignment)
  86. {
  87. //name = "宋体";
  88. //size = 12;
  89. //color = Constants.xlAutomatic;
  90. //HorizontalAlignment = Constants.xlRight;
  91. var ws = GetSheet(wsn);
  92. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
  93. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
  94. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
  95. ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
  96. }
  97. public void UniteCells(Worksheet ws, int x1, int y1, int x2, int y2)
  98. //合并单元格
  99. {
  100. ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
  101. }
  102. public void UniteCells(string ws, int x1, int y1, int x2, int y2)
  103. //合并单元格
  104. {
  105. GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);
  106. }
  107. public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
  108. //将内存中数据表格插入到Microsoft.Office.Interop.Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
  109. {
  110. for (int i = 0; i <= dt.Rows.Count - 1; i++)
  111. {
  112. for (int j = 0; j <= dt.Columns.Count - 1; j++)
  113. {
  114. GetSheet(ws).Cells[startX + i, j + startY] = dt.Rows[i][j].ToString();
  115. }
  116. }
  117. }
  118. public void InsertTable(System.Data.DataTable dt, Worksheet ws, int startX, int startY)
  119. //将内存中数据表格插入到Microsoft.Office.Interop.Excel指定工作表的指定位置二
  120. {
  121. for (int i = 0; i <= dt.Rows.Count - 1; i++)
  122. {
  123. for (int j = 0; j <= dt.Columns.Count - 1; j++)
  124. {
  125. ws.Cells[startX + i, j + startY] = dt.Rows[i][j];
  126. }
  127. }
  128. }
  129. public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
  130. //将内存中数据表格添加到Microsoft.Office.Interop.Excel指定工作表的指定位置一
  131. {
  132. for (int i = 0; i <= dt.Rows.Count - 1; i++)
  133. {
  134. for (int j = 0; j <= dt.Columns.Count - 1; j++)
  135. {
  136. GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];
  137. }
  138. }
  139. }
  140. public void AddTable(System.Data.DataTable dt, Worksheet ws, int startX, int startY)
  141. //将内存中数据表格添加到Microsoft.Office.Interop.Excel指定工作表的指定位置二
  142. {
  143. for (int i = 0; i <= dt.Rows.Count - 1; i++)
  144. {
  145. for (int j = 0; j <= dt.Columns.Count - 1; j++)
  146. {
  147. ws.Cells[i + startX, j + startY] = dt.Rows[i][j];
  148. }
  149. }
  150. }
  151. public void InsertPictures(string Filename, string ws)
  152. //插入图片操作一
  153. {
  154. //GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  155. //后面的数字表示位置
  156. }
  157. //public void InsertPictures(string Filename, string ws, int Height, int Width)
  158. //插入图片操作二
  159. //{
  160. // GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  161. // GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
  162. // GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
  163. //}
  164. //public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)
  165. //插入图片操作三
  166. //{
  167. // GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  168. // GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
  169. // GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
  170. // GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
  171. // GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
  172. //}
  173. public void InsertActiveChart(XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, XlRowCol ChartDataType)
  174. //插入图表操作
  175. {
  176. ChartDataType = XlRowCol.xlColumns;
  177. wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  178. {
  179. wb.ActiveChart.ChartType = ChartType;
  180. wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
  181. wb.ActiveChart.Location(XlChartLocation.xlLocationAsObject, ws);
  182. }
  183. }
  184. public bool Save()
  185. //保存文档
  186. {
  187. if (mFilename == "")
  188. {
  189. return false;
  190. }
  191. else
  192. {
  193. try
  194. {
  195. wb.Save();
  196. return true;
  197. }
  198. catch (Exception ex)
  199. {
  200. return false;
  201. }
  202. }
  203. }
  204. public bool SaveAs(object FileName)
  205. //文档另存为
  206. {
  207. try
  208. {
  209. wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  210. return true;
  211. }
  212. catch (Exception ex)
  213. {
  214. return false;
  215. }
  216. }
  217. public bool SaveAsPdf(object FileName)
  218. //文档另存为
  219. {
  220. try
  221. {
  222. wb.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, FileName);
  223. return true;
  224. }
  225. catch (Exception ex)
  226. {
  227. return false;
  228. }
  229. }
  230. public void Close()
  231. //关闭一个Microsoft.Office.Interop.Excel对象,销毁对象
  232. {
  233. //wb.Save();
  234. if (wb != null)
  235. {
  236. wb.Close(Type.Missing, Type.Missing, Type.Missing);
  237. }
  238. if (wbs != null)
  239. {
  240. wbs.Close();
  241. }
  242. if (app != null)
  243. {
  244. app.Quit();
  245. }
  246. wb = null;
  247. wbs = null;
  248. app = null;
  249. GC.Collect();
  250. }
  251. }
  252. }