EPPlus操作excel

一 基本使用

 1 string outputFile = "d:\\output.xlsx";        //输出的excel文件
 2 string templateFile = "d:\\template.xlsx"; //excel模板文件
 3 using (var package = new ExcelPackage(new FileInfo(outputFile), new FileInfo(templateFile)))
 4 {
 5    var worksheet = package.Workbook.Worksheets["Sheet1"];
 6    worksheet.Cells[1,1].Value = "文本内容";
 7    //TODO:
 8    //...
 9    
10    package.Save();   //保存
11 }

 

二 设置样式,格式

 1 //设置行高,列宽
 2 worksheet.Row(1).Height = 22.50;
 3 worksheet.Row(2).Height = 22.50;
 4 worksheet.Column(1).Width= 40.50;
 5 worksheet.Column(2).Width= 40.50;
6 7 //设置单元格 8 using (var r = worksheet.Cells[fromRow, fromCol, toRow, toCol]) 9 { 10 //是否合并单元格 11 r.Merge = true; 12 13 //单元格边框线条 14 r.Style.Border.Top.Style = ExcelBorderStyle.Thin; 15 r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; 16 r.Style.Border.Left.Style = ExcelBorderStyle.Thin; 17 r.Style.Border.Right.Style = ExcelBorderStyle.Thin; 18 19 //单元格边框颜色 20 r.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black); 21 r.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.Black); 22 r.Style.Border.Left.Color.SetColor(System.Drawing.Color.Black); 23 r.Style.Border.Right.Color.SetColor(System.Drawing.Color.Black); 24 }

 

三 拷贝单元格

1 var destWorksheet = package.Workbook.Worksheets["目标worksheet"];
2 var srcWorksheet = package.Workbook.Worksheets["源worksheet"];
3 var destCellRange = destWorksheet.Cells[fromRow1, fromColumn1, toRow1, toColumn1];
4 var srcCellRange = srcWorksheet.Cells[fromRow2, fromColumn2, toRow2, toColumn2];
5 srcCellRange.Copy(destCellRange);

 

posted @ 2020-04-28 14:53  秒杀5S  阅读(923)  评论(0编辑  收藏  举报