//using ClosedXML.Excel; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LeatherApp.Utils { public class ExcelUtil { public static DataTable ConvertToDataTable(IList data,List lstColName) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); foreach (PropertyDescriptor prop in properties) { if(lstColName.Contains(prop.Name)) table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); } foreach (T item in data) { DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) { if (lstColName.Contains(prop.Name)) row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; } table.Rows.Add(row); } return table; } public static bool DataTable2CSV( string saveFilePath,System.Data.DataTable dt) { //MessageBox.Show(AbosultedFilePath); System.IO.FileStream fs = new FileStream(saveFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write); StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding()); //Tabel header for (int i = 0; i < dt.Columns.Count; i++) { sw.Write(dt.Columns[i].ColumnName); sw.Write("\t"); } sw.WriteLine(""); //Table body for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { sw.Write(DelQuota(dt.Rows[i][j].ToString())); sw.Write("\t"); } sw.WriteLine(""); } sw.Flush(); sw.Close(); return true; } public static string DelQuota(string str) { string result = str; string[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", "/", ":", "/,", "<", ">", "?" };//".", for (int i = 0; i < strQuota.Length; i++) { if (result.IndexOf(strQuota[i]) > -1) result = result.Replace(strQuota[i], ""); } return result; } //public static bool saveFile(string saveFilePath, DataTable dataTable) //{ // try // { // //// 创建一个 DataTable 对象来存储数据 // //DataTable dataTable = new DataTable("MyData"); // //// 添加列到 DataTable // //dataTable.Columns.Add("Name", typeof(string)); // //dataTable.Columns.Add("Age", typeof(int)); // //// 向 DataTable 中添加数据行 // //dataTable.Rows.Add("John Doe", 30); // //dataTable.Rows.Add("Jane Smith", 25); // // 使用 ClosedXML 组件导出 Excel 文件 // using (XLWorkbook workbook = new XLWorkbook()) // { // IXLWorksheet worksheet = workbook.AddWorksheet("MySheet"); // int row = 1; //行从1开始 // for (int i = 0; i < dataTable.Rows.Count; i++) // { // for (int j = 0; j < dataTable.Columns.Count; j++) // { // if (row == 1)//头标题 // worksheet.Cell(1, j + 1).Value = dataTable.Columns[j].ColumnName; // //内容 // worksheet.Cell(row + 1, j + 1).Value = dataTable.Rows[i][j].ToString(); // // 设置单元格样式 // //worksheet.Cell(row, 2).Style.Font.Bold = true; // //worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right; // } // row++; // } // // 将 Excel 文件保存到磁盘 // //string fileName = @"C:\temp\MyExcelFile.xlsx"; // workbook.SaveAs(saveFilePath); // // 释放资源 // workbook.Dispose(); // } // return true; // } // catch (Exception ex) // { // throw ex; // } //} //public static bool test(string saveFilePath, DataTable dataTable1) //{ // try // { // // 创建一个 DataTable 对象来存储数据 // DataTable dataTable = new DataTable("MyData"); // // 添加列到 DataTable // dataTable.Columns.Add("Name", typeof(string)); // dataTable.Columns.Add("Age", typeof(int)); // // 向 DataTable 中添加数据行 // dataTable.Rows.Add("John Doe", 30); // dataTable.Rows.Add("Jane Smith", 25); // // 使用 ClosedXML 组件导出 Excel 文件 // using (XLWorkbook workbook = new XLWorkbook()) // { // IXLWorksheet worksheet = workbook.AddWorksheet("MySheet"); // int row = 1; // foreach (DataRow dataRow in dataTable.Rows) // { // worksheet.Cell(row, 1).Value = dataRow["Name"].ToString(); // worksheet.Cell(row, 2).Value = Convert.ToInt32(dataRow["Age"]); // // 设置单元格样式 // worksheet.Cell(row, 2).Style.Font.Bold = true; // worksheet.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right; // row++; // } // // 将 Excel 文件保存到磁盘 // //string fileName = @"C:\temp\MyExcelFile.xlsx"; // workbook.SaveAs(saveFilePath); // // 释放资源 // workbook.Dispose(); // } // return true; // } // catch (Exception ex) // { // return false; // } //} } }