随笔 - 165, 文章 - 0, 评论 - 18, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ExcelPackage 使用說明

Posted on   火冰·瓶  阅读(1590)  评论(0编辑  收藏  举报

1.使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        public IActionResult Excel()
        {
            string sWebRootFolder = _hostingEnvironment.WebRootPath;
            string sFileName = "测试导出excel.xlsx";
 
            FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
 
            file.Delete();
 
            using (ExcelPackage package = new ExcelPackage(file))
 
            {
                // 添加worksheet
 
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sheet1");
 
                //添加头
                //worksheet.Cells[1, 1].Value = "ID";
                //worksheet.Cells[1, 2].Value = "Name";
                //worksheet.Cells[1, 3].Value = "Url";
 
                worksheet.Cells["A1"].Value = "编号";
                worksheet.Cells["B1"].Value = "姓名";
                worksheet.Cells["C1"].Value = "Url";
                worksheet.Cells["D1"].Value = "时间";
 
                //添加值
                worksheet.Cells["A2"].Value = 1000;
                worksheet.Cells["B2"].Value = "For丨丶";
                worksheet.Cells["C2"].Value = "网页链接";
                worksheet.Cells["D2"].Value = DateTime.Now.ToString();
 
                worksheet.Cells["A3"].Value = 1001;
                worksheet.Cells["B3"].Value = "For丨丶Tomorrow";
                worksheet.Cells["C3"].Value = "网页链接";
                worksheet.Cells["C3"].Style.Font.Bold = true;
 
                package.Save();
            }
            return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName);
        }

 

2.设置样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
worksheet.Cells["A1"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//左右居中
worksheet.Cells["A1"].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;//上下居中
worksheet.Cells["A1:H10"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//A1到H10区域所有单元格左右居中
worksheet.Cells["A1"].Value="四川\r\n成都"//换行   配合下面的style才生效
worksheet.Cells["A1"].Style.WrapText = true; //单元格自动换行
worksheet.Cells["A1:H10"].Style.WrapText = true;   //A1到H10区域所有单元格自动换行
worksheet.Cells["A1:H10"].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin); //A1到H10区域的外边框样式
worksheet.Cells["A1:AA1"].Style.Font.Bold = true; //字体加粗
worksheet.Cells[row,  col].Style.Font.Color.SetColor(Color.Red);           //紅色
worksheet.Cells["A1:H10"].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;//单元格边框
worksheet.Cells["A1:H10"].Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
worksheet.Cells["A1:H10"].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
worksheet.Cells["A1:H10"].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
 
worksheet.Cells[row,  col, row+1,  col+1].Style.Numberformat.Format = "@";                                 //设定格式
worksheet.Cells[row, col].Formula = "=SUM(B1:B2)";// 求和
worksheet.Cells[row,  col, row+1,  col+1].Merge = true;   //合并单元格
worksheet.Cells["A1:A2"].Merge = true;    //合并单元格
 
 
worksheet.Cells[row,  col, row+1,  col+1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow   ) ;  //背景颜色
//注:設定背景顏色,要加下面這段,不然提示(Can't set color when patterntype is not set.)
worksheet.Cells[row, 1, row, 6].Style.Fill.PatternType = ExcelFillStyle.Solid;
 
 
t_Sheet0.Row (1).Height = 54;    //行高
t_Sheet0.Column(1).Width = 13.5;//列寬

  

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示