当前位置:   article > 正文

C# EPPlus读写Excel

c# epplus

记录一下自己学习使用EPPlus读写Excel的知识点。

EPPlus Software logo

EPPlus开源库,可以直接在vs上的NuGet包里直接安装。

1.写入Excel文件

  1. using OfficeOpenXml;
  2. .
  3. .
  4. .
  5. ExcelPackage.LicenseContext = LicenseContext.Commercial;//EPPlus需要添加许可
  6. using (var package=new ExcelPackage())
  7. {
  8. //创建Worksheets
  9. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Inventory");
  10. //添加表格头
  11. worksheet.Cells[1, 1].Value = "ID";
  12. // worksheet.Cells[1,1].Value = "ID";
  13. worksheet.Cells[1, 2].Value = "Product";
  14. worksheet.Cells[1, 3].Value = "Quantity";
  15. worksheet.Cells[1, 4].Value = "Price";
  16. worksheet.Cells[1, 5].Value = "Value";
  17. //添加元素
  18. worksheet.Cells["A2"].Value = 12001;
  19. worksheet.Cells["B2"].Value = "Nails";
  20. worksheet.Cells["C2"].Value = 37;
  21. worksheet.Cells["D2"].Value = 3.99;
  22. worksheet.Cells["A3"].Value = 12002;
  23. worksheet.Cells["B3"].Value = "Hammer";
  24. worksheet.Cells["C3"].Value = 5;
  25. worksheet.Cells["D3"].Value = 12.10;
  26. worksheet.Cells["A4"].Value = 12003;
  27. worksheet.Cells["B4"].Value = "Saw";
  28. worksheet.Cells["C4"].Value = 12;
  29. worksheet.Cells["D4"].Value = 15.37;
  30. //添加计算格式
  31. worksheet.Cells["E2:E4"].Formula = "C2*D2";
  32. //表格格式
  33. //从(1,1)到(1,5)范围内表格格式就行调整
  34. using(var range=worksheet.Cells[1,1,1,5])
  35. {
  36. range.Style.Font.Bold = true; //字体加粗
  37. range.Style.Fill.PatternType = ExcelFillStyle.Solid;//单元格填充
  38. range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue); //背景颜色
  39. range.Style.Font.Color.SetColor(Color.White);//字体颜色
  40. }
  41. worksheet.Cells["A5:E5"].Style.Border.Top.Style = ExcelBorderStyle.Thin;
  42. worksheet.Cells["A5:E5"].Style.Font.Bold = true;
  43. worksheet.Cells[5, 3, 5, 5].Formula = String.Format("SUBTOTAL(9,{0})", new ExcelAddress(2, 3, 4, 3).Address);//累加
  44. worksheet.Cells["C2:C5"].Style.Numberformat.Format = "#,##0"; //数字格式
  45. worksheet.Cells["D2:E5"].Style.Numberformat.Format = "#,##0.00";
  46. worksheet.Cells["A1:E4"].AutoFilter = true;
  47. worksheet.Cells["A2:A4"].Style.Numberformat.Format = "@";
  48. // 设置标题文本
  49. worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\" Inventory";
  50. // 将页码添加到页脚加上总页数
  51. worksheet.HeaderFooter.OddFooter.RightAlignedText =
  52. string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber, ExcelHeaderFooter.NumberOfPages);
  53. // 将图纸名称添加到页脚
  54. worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\" Inventory";
  55. // 将页码添加到页脚加上总页数
  56. worksheet.HeaderFooter.OddFooter.RightAlignedText =
  57. string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber, ExcelHeaderFooter.NumberOfPages);
  58. // 将图纸名称添加到页脚
  59. worksheet.HeaderFooter.OddFooter.CenteredText = ExcelHeaderFooter.SheetName;
  60. // 将文件路径添加到页脚
  61. worksheet.HeaderFooter.OddFooter.LeftAlignedText = ExcelHeaderFooter.FilePath + ExcelHeaderFooter.FileName;
  62. //更改图纸视图以在页面布局模式下显示
  63. worksheet.View.PageLayoutView = true;
  64. //添加保存路径
  65. FileInfo fileInfo = new FileInfo(@"F:\系统默认\桌面\LearningEPPlus\sample.xlsx");
  66. //创建并保存Excel
  67. package.SaveAs(fileInfo);

实际效果如下 

2.读取Excel文件中某一sheet内的数据

  1. string excelFilePath = ofd.FileName;
  2. string sheetName = "sheetName";//读取sheetName的sheet
  3. textBoxFilePath.Text = ofd.FileName;
  4. FileInfo fileInfo = new FileInfo(excelFilePath);
  5. using (ExcelPackage package = new ExcelPackage(fileInfo))
  6. {
  7. // 获取指定名称的工作表
  8. ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetName];
  9. if (worksheet != null)
  10. {
  11. // 获取表格数据的起始行和列
  12. int startRow = worksheet.Dimension.Start.Row;
  13. int startCol = worksheet.Dimension.Start.Column;
  14. // 获取表格数据的结束行和列
  15. int endRow = worksheet.Dimension.End.Row;
  16. int endCol = worksheet.Dimension.End.Column;
  17. float[,] MapData = new float[endRow , endCol ];
  18. // 从 Excel 表格读取数据并存放在对象中
  19. for (int row = startRow + 1; row <= endRow; row++) // 第一行通常是标题,所以从第二行开始读取数据
  20. {
  21. for (int col = startCol+1; col <= endCol; col++)
  22. {
  23. MapData[row - 2, col-2] = Convert.ToSingle(worksheet.Cells[row, col].Value);
  24. }
  25. }
  26. }
  27. else
  28. {
  29. Console.WriteLine($"Sheet '{sheetName}' not found in the Excel file.");
  30. }
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/118564
推荐阅读
相关标签
  

闽ICP备14008679号