EPPlus.Core(OfficeOpenXml) 获取 Excel 数据集合
/// <summary>
/// 获取 Excel Sheet 数据集合
/// </summary>
/// <param name="path">文件路径</param>
/// <param name="index">Sheets下标,可选参数默认 0</param>
/// <returns></returns>
public List<string[]> GetSheetValues(string path, int index = 0)
{
var file = new FileInfo(path);
if (file == null) return null;
var strList = new List<string[]>();
var arrayList = new ArrayList();
using (var package = new ExcelPackage(file))
{
var worksheet = package.Workbook.Worksheets[index];
var columns = worksheet.Dimension.Columns;
for (int row = 1; row <= worksheet.Dimension.Rows; row++)
{
for (int cel = 1; cel <= columns; cel++)
{
arrayList.Add(worksheet.Cells[row, cel].Value.ToString());
}
var arrString = (string[])arrayList.ToArray(typeof(string));
strList.Add(arrString);
arrayList.Clear();
arrString = null;
}
return strList;
}
}
首先通过 NuGet 获取 EPPlus.Core 包,然后就可以使用了。