Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();

           }
       }
Posted
Updated 26-Feb-17 18:40pm
v2

1 solution

Go to below url. Hope it will help you

Header row bold in excel
 
Share this answer
 
Comments
s23user 27-Feb-17 1:15am    
It did not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900