public ActionResult ExportExcel(string keyValue, string columnJson)
{
//创建对象
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
//全局样式
CommonExportStyle style = new CommonExportStyle();
//单个sheet页重新命名
Aspose.Cells.Worksheet sheet = wb.Worksheets[0];
=“sheet1”
//循环添加多个sheet 页
int RowIndex=0;//行
int colindex=0;列
for (int i = 0; i < data.Count; i++)
{
wb.Worksheets.Add(data[i].NAME);
//开始添加值
sheet.Cells[RowIndex, colindex].PutValue("姓名:");
sheet.Cells[RowIndex, colindex + 1].PutValue(“”张三);
表格同理组装表头,在组装数据行
//单元格添加样式
sheet.Cells.SetColumnWidth(colindex, 15);//列宽度
//sheet.AutoFitColumns(RowIndex, colindex);//自适应列宽
//单元格内容右对齐
Aspose.Cells.Style bzstyle = new Aspose.Cells.Style();
bzstyle.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Right;
sheet.Cells[RowIndex + 1, 0].SetStyle(bzstyle);
//跨行
sheet.Cells.Merge(RowIndex + 1, 1, 1, maxcell - 1);
//单元格内容较多换行
Aspose.Cells.Style bzstylevalue = new Aspose.Cells.Style();
bzstylevalue.IsTextWrapped = true;
sheet.Cells[RowIndex + 1, 1].SetStyle(bzstylevalue);
sheet.Cells.SetRowHeight(RowIndex + 1, 30); //高度一定要够不然显示不全
//全局样式 表格效果
Aspose.Cells.Range wstrange = sheet.Cells.CreateRange(0, 0, RowIndex + 1, 20);
Aspose.Cells.StyleFlag stFlag = new Aspose.Cells.StyleFlag();
stFlag.Borders = true;
wstrange.ApplyStyle(style.TableContentStyle(), stFlag);
}
//最后一步输出返回文件
MemoryStream memStream = new MemoryStream();
wb.Save(memStream, Aspose.Cells.FileFormatType.Xlsx);
byte[] filedata = memStream.ToArray();
return File(filedata, "application/octet-stream", "测试导出.xlsx");
}