本文主要是介绍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 包,然后就可以使用了。
这篇关于EPPlus.Core(OfficeOpenXml) 获取 Excel 数据集合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!