EPplus的读写
- POI(http://npoi.codeplex.com/)
- MyXls(http://sourceforge.net/projects/myxls/)
- Koogra(http://sourceforge.net/projects/koogra/)
- ExcelLibrary(http://code.google.com/p/excellibrary/)
- ExcelPackage(http://excelpackage.codeplex.com/)
- EPPlus(http://epplus.codeplex.com/)
- LinqToExcel(http://code.google.com/p/linqtoexcel/)
其中大部分的意见都认为“对于Excel 97-2003格式,还是用NPOI最好;而对于2007(xlsx)以上版本,可以使用EPPlus”。由于工作中基本上都是使用xlsx,因此这里直接选择了EPPlus。
EPPlus读取excel:
using (ExcelPackage package = new ExcelPackage(new FileStream(path, FileMode.Open))) { for (int i = 1; i <= package.Workbook.Worksheets.Count; ++i) { ExcelWorksheet 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 } } } } }
EPPlus写入excel:
using (ExcelPackage package = new ExcelPackage()) { ExcelWorksheet sheet = package.Workbook.Worksheets.Add("Sheet1"); sheet.Cells[1, 1].Value = "1"; sheet.Cells[1, 2].Value = "2"; sheet.Cells[1, 3].Value = "3"; sheet.Cells[1, 4].Value = "4"; sheet.Cells[1, 5].Value = "5"; sheet.Cells[1, 6].Value = "6"; using (Stream stream = new FileStream(path, FileMode.Create)) { package.SaveAs(stream); } }
原文地址:https://www.cnblogs.com/libla/p/5824296.html