[英]EPPlus: how can I assign border around each cell after I apply LoadFromCollection?
在我的導出 ActionResult 中,我能夠將模型加載到我的 ExcelPackage 中。
在應用LoadFromCollection
我遇到問題的地方是在每個單元格周圍分配邊框。 雖然AutoFitColumns
正確應用,但我應用的邊框樣式僅適用於Cells["D1"]
,而不適用於表格。
BorderAround
成功地在整個表格周圍放置了一個邊框,但我寧願將邊框應用於表格內的單元格。 有沒有辦法做到這一點?
// 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();
如果我知道模型具有的列數,我可以使用函數計算行數並執行以下操作:
var modelRows = exportQuery.Count()+1;
string modelRange = "D1:F" + modelRows.ToString();
var modelTable = worksheet.Cells[modelRange];
或者,有更多的上下文。 我確認 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();
這將做到這一點 - worksheet.Cells[worksheet.Dimension.Address]
using (ExcelPackage excel = new ExcelPackage())
{
excel.Workbook.Worksheets.Add(sheetName);
excel.SaveAs(excelFile);
string headerRange = "A1:" + char.ConvertFromUtf32(dtJobs.Columns.Count + 64) + "1";
// Target a worksheet
var worksheet = excel.Workbook.Worksheets[sheetName];
#region design Header
//worksheet.Cells[headerRange].Style.Font.Bold = true;
worksheet.Cells[headerRange].Style.Font.Size = 11;
worksheet.Cells[headerRange].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[headerRange].Style.Fill.BackgroundColor.SetColor(Color.DarkGray);
//worksheet.Cells[headerRange].Style.WrapText = true;
worksheet.Cells[headerRange].Style.Font.Color.SetColor(Color.White);
worksheet.Cells[headerRange].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
worksheet.Cells[headerRange].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
#endregion
var excelWorksheet = excel.Workbook.Worksheets[sheetName];
excelWorksheet.Cells[excelWorksheet.Dimension.End.Row, 1].LoadFromDataTable(dtJobs, true);
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
worksheet.Cells[worksheet.Dimension.Address].Style.Border.Top.Style = ExcelBorderStyle.Thin;
worksheet.Cells[worksheet.Dimension.Address].Style.Border.Left.Style = ExcelBorderStyle.Thin;
worksheet.Cells[worksheet.Dimension.Address].Style.Border.Right.Style = ExcelBorderStyle.Thin;
worksheet.Cells[worksheet.Dimension.Address].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
excel.SaveAs(excelFile);
return filePath;
}
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();
// calculate
ws.Calculate();
saveFileDialog_SaveExcel.Filter = "Excel files (*.xlsx)|*.xlsx";
var dialogResult = saveFileDialog_SaveExcel.ShowDialog();
if (dialogResult == DialogResult.OK)
{
package.SaveAs(new FileInfo(saveFileDialog_SaveExcel.FileName));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.