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

nuget安装npoi

asp.net core用NPOI导出EXCEL电子表格_第1张图片

//导出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>

你可能感兴趣的:(NPOI,asp.net,core,asp.net,core,.net,core,导出excel,npoi,c#)