Introduction
Open XML is an open standard for creating word-processing documents, presentations, and spreadsheets. Through OOXML we can create office applications on different platform. Open XML is designed for existing word-processing documents, presentations, and spreadsheets that are encoded in binary formats defined by Microsoft Office applications.
An Open XML file is stored in a ZIP archive for packaging and compression.
Spreadsheet workbooks are described by using SpreadsheetML markup. Workbook packages can contain:
Workbook part (required part)
One or more worksheets
Charts
Tables
Custom XML
You can download Open XML SDK from Microsoft site --
http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0HYPERLINK "http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en"&HYPERLINK "http://www.microsoft.com/downloads/en/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en"displaylang=enCreating Excel File
To create excel file through OOXML we need to add following references in our application.
DocumentFormat.OpenXml and
WindowBaseTo create new excel file first we need to create object of spread sheet document
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath,SpreadsheetDocumentType.Workbook);
filepath stores the local path where excel file will be created.
Now we need to add workbook spreadsheetDocument.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = newWorkbook();
Add a WorksheetPart to the WorkbookPart. Worksheet part represents the worksheet in the workbook.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = newWorksheet(newSheetData());
Append a new worksheet and associate it with the workbook.
Sheet sheet = newSheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
sheets.Append(sheet);
Following functions will explain how can we create excel file.
Function for creating Spreadsheet.
public staticvoid CreateSpreadsheetWorkbook(string filepath, DataTable dt)
{
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
FileInfo f = newFileInfo(filepath);
if(f.Exists)
f.Delete();
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath,SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = newWorkbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = newWorksheet(newSheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(newSheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = newSheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
sheets.Append(sheet);
string cl = "";
uint row = 2;
int index;
Cell cell;
foreach (DataRow dr in dt.Rows)
{
for (int idx = 0; idx < dt.Columns.Count; idx++)
{
if (idx >= 26)
cl = "A" + Convert.ToString(Convert.ToChar(65 + idx - 26));
else
cl = Convert.ToString(Convert.ToChar(65 + idx));
SharedStringTablePart shareStringPart;
if (spreadsheetDocument.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
{
shareStringPart = spreadsheetDocument.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
}
else
{
shareStringPart = spreadsheetDocument.WorkbookPart.AddNewPart<SharedStringTablePart>();
}
if (row == 2)
{
index = InsertSharedStringItem(dt.Columns[idx].ColumnName, shareStringPart);
cell = InsertCellInWorksheet(cl, row - 1, worksheetPart);
cell.CellValue = newCellValue(index.ToString());
cell.DataType = newEnumValue<CellValues>(CellValues.SharedString);
}
// Insert the text into the SharedStringTablePart.
index = InsertSharedStringItem(Convert.ToString(dr[idx]), shareStringPart);
cell = InsertCellInWorksheet(cl, row, worksheetPart);
cell.CellValue = newCellValue(index.ToString());
cell.DataType = newEnumValue<CellValues>(CellValues.SharedString);
}
row++;
}
//InsertCellInWorksheet("A", 1, worksheetPart);
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
//InsertText(@"c:\MyXL3.xlx", "Hello");
}

MayankPosted Dec 23, 2018, 5:07 AM
I have a requirement Where I need to insert table in the sheet.For this added a package in the content type like below. select new XElement(dn + "Override", new XAttribute("PartName", "/xl/tables/table1.xml"), new XAttribute("ContentType", "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml") ) But on opening the excel it is showing "can not open the file specific part does not exist in the package".Please suggest.
hello fragrancePosted Feb 20, 2015, 12:55 AM
sir,actually i've a requirement where we have an excel file uploaded in a document library and that excel file we have 10-15 sheets so and in each sheet there are 4-5 tables named as Table1,Table2......So w have to read the Table1 which is in Summary sheet into the data table.Can you please help how do i get that done using above procedure.
GianfrancoPosted Dec 1, 2014, 11:28 AM
this example works on Windows phone?
vamsi krishnaeditedPosted Mar 19, 2013, 4:59 PMEdited Mar 19, 2013, 5:00 PM
is there any way to send the excel directly to client, without saving/creating the excel first on server? Excel Interop dlls work great but have many limitations on server side.
Alex MegasPosted Apr 7, 2012, 9:39 AM
Vivek, thank you very much !! Great work !! You saved me a lot of time ! Keep on posting !!!
vijay gargPosted Jul 6, 2011, 3:45 AM
How can we generate Excel from following input. <TBODY> <TR vAlign=top> <TD vAlign=top> <DIV style="BORDER-BOTTOM: #bfbfbf 1px solid; POSITION: relative; BORDER-LEFT: #bfbfbf 1px solid; OVERFLOW-X: scroll; OVERFLOW-Y: scroll; WIDTH: 900px; HEIGHT: 360px; BORDER-TOP: #bfbfbf 1px solid; BORDER-RIGHT: #bfbfbf 1px solid; align: center" id=ctl00_contentBpoPortal_Mydiv> <TABLE style="WIDTH: 98%; BORDER-COLLAPSE: collapse" id=ctl00_contentBpoPortal_dgReport class="inputformtext" border=1 rules=all cellSpacing=0> <TBODY> <TR style="BORDER-BOTTOM: 2px solid; BORDER-LEFT: 2px solid; BACKGROUND-COLOR: darkgray; FONT-FAMILY: Verdana; WHITE-SPACE: nowrap; HEIGHT: 20px; COLOR: black; FONT-SIZE: x-small; BORDER-TOP: 2px solid; TOP: -2px; FONT-WEIGHT: bold; BORDER-RIGHT: 2px solid" class="gvFixedHeader" align=middle> <TD style="WHITE-SPACE: nowrap">Product Group</TD> <TD style="WIDTH: 200px">Prodn FTE Requested</TD> <TD style="WIDTH: 200px">E Factor</TD> <TD style="WIDTH: 200px">TL Required (Staffing Request)</TD> <TD style="WIDTH: 200px">Billable HeadCount</TD> <TD style="WIDTH: 200px">Prodn FTE On Board</TD> <TD style="WIDTH: 200px">TLs On Board</TD> <TD style="WIDTH: 200px">Unbilled Production</TD></TR> <TR style="BORDER-BOTTOM: #bfbfbf 2px solid; BORDER-LEFT: #bfbfbf 2px solid; FONT-FAMILY: Verdana; WHITE-SPACE: nowrap; HEIGHT: 15px; FONT-SIZE: 10px; BORDER-TOP: #bfbfbf 2px solid; FONT-WEIGHT: normal; BORDER-RIGHT: #bfbfbf 2px solid" align=left> <TD style="WHITE-SPACE: nowrap">BA&R</TD> <TD style="WIDTH: 200px">4</TD> <TD style="WIDTH: 200px">0</TD> <TD style="WIDTH: 200px">2</TD> <TD style="WIDTH: 200px">6</TD> <TD style="WIDTH: 200px">0</TD> <TD style="WIDTH: 200px">0.50</TD> <TD style="WIDTH: 200px">-5.50</TD></TR> ...
Suri Meenakshi SundaramPosted Dec 14, 2010, 5:59 AM
Its Nice article. keep posting similar to this