添加项目文件。
This commit is contained in:
320
Sai.Toolkit.Core/Heplers/EPPlusHelper.cs
Normal file
320
Sai.Toolkit.Core/Heplers/EPPlusHelper.cs
Normal file
@@ -0,0 +1,320 @@
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
|
||||
using OfficeOpenXml;
|
||||
using OfficeOpenXml.Style;
|
||||
|
||||
namespace Sai.Toolkit.Core.Helpers;
|
||||
|
||||
public class EPPlusHelper //公式计算最后需要调用Calculate()
|
||||
{
|
||||
/// <summary>
|
||||
/// DataTable导出为Excel
|
||||
/// </summary>
|
||||
/// <param name="destFileName"></param>
|
||||
/// <param name="dt"></param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给第一个表中的单元格赋值
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="rowNum"></param>
|
||||
/// <param name="columnNum"></param>
|
||||
/// <param name="value"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 工作表转换为DataTable,首行为表头
|
||||
/// </summary>
|
||||
/// <param name="worksheet">行列索引从1开始</param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 工作表转换为DataTable
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取工作表
|
||||
/// </summary>
|
||||
/// <param name="excelFilePath"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
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("工作表不存在。");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取Excel所有表
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
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
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置单元格的值
|
||||
/// </summary>
|
||||
/// <param name="sheet"></param>
|
||||
/// <param name="rowNum">1开始</param>
|
||||
/// <param name="columnNum">1开始</param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetCellValue(ExcelWorksheet sheet, int rowNum, int columnNum, string value)
|
||||
{
|
||||
//sheet.Cells[rowNum, ColumnNum].Value = value;
|
||||
sheet.SetValue(rowNum, columnNum, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过单元格名设置值
|
||||
/// </summary>
|
||||
/// <param name="sheet"></param>
|
||||
/// <param name="cellName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetCellValue(ExcelWorksheet sheet, string cellName, string value)
|
||||
{
|
||||
sheet.Cells[cellName].Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给工作表中的单元格赋值
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="sheetname"></param>
|
||||
/// <param name="rowNum"></param>
|
||||
/// <param name="columnNum"></param>
|
||||
/// <param name="value"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给工作表中的单元格赋值
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="sheetname"></param>
|
||||
/// <param name="cellName">单元格名称,比如A1</param>
|
||||
/// <param name="value"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给工作表中的单元格赋值
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="index">表索引,0开始</param>
|
||||
/// <param name="cellName">单元格名称,如A1</param>
|
||||
/// <param name="value"></param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置样式
|
||||
/// </summary>
|
||||
/// <param name="sheet"></param>
|
||||
/// <param name="fromRow"></param>
|
||||
/// <param name="fromCol"></param>
|
||||
/// <param name="toRow"></param>
|
||||
/// <param name="toCol"></param>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置表头样式
|
||||
/// </summary>
|
||||
/// <param name="sheet"></param>
|
||||
/// <param name="fromRow"></param>
|
||||
/// <param name="fromCol"></param>
|
||||
/// <param name="toRow"></param>
|
||||
/// <param name="toCol"></param>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建Excel文件添加Excel表格
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <param name="action"></param>
|
||||
public static void WriteExcel(string filePath, string sheetName, Action<ExcelPackage> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入Excel
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="action"></param>
|
||||
public static void WriteExcel(string filePath, Action<ExcelPackage> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user