EPPlus:应用LoadFromCollection之后如何在每个单元格周围分配边框?

问题描述 投票:27回答:2

在导出的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();
c# asp.net-mvc-5 epplus
2个回答
48
投票
或具有更多上下文。我验证了EPPlus将在Cells []中接受一个字符串变量,这使我可以选择整个表并正确应用边框样式和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();


0
投票
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();
© www.soinside.com 2019 - 2024. All rights reserved.