在OpenXML中使用C#设置A1:M30的边框,可以通过以下步骤实现:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
// 创建一个新的Excel文档
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("Sample.xlsx", SpreadsheetDocumentType.Workbook);
// 添加WorkbookPart
WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// 添加WorksheetPart
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// 添加Sheet
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
sheets.Append(sheet);
// 保存并关闭文档
workbookPart.Workbook.Save();
spreadsheetDocument.Close();
// 打开Excel文档
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open("Sample.xlsx", true);
// 获取WorksheetPart
WorksheetPart worksheetPart = spreadsheetDocument.WorkbookPart.WorksheetParts.First();
// 获取SheetData
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
// 设置边框
for (int row = 1; row <= 30; row++)
{
for (char column = 'A'; column <= 'M'; column++)
{
string cellReference = $"{column}{row}";
// 获取单元格
Cell cell = sheetData.Descendants<Cell>().FirstOrDefault(c => c.CellReference.Value == cellReference);
if (cell != null)
{
// 创建边框
Border border = new Border(
new LeftBorder(new Color() { Rgb = new HexBinaryValue("00000000") }) { Style = BorderStyleValues.Thin },
new RightBorder(new Color() { Rgb = new HexBinaryValue("00000000") }) { Style = BorderStyleValues.Thin },
new TopBorder(new Color() { Rgb = new HexBinaryValue("00000000") }) { Style = BorderStyleValues.Thin },
new BottomBorder(new Color() { Rgb = new HexBinaryValue("00000000") }) { Style = BorderStyleValues.Thin }
);
// 获取或创建CellStyle
CellFormat cellFormat = cell.StyleIndex != null ? (CellFormat)spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ChildElements[(int)cell.StyleIndex.Value] : new CellFormat();
// 设置边框样式
cellFormat.BorderId = spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.BorderStyles.Count++;
spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.BorderStyles.AppendChild(border);
// 设置CellStyle
cellFormat.ApplyBorder = true;
cellFormat.BorderId = spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.BorderStyles.Count - 1;
// 更新或添加CellStyle
if (cell.StyleIndex != null)
{
spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ReplaceChild(cellFormat, spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ChildElements[(int)cell.StyleIndex.Value]);
}
else
{
spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.AppendChild(cellFormat);
}
// 更新单元格
cell.StyleIndex = (uint)spreadsheetDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count - 1;
}
}
}
// 保存并关闭文档
spreadsheetDocument.WorkbookPart.Workbook.Save();
spreadsheetDocument.Close();
以上代码将会创建一个名为"Sample.xlsx"的Excel文档,并设置A1:M30的边框为细线条。你可以根据需要修改代码中的文件名和边框样式。
领取专属 10元无门槛券
手把手带您无忧上云