首页 > 其他 > 详细

ExcelPackage 使用說明

时间:2021-08-19 08:52:36      阅读:15      评论:0      收藏:0      [点我收藏+]

1.使用方法

        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("部落");

                //添加头
                //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.设置样式

worksheet.Cells["A1"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//左右居中
worksheet.Cells["A1"].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;//上下居中

worksheet.Cells["A1:H" + (i - 1)].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
worksheet.Cells["A1:H" + (i - 1)].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;

  

  

ExcelPackage 使用說明

原文:https://www.cnblogs.com/fireicesion/p/15158884.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!