EPPlus:应用 LoadFromCollection 后如何在每个单元格周围分配边框?
EPPlus: how can I assign border around each cell after I apply LoadFromCollection?
在我的导出 ActionResult 中,我能够将模型加载到我的 ExcelPackage 中。
我遇到问题的地方是在应用 LoadFromCollection
后在每个单元格周围分配边框。虽然 AutoFitColumns
正确应用,但我应用的边框样式仅适用于 Cells["D1"]
,但不适用于 table.
BorderAround
成功地在整个 table 周围放置了边框,但我宁愿将边框应用于 内部 table 的单元格].有什么办法可以做到吗?
// 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[] 中的字符串变量,这允许我 select 整个 table 并正确应用我的边框样式和 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();
// 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));
}
这样就可以了 - 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;
}
在我的导出 ActionResult 中,我能够将模型加载到我的 ExcelPackage 中。
我遇到问题的地方是在应用 LoadFromCollection
后在每个单元格周围分配边框。虽然 AutoFitColumns
正确应用,但我应用的边框样式仅适用于 Cells["D1"]
,但不适用于 table.
BorderAround
成功地在整个 table 周围放置了边框,但我宁愿将边框应用于 内部 table 的单元格].有什么办法可以做到吗?
// 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[] 中的字符串变量,这允许我 select 整个 table 并正确应用我的边框样式和 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();
// 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));
}
这样就可以了 - 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;
}