革博士程序V1仓库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

161 行
6.7 KiB

  1. //using ClosedXML.Excel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace LeatherApp.Utils
  11. {
  12. public class ExcelUtil
  13. {
  14. public static DataTable ConvertToDataTable<T>(IList<T> data,List<string> lstColName)
  15. {
  16. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
  17. DataTable table = new DataTable();
  18. foreach (PropertyDescriptor prop in properties)
  19. {
  20. if(lstColName.Contains(prop.Name))
  21. table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
  22. }
  23. foreach (T item in data)
  24. {
  25. DataRow row = table.NewRow();
  26. foreach (PropertyDescriptor prop in properties)
  27. {
  28. if (lstColName.Contains(prop.Name))
  29. row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
  30. }
  31. table.Rows.Add(row);
  32. }
  33. return table;
  34. }
  35. public static bool DataTable2CSV( string saveFilePath,System.Data.DataTable dt)
  36. {
  37. //MessageBox.Show(AbosultedFilePath);
  38. System.IO.FileStream fs = new FileStream(saveFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
  39. StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
  40. //Tabel header
  41. for (int i = 0; i < dt.Columns.Count; i++)
  42. {
  43. sw.Write(dt.Columns[i].ColumnName);
  44. sw.Write("\t");
  45. }
  46. sw.WriteLine("");
  47. //Table body
  48. for (int i = 0; i < dt.Rows.Count; i++)
  49. {
  50. for (int j = 0; j < dt.Columns.Count; j++)
  51. {
  52. sw.Write(DelQuota(dt.Rows[i][j].ToString()));
  53. sw.Write("\t");
  54. }
  55. sw.WriteLine("");
  56. }
  57. sw.Flush();
  58. sw.Close();
  59. return true;
  60. }
  61. public static string DelQuota(string str)
  62. {
  63. string result = str;
  64. string[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", "/", ":", "/,", "<", ">", "?" };//".",
  65. for (int i = 0; i < strQuota.Length; i++)
  66. {
  67. if (result.IndexOf(strQuota[i]) > -1)
  68. result = result.Replace(strQuota[i], "");
  69. }
  70. return result;
  71. }
  72. //public static bool saveFile(string saveFilePath, DataTable dataTable)
  73. //{
  74. // try
  75. // {
  76. // //// 创建一个 DataTable 对象来存储数据
  77. // //DataTable dataTable = new DataTable("MyData");
  78. // //// 添加列到 DataTable
  79. // //dataTable.Columns.Add("Name", typeof(string));
  80. // //dataTable.Columns.Add("Age", typeof(int));
  81. // //// 向 DataTable 中添加数据行
  82. // //dataTable.Rows.Add("John Doe", 30);
  83. // //dataTable.Rows.Add("Jane Smith", 25);
  84. // // 使用 ClosedXML 组件导出 Excel 文件
  85. // using (XLWorkbook workbook = new XLWorkbook())
  86. // {
  87. // IXLWorksheet worksheet = workbook.AddWorksheet("MySheet");
  88. // int row = 1; //行从1开始
  89. // for (int i = 0; i < dataTable.Rows.Count; i++)
  90. // {
  91. // for (int j = 0; j < dataTable.Columns.Count; j++)
  92. // {
  93. // if (row == 1)//头标题
  94. // worksheet.Cell(1, j + 1).Value = dataTable.Columns[j].ColumnName;
  95. // //内容
  96. // worksheet.Cell(row + 1, j + 1).Value = dataTable.Rows[i][j].ToString();
  97. // // 设置单元格样式
  98. // //worksheet.Cell(row, 2).Style.Font.Bold = true;
  99. // //worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  100. // }
  101. // row++;
  102. // }
  103. // // 将 Excel 文件保存到磁盘
  104. // //string fileName = @"C:\temp\MyExcelFile.xlsx";
  105. // workbook.SaveAs(saveFilePath);
  106. // // 释放资源
  107. // workbook.Dispose();
  108. // }
  109. // return true;
  110. // }
  111. // catch (Exception ex)
  112. // {
  113. // throw ex;
  114. // }
  115. //}
  116. //public static bool test(string saveFilePath, DataTable dataTable1)
  117. //{
  118. // try
  119. // {
  120. // // 创建一个 DataTable 对象来存储数据
  121. // DataTable dataTable = new DataTable("MyData");
  122. // // 添加列到 DataTable
  123. // dataTable.Columns.Add("Name", typeof(string));
  124. // dataTable.Columns.Add("Age", typeof(int));
  125. // // 向 DataTable 中添加数据行
  126. // dataTable.Rows.Add("John Doe", 30);
  127. // dataTable.Rows.Add("Jane Smith", 25);
  128. // // 使用 ClosedXML 组件导出 Excel 文件
  129. // using (XLWorkbook workbook = new XLWorkbook())
  130. // {
  131. // IXLWorksheet worksheet = workbook.AddWorksheet("MySheet");
  132. // int row = 1;
  133. // foreach (DataRow dataRow in dataTable.Rows)
  134. // {
  135. // worksheet.Cell(row, 1).Value = dataRow["Name"].ToString();
  136. // worksheet.Cell(row, 2).Value = Convert.ToInt32(dataRow["Age"]);
  137. // // 设置单元格样式
  138. // worksheet.Cell(row, 2).Style.Font.Bold = true;
  139. // worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
  140. // row++;
  141. // }
  142. // // 将 Excel 文件保存到磁盘
  143. // //string fileName = @"C:\temp\MyExcelFile.xlsx";
  144. // workbook.SaveAs(saveFilePath);
  145. // // 释放资源
  146. // workbook.Dispose();
  147. // }
  148. // return true;
  149. // }
  150. // catch (Exception ex)
  151. // {
  152. // return false;
  153. // }
  154. //}
  155. }
  156. }