ExcelPackage 使用說明

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 使用說明

上一篇:css 背景平铺100% 浏览器缩小后背景不完整bug


下一篇:MVC显示Base64图片