在导出的ActionResult中,我能够将模型加载到ExcelPackage中。
我遇到麻烦的地方是在应用LoadFromCollection
后在每个单元格周围分配边框。当AutoFitColumns
正确适用时,我应用的边框样式仅适用于Cells["D1"]
,不适用于表格。
BorderAround
成功在整个表周围放置边框,但是我宁愿将边框应用于表的单元格[[inside。有办法吗?
// Fill worksheet with data to export
var modelCells = worksheet.Cells["D1"];
var border = modelCells.Style.Border.Top.Style = modelCells.Style.Border.Left.Style = modelCells.Style.Border.Right.Style = modelCells.Style.Border.Bottom.Style = ExcelBorderStyle.Medium;
modelCells
.LoadFromCollection(Collection: exportQuery, PrintHeaders: true)
.AutoFitColumns();
AutoFitColumns{}
。我所需要做的就是在modelRange
变量中输入开始列和结束列。var modelCells = worksheet.Cells["D1"];
var modelRows = exportQuery.Count()+1;
string modelRange = "D1:F" + modelRows.ToString();
var modelTable = worksheet.Cells[modelRange];
// Assign borders
modelTable.Style.Border.Top.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Left.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Right.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
// Fill worksheet with data to export
modelCells.LoadFromCollection(Collection: exportQuery, PrintHeaders: true);
modelTable.AutoFitColumns();
var package = new ExcelPackage(new MemoryStream());
var ws = package.Workbook.Worksheets.Add("Test");
var modelTable = ws.Cells;
modelTable.Style.Border.Top.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Left.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Right.Style = ExcelBorderStyle.Thin;
modelTable.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
modelTable.AutoFitColumns();