|
|
using Newtonsoft.Json;
|
|
|
using OfficeOpenXml;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.ComponentModel;
|
|
|
using System.Data;
|
|
|
using System.IO;
|
|
|
using System.Reflection;
|
|
|
using CompressorXN_Log;
|
|
|
|
|
|
namespace CompressorXN_HelperLib
|
|
|
{
|
|
|
public class ExcelHelper
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 读取Excel
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="filePath">Excel文件路径</param>
|
|
|
/// <param name="sheetName">sheet名称</param>
|
|
|
/// <param name="hasHeader"></param>
|
|
|
/// <returns></returns>
|
|
|
public static T ReadFromExcel<T>(string filePath, string sheetName = "sheet0", bool hasHeader = true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//设置许可证上下文为非商业版
|
|
|
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
|
|
using (var excelPack = new ExcelPackage())
|
|
|
{
|
|
|
//Load excel stream
|
|
|
using (var stream = File.OpenRead(filePath))
|
|
|
{
|
|
|
excelPack.Load(stream);
|
|
|
}
|
|
|
//处理第一个工作表。(如果处理多个表格,可以在此用for循环处理)
|
|
|
var ws = excelPack.Workbook.Worksheets[sheetName];
|
|
|
DataTable excelasTable = new DataTable();
|
|
|
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(firstRowCell.Text))
|
|
|
{
|
|
|
string firstColumn = string.Format("Column {0}", firstRowCell.Start.Column);
|
|
|
excelasTable.Columns.Add(hasHeader ? firstRowCell.Text : firstColumn);
|
|
|
}
|
|
|
}
|
|
|
var startRow = hasHeader ? 2 : 1;
|
|
|
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
|
|
|
{
|
|
|
var wsRow = ws.Cells[rowNum, 1, rowNum, excelasTable.Columns.Count];
|
|
|
DataRow row = excelasTable.Rows.Add();
|
|
|
foreach (var cell in wsRow)
|
|
|
{
|
|
|
row[cell.Start.Column - 1] = cell.Text;
|
|
|
}
|
|
|
}
|
|
|
if (excelasTable.Rows.Count > 0)
|
|
|
{
|
|
|
//将所有内容作为泛型获取,最终定是否强制转换为所需类型
|
|
|
var generatedType = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(excelasTable));
|
|
|
return (T)Convert.ChangeType(generatedType, typeof(T));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return default;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "解析Excel失败");
|
|
|
return default;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 读取Excel为Table
|
|
|
/// </summary>
|
|
|
/// <param name="strFilePath"></param>
|
|
|
/// <param name="tbName"></param>
|
|
|
/// <param name="hasHeader"></param>
|
|
|
/// <returns></returns>
|
|
|
public static DataTable GetExcel(string strFilePath, string tbName = "dt1", bool hasHeader = true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
FileInfo file = new FileInfo(strFilePath);
|
|
|
//指定EPPlus使用非商业证书
|
|
|
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
|
|
using (ExcelPackage ep = new ExcelPackage(file))
|
|
|
{
|
|
|
ExcelWorksheet ws = ep.Workbook.Worksheets[tbName];
|
|
|
int maxColumnNum = ws.Dimension.End.Column;//最大列
|
|
|
int minColumnNum = ws.Dimension.Start.Column;//最小列
|
|
|
|
|
|
int maxRowNum = ws.Dimension.End.Row;//最小行
|
|
|
int minRowNum = ws.Dimension.Start.Row;//最大行
|
|
|
|
|
|
DataTable vTable = new DataTable();
|
|
|
var rowCells = ws.Cells[1, 1, 1, ws.Dimension.End.Column];
|
|
|
DataColumn vC;
|
|
|
if (hasHeader)
|
|
|
{
|
|
|
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(firstRowCell.Text))
|
|
|
{
|
|
|
vC = new DataColumn(firstRowCell.Text, typeof(string));
|
|
|
vTable.Columns.Add(vC);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
for (int j = 1; j <= maxColumnNum; j++)
|
|
|
{
|
|
|
vC = new DataColumn("col_" + j, typeof(string));
|
|
|
vTable.Columns.Add(vC);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int n = 2; n <= maxRowNum; n++)
|
|
|
{
|
|
|
DataRow vRow = vTable.NewRow();
|
|
|
for (int m = 1; m <= maxColumnNum; m++)
|
|
|
{
|
|
|
vRow[m - 1] = ws.Cells[n, m].Value;
|
|
|
}
|
|
|
vTable.Rows.Add(vRow);
|
|
|
}
|
|
|
return vTable;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
throw ex;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// DataTable转list
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="dataTable"></param>
|
|
|
/// <returns></returns>
|
|
|
public static List<T> ConvertDataTableToList<T>(DataTable dataTable) where T : new()
|
|
|
{
|
|
|
List<T> list = new List<T>();
|
|
|
bool isEndForeach = false;//是否结束外层循环
|
|
|
foreach (DataRow row in dataTable.Rows)
|
|
|
{
|
|
|
T obj = new T();
|
|
|
int colIndex = 0;//第几列
|
|
|
foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
|
|
|
{
|
|
|
string displayName = propertyInfo.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName;
|
|
|
// string propertyName=propertyInfo.Name
|
|
|
if (string.IsNullOrEmpty(displayName))
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
if (dataTable.Columns.Contains(displayName))
|
|
|
{
|
|
|
string cellVal = row[displayName]?.ToString();
|
|
|
//如果第一列值为空则结束循环
|
|
|
if (colIndex == 0 && string.IsNullOrEmpty(cellVal))
|
|
|
{
|
|
|
isEndForeach = true;
|
|
|
break;
|
|
|
}
|
|
|
// 根据需要进行类型转换
|
|
|
propertyInfo.SetValue(obj, Convert.ChangeType(cellVal, propertyInfo.PropertyType), null);
|
|
|
// propertyInfo.SetValue(obj, row[propertyInfo.Name]);
|
|
|
}
|
|
|
colIndex++;
|
|
|
}
|
|
|
//结束外层循环
|
|
|
if (isEndForeach)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
list.Add(obj);
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|