Форматирование ячеек при экспорте в Excel с OpenXML - PullRequest
0 голосов
/ 19 сентября 2019

У меня возникают проблемы при попытке отформатировать определенные ячейки отчета, поскольку я экспортирую свои данные в таблицу Excel с использованием директивы OfficeOpenXML.Я использую директиву System.Drawing для .Fill и .Merge определенных ячеек, но любые ячейки, которые я пытался применить, ограничивающие, похоже, не работают, или я добавляю ненужные директивы, которые нарушают другие части моего кода.

Я применяю форматирование к своему листу Excel здесь:

//Format the excel sheet
var allCells = excelSheet.Cells[1, 1, excelSheet.Dimension.End.Row,  excelSheet.Dimension.End.Column];
var allCellsFont = allCells.Style.Font;
allCellsFont.SetFromFont(new Font("Calibri", 10));
excelSheet.DefaultRowHeight = 20;

//Activity Report Title
var titleCells = excelSheet.Cells[1, 1];
excelSheet.Cells[1, 1, 1, excelSheet.Dimension.End.Column].Merge = true;
excelSheet.Row(1).Height = 25;
titleCells.Value = "Activity Report";
titleCells.Style.Font.Bold = true;
titleCells.Style.Font.Size = 12;
titleCells.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
titleCells.Style.Fill.BackgroundColor.SetColor(Color.LightSkyBlue);
titleCells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

//Date Title
var dateCells = excelSheet.Cells[2, 1];
excelSheet.Cells[2, 1, 2, excelSheet.Dimension.End.Column].Merge = true;
if (!string.IsNullOrWhiteSpace(txtToDate.Text))
{
       excelSheet.Cells[2, 1].Value = "DATE: " + txtFromDate.Text + "    TO    " + txtToDate.Text;
}
else
{
       excelSheet.Cells[2, 1].Value = "DATE: " + txtFromDate.Text + "    TO CURRENT";

dateCells.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
dateCells.Style.Fill.BackgroundColor.SetColor(Color.LightSkyBlue);
dateCells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;

//Format for the datetime in cols A1 and A2
excelSheet.Column(1).Style.Numberformat.Format = "yyyy-mm-dd, hh:mm:ss";
excelSheet.Column(2).Style.Numberformat.Format = "yyyy-mm-dd, hh:mm:ss";
excelSheet.Row(4).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
excelSheet.Cells[excelSheet.Dimension.Address].AutoFitColumns();
excelSheet.View.ZoomScale = 100;

Я пытался добавить стили границ следующим образом:

Border border = new Border();
border.LeftBorder = new LeftBorder();
border.RightBorder = new RightBorder();
border.TopBorder = new TopBorder();
border.BottomBorder = new BottomBorder();

Тогда мне нужнодобавить эти директивы, если я хочу, чтобы это работало, но я все еще не знаю, как правильно применить это к моим ячейкам.

using Border = DocumentFormat.OpenXml.Spreadsheet.Border;
using LeftBorder = DocumentFormat.OpenXml.Spreadsheet.LeftBorder;
using RightBorder = DocumentFormat.OpenXml.Spreadsheet.RightBorder;
using TopBorder = DocumentFormat.OpenXml.Spreadsheet.TopBorder;
using BottomBorder = DocumentFormat.OpenXml.Spreadsheet.BottomBorder;

Любая помощь будет принята с благодарностью!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...