使用开放XML,如何在Excel工作表中格式化标题列粗体? [英] Using open XML, how do I format header column bold in excel sheet?
问题描述
借助互联网中的大量参考,我可以创建一种方法,将数据集导出为excel文件,为数据集中的每个表创建多个选项卡。
这是我的代码:
public static void ExportDataSet(DataSet dataset,string excelFilePath)
{
尝试
{
使用(var workbook = SpreadsheetDocument.Create(excelFilePath,DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
foreach(数据集中的System.Data.DataTable表。表)
{
var sheetPart = workbook.WorkbookPart.AddNewPart< worksheetpart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet .SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet .Sheets sheet = workbook.WorkbookPart.Workbook.GetFirstChild< documentformat.openxml.spreadsheet.sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if(sheets.Elements< documentformat.openxml.spreadsheet.sheet>()。Count()> 0)
{
sheetId =
sheets.Elements< documentformat.openxml.spreadsheet.sheet>()。选择(s => s.SheetId.Value).Max()+ 1;
}
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat。 OpenXml.Spreadsheet.Sheet(){Id = relationshipId,SheetId = sheetId,Name = table.TableName};
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List< string> columns = new List< string>();
if(table.Rows.Count == 0)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet。 CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(**今天没什么**);
headerRow.AppendChild(cell) ;
sheetData.AppendChild(headerRow);
}
其他
{
foreach(table.Columns中的System.Data.DataColumn列)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml。 Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach(系统。 Table.Rows中的Data.DataRow dsrow
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach(列中的字符串列表)
{
DocumentFormat.OpenXml.Spreadsheet。 Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow [col] .ToString()); //
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
}
}
}
catch(例外)
{
throw;
}
}
现在我需要将标题设置为粗体。我怎么做?
我尝试过的事情:
我发现了这个方法,它抛出了null引用异常。在查看上面的代码时,我需要在此方法中进行哪些更改。如果有人能帮助我,我将不胜感激。
With help of lot of reference in internet i am able to create an method that export data set to excel file, creating multiple tab for each table in Dataset.
this is my code:
public static void ExportDataSet(DataSet dataset, string excelFilePath)
{
try
{
using(var workbook = SpreadsheetDocument.Create(excelFilePath, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
foreach(System.Data.DataTable table in dataset.Tables)
{
var sheetPart = workbook.WorkbookPart.AddNewPart<worksheetpart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<documentformat.openxml.spreadsheet.sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if(sheets.Elements<documentformat.openxml.spreadsheet.sheet>().Count() > 0)
{
sheetId =
sheets.Elements<documentformat.openxml.spreadsheet.sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<string> columns = new List<string>();
if(table.Rows.Count == 0)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue("** Nothing for today **");
headerRow.AppendChild(cell);
sheetData.AppendChild(headerRow);
}
else
{
foreach(System.Data.DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach(System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach(String col in columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
}
}
}
catch(Exception)
{
throw;
}
}
Now i need to set header to bold. How do i do it?
What I have tried:
I found out this method, which throws null reference exception. What do i need to make change in this method looking at my above code. I would be grateful if anyone could help me out.
public static void SetSpreadsheetHeaderBold(string excelFilePath)
{
using(SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(excelFilePath, true))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
WorkbookStylesPart stylesPart = spreadsheetDocument.WorkbookPart.WorkbookStylesPart;
DocumentFormat.OpenXml.Spreadsheet.Font font1 = new DocumentFormat.OpenXml.Spreadsheet.Font(
new Bold(),
new FontSize() { Val = 11 },
new Color() { Rgb = new HexBinaryValue() { Value = "000000" } },
new FontName() { Val = "Calibri" });
stylesPart.Stylesheet.Fonts.Append(font1);
stylesPart.Stylesheet.Save();
UInt32Value fontId = Convert.ToUInt32(stylesPart.Stylesheet.Fonts.ChildElements.Count - 1);
CellFormat cf = new CellFormat() { FontId = fontId, FillId = 0, BorderId = 0, ApplyFont = true };
stylesPart.Stylesheet.CellFormats.Append(cf);
Row r = sheetData.Elements<Row>().First<Row>();
int index1 = stylesPart.Stylesheet.CellFormats.ChildElements.Count - 1;
foreach(Cell c in r.Elements<Cell>())
{
c.StyleIndex = Convert.ToUInt32(index1);
worksheetPart.Worksheet.Save();
}
spreadsheetDocument.Close();
}
}
推荐答案
转到下面的网址。希望它会对你有所帮助
excel中的标题行粗体
Go to below url. Hope it will help you
Header row bold in excel
这篇关于使用开放XML,如何在Excel工作表中格式化标题列粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!