asp.net core用NPOI导出EXCEL电子表格

nuget安装npoi

在这里插入图片描述

//导出excel; 数量统计
public async Task<IActionResult> ExportAmountTotalExcel()
{
    await Task.Yield();
    var amount = await imgBLL.GetImgViewAmount();

    var workbook = new HSSFWorkbook();
    var sheet = workbook.CreateSheet();

    var headFont = workbook.CreateFont();
    //加粗
    headFont.IsBold = true;
    //字体大小
    headFont.FontHeightInPoints = 12;

    //单元格样式1
    var cellStyleTitle = workbook.CreateCellStyle();
    cellStyleTitle.SetFont(headFont);
    
    //亮绿标题背景色
	cellStyleTitle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.BrightGreen.Index;
	cellStyleTitle.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
	cellStyleTitle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BrightGreen.Index;
            
    //单元格边框
    cellStyleTitle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    cellStyleTitle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    cellStyleTitle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    cellStyleTitle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    //垂直方向居中
    cellStyleTitle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;

    //单元格样式2
    var cellStyle = workbook.CreateCellStyle();
    //单元格边框
    cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
    cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
    cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
    //字符左对齐
    cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;

    //自适应列宽
    //sheet.AutoSizeColumn(0);


    //标题
    var row = sheet.CreateRow(0);
    row.HeightInPoints = 16;
    var cell = row.CreateCell(0);
    cell.SetCellValue("资源总数量");
    cell.CellStyle = cellStyleTitle;

    cell = row.CreateCell(1);
    cell.SetCellValue("图片总数量");
    cell.CellStyle = cellStyleTitle;

    cell = row.CreateCell(2);
    cell.SetCellValue("图片总数量占比");
    cell.CellStyle = cellStyleTitle;

    cell = row.CreateCell(3);
    cell.SetCellValue("视频总数量");
    cell.CellStyle = cellStyleTitle;

    cell = row.CreateCell(4);
    cell.SetCellValue("视频总数量占比");
    cell.CellStyle = cellStyleTitle;

    //数据
    int[] data = amount.Item1;
    int total_zy = data[0] + data[2];
    //图片占比,除以资源数
    double img_zb = Math.Round(data[0] * 100d / total_zy, 1);
    //视频占比,除以资源数
    double video_zb = Math.Round(data[2] * 100d / total_zy, 1);
    var row1 = sheet.CreateRow(1);
    row1.HeightInPoints = 16;
    var cell2 = row1.CreateCell(0);
    cell2.SetCellValue(total_zy);
    cell2.CellStyle = cellStyle;

    //图片总数
    cell2 = row1.CreateCell(1);
    cell2.SetCellValue(data[0]);
    cell2.CellStyle = cellStyle;

    cell2 = row1.CreateCell(2);
    cell2.SetCellValue(img_zb + "%");
    cell2.CellStyle = cellStyle;

    //视频总数
    cell2 = row1.CreateCell(3);
    cell2.SetCellValue(data[2]);
    cell2.CellStyle = cellStyle;

    cell2 = row1.CreateCell(4);
    cell2.SetCellValue(video_zb + "%");
    cell2.CellStyle = cellStyle;

    //设置列宽
    for (int i = 0; i < sheet.GetRow(0).LastCellNum; i++)
    {
        //14为office里面设置的列宽值
        sheet.SetColumnWidth(i, 256 * 18);
    }

    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
    workbook.Write(memoryStream);
    return File(memoryStream.ToArray(), "application/vnd.ms-excel");
}

前端页面代码:

<a href="@Url.Action("ExportAmountTotalExcel","count")" download="总数量统计"  >导出</a>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值