C# Aspose匯出Excel
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];
sheet.Name=“sheet1”
//迴圈新增多個sheet頁
intRowIndex=0;//行
intcolindex=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");
}