nuget安装npoi
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 ;
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;
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;
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 ] * 100 d / total_zy, 1 ) ;
double video_zb = Math. Round ( data[ 2 ] * 100 d / 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++ )
{
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>