using System.Data; using System.IO; using OfficeOpenXml; using OfficeOpenXml.Style; namespace Sai.Toolkit.Core.Helpers; public class EPPlusHelper //公式计算最后需要调用Calculate() { /// /// DataTable导出为Excel /// /// /// public static void DataTableToExcel(DataTable dt, string destFileName) { FileInfo fi = new(destFileName); using ExcelPackage package = new(fi); try { var workSheet = package.Workbook.Worksheets.Add(dt.TableName); workSheet.Cells["A1"].LoadFromDataTable(dt, true); } catch (Exception) { // ignored } finally { package.Save(); } } /// /// 给第一个表中的单元格赋值 /// /// /// /// /// public static void EditCellValue(string fileName, int rowNum, int columnNum, string value) { FileInfo fi = new(fileName); using ExcelPackage package = new(fi); //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; var sheet = package.Workbook.Worksheets[0]; //string filename = System.IO.Path.GetFileNameWithoutExtension(fileName); sheet.Cells[rowNum, columnNum].Value = value; package.Save(); } /// /// 工作表转换为DataTable,首行为表头 /// /// 行列索引从1开始 /// public static DataTable ExcelSheetToDataTable(ExcelWorksheet worksheet) { if (worksheet == null) { return null; } //获取worksheet的行数 var rows = worksheet.Dimension.End.Row; //获取worksheet的列数 var cols = worksheet.Dimension.End.Column; DataTable dt = new(worksheet.Name); DataRow dr = null; for (var i = 1; i <= rows; i++) { if (i > 1) { dr = dt.Rows.Add(); } for (var j = 1; j <= cols; j++) { //默认将第一行设置为datatable的标题 var value = worksheet.Cells[i, j].Value; if (i == 1) { dt.Columns.Add(value == null ? $"标题{i}" : value.ToString()); } //剩下的写入datatable else { dr[j - 1] = value == null ? string.Empty : value.ToString(); } } } return dt; } /// /// 工作表转换为DataTable /// /// /// /// public static DataTable ExcelSheetToDataTable(string fileName, string sheetName) { FileInfo fi = new(fileName); ExcelPackage p = new(fi); var workbook = p.Workbook; var worksheet = workbook.Worksheets[sheetName]; return ExcelSheetToDataTable(worksheet); } /// /// 获取工作表 /// /// /// /// /// public static ExcelWorksheet GetExcelWorksheet(string excelFilePath, string sheetName) { var excelworkbook = new FileInfo(excelFilePath); using var p = new ExcelPackage(excelworkbook); var ws = p.Workbook.Worksheets[sheetName]; return ws ?? throw new Exception("工作表不存在。"); } /// /// 读取Excel所有表 /// /// public void ReadExcel(string filePath) { using ExcelPackage package = new(new FileStream(filePath, FileMode.Open)); for (var i = 1; i <= package.Workbook.Worksheets.Count; ++i) { var sheet = package.Workbook.Worksheets[i]; for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++) { for (int m = sheet.Dimension.Start.Row, n = sheet.Dimension.End.Row; m <= n; m++) { //string str = GetValue(sheet, m, j); //if (str != null) //{ // // do something //} } } } } /// /// 设置单元格的值 /// /// /// 1开始 /// 1开始 /// public static void SetCellValue(ExcelWorksheet sheet, int rowNum, int columnNum, string value) { //sheet.Cells[rowNum, ColumnNum].Value = value; sheet.SetValue(rowNum, columnNum, value); } /// /// 通过单元格名设置值 /// /// /// /// public static void SetCellValue(ExcelWorksheet sheet, string cellName, string value) { sheet.Cells[cellName].Value = value; } /// /// 给工作表中的单元格赋值 /// /// /// /// /// /// public static void SetCellValue(string fileName, string sheetname, int rowNum, int columnNum, string value) { FileInfo fi = new(fileName); using ExcelPackage package = new(fi); //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; var sheet = package.Workbook.Worksheets[sheetname]; //string filename = System.IO.Path.GetFileNameWithoutExtension(fileName); sheet.SetValue(rowNum, columnNum, value); package.Save(); } /// /// 给工作表中的单元格赋值 /// /// /// /// 单元格名称,比如A1 /// public static void SetCellValue(string fileName, string sheetname, string cellName, string value) { FileInfo fi = new(fileName); using ExcelPackage package = new(fi); //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; var sheet = package.Workbook.Worksheets[sheetname]; //string filename = System.IO.Path.GetFileNameWithoutExtension(fileName); sheet.Cells[cellName].Value = value; package.Save(); } /// /// 给工作表中的单元格赋值 /// /// /// 表索引,0开始 /// 单元格名称,如A1 /// public static void SetCellValue(string fileName, int index, string cellName, string value) { FileInfo fi = new(fileName); using ExcelPackage package = new(fi); //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; var sheet = package.Workbook.Worksheets[index]; //string filename = System.IO.Path.GetFileNameWithoutExtension(fileName); sheet.Cells[cellName].Value = value; package.Save(); } /// /// 设置样式 /// /// /// /// /// /// public static void SetStyle(ExcelWorksheet sheet, int fromRow, int fromCol, int toRow, int toCol) { using var range = sheet.Cells[fromRow, fromCol, toRow, toCol]; //range.Style.Font.Name = "微软雅黑"; range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; range.Style.Font.Size = 12; range.Style.Border.Top.Style = ExcelBorderStyle.Thin; range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; range.Style.Border.Left.Style = ExcelBorderStyle.Thin; range.Style.Border.Right.Style = ExcelBorderStyle.Thin; //range.Style.SetBackgroundColor(System.Drawing.SelectedColor.FromArgb(142, 169, 219)); } /// /// 设置表头样式 /// /// /// /// /// /// public static void SetTitle(ExcelWorksheet sheet, int fromRow, int fromCol, int toRow, int toCol) { using var range = sheet.Cells[fromRow, fromCol, toRow, toCol]; range.Style.Font.Name = "微软雅黑"; range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; range.Style.Font.Size = 14; range.Style.Font.Bold = true; range.Style.Border.Top.Style = ExcelBorderStyle.Thin; range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; range.Style.Border.Left.Style = ExcelBorderStyle.Thin; range.Style.Border.Right.Style = ExcelBorderStyle.Thin; //range.SetBackgroundColor(System.Drawing.SelectedColor.FromArgb(142, 169, 219)); } /// /// 创建Excel文件添加Excel表格 /// /// /// /// public static void WriteExcel(string filePath, string sheetName, Action action) { using ExcelPackage package = new(); var sheet = package.Workbook.Worksheets.Add(sheetName); using Stream stream = new FileStream(filePath, FileMode.Create); action(package); package.SaveAs(stream); } /// /// 写入Excel /// /// /// public static void WriteExcel(string filePath, Action action) { var fileInfo = new FileInfo(filePath); if (fileInfo.Exists) { try { fileInfo.Delete(); } catch (IOException) { throw new IOException("文件正在使用"); } using ExcelPackage package = new(fileInfo); action(package); package.Save(); } else { using ExcelPackage package = new(fileInfo); action(package); package.Save(); //throw new ArgumentNullException(); } } }