锐腾搅拌上料功能
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.

135 lines
5.3 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Office.Interop.Excel;
  6. using System.IO;
  7. using ICSSoft.Frame.Data.Entity;
  8. namespace ICSSoft.Frame.Data.BLL
  9. {
  10. public class FileUtil
  11. {
  12. /// <summary>
  13. /// 导出Excel文件
  14. /// </summary>
  15. /// <param name="fileName"></param>
  16. /// <param name="data"></param>
  17. /// <remarks></remarks>
  18. public static void exportToExcelFile(string fileName, List<FormReadExcelUIModelColumns> colNameList)
  19. {
  20. Application oExcel = new Application();
  21. Workbooks workbooks = oExcel.Workbooks;
  22. Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
  23. Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
  24. try
  25. {
  26. worksheet.Name = "导入模板";
  27. Range range;
  28. for (int i = 0; i < colNameList.Count; i++)
  29. {
  30. worksheet.Cells[1, i + 1] = colNameList[i].columnsName;
  31. if (colNameList[i].NotNull == true)
  32. {
  33. range = (Range)worksheet.Cells[1, i + 1];
  34. range.Interior.ColorIndex = 8;
  35. }
  36. }
  37. //oExcel.Visible = true;
  38. oExcel.ActiveWindow.SplitRow = 1; //锁定第四行
  39. //oExcel.ActiveWindow.SplitColumn = 1; //锁定第一列
  40. oExcel.ActiveWindow.FreezePanes = true; //冻结锁定区域
  41. oExcel.ActiveSheet.rows(1).Font.Bold = true; //首行加粗
  42. oExcel.ActiveSheet.columns.autofit(); //单元格自动适应宽度
  43. oExcel.ActiveSheet.columns.HorizontalAlignment = 3; //单元格内容水平居中
  44. workbook.SaveAs(fileName);
  45. workbook.Close();
  46. }
  47. catch (Exception ex)
  48. {
  49. throw new Exception("导出Excel文件:" + ex.Message);
  50. }
  51. oExcel = null;
  52. workbook = null;
  53. worksheet = null;
  54. }
  55. public static void exportToExcelFileFromDataTable(string fileName, System.Data.DataTable dt, params string[] readonlyColumNames)
  56. {
  57. Application excelApp = new Application();
  58. Workbooks workbooks = excelApp.Workbooks;
  59. Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
  60. Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
  61. try
  62. {
  63. worksheet.Name = "导入模板";
  64. Range range;
  65. for (int c = 0; c < dt.Columns.Count; c++)
  66. {
  67. worksheet.Cells[1, c + 1] = dt.Columns[c].ColumnName;
  68. range = (Range)worksheet.Cells[1, c + 1];
  69. range.Interior.ColorIndex = 8;
  70. }
  71. for (int i = 0; i < dt.Rows.Count; i++)
  72. {
  73. for (int j = 0; j < dt.Columns.Count; j++)
  74. {
  75. string v = "";
  76. if (dt.Rows[i][j] == DBNull.Value || dt.Rows[i][j] == null)
  77. {
  78. v = "";
  79. }
  80. else
  81. {
  82. if (dt.Columns[j].DataType == typeof(DateTime))
  83. {
  84. v = Convert.ToDateTime(dt.Rows[i][j]).ToString("yyyy-MM-dd HH:mm:ss");
  85. }
  86. else
  87. {
  88. v = dt.Rows[i][j].ToString();
  89. }
  90. }
  91. excelApp.Cells[i + 2, j + 1] = v;
  92. }
  93. }
  94. //for (int i = 0; i < colNameList.Count; i++)
  95. //{
  96. // worksheet.Cells[1, i + 1] = colNameList[i].columnsName;
  97. // if (colNameList[i].NotNull == true)
  98. // {
  99. // range = (Range)worksheet.Cells[1, i + 1];
  100. // range.Interior.ColorIndex = 8;
  101. // }
  102. //}
  103. excelApp.ActiveWindow.SplitRow = 1; //锁定第1行
  104. //excelApp.ActiveWindow.SplitColumn = 1; //锁定第1列
  105. excelApp.ActiveWindow.FreezePanes = true; //冻结锁定区域
  106. excelApp.ActiveSheet.rows(1).Font.Bold = true; //首行加粗
  107. excelApp.ActiveSheet.columns.autofit(); //单元格自动适应宽度
  108. excelApp.ActiveSheet.columns.HorizontalAlignment = 3; //单元格内容水平居中
  109. excelApp.Visible = true; //设置Excel可见
  110. workbook.SaveAs(fileName);
  111. workbook.Close();
  112. }
  113. catch (Exception ex)
  114. {
  115. throw new Exception("导出Excel文件:" + ex.Message);
  116. }
  117. excelApp = null;
  118. workbook = null;
  119. worksheet = null;
  120. }
  121. }
  122. }