My requirement is to export huge records with rows equal to 5 lakhs. I was using OpenXml approach like this :
public void ExportCheck(DataSet ds, string destination)
{
using (var workbook = SpreadsheetDocument.Create(destination, 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 ds.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 emptyRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
DocumentFormat.OpenXml.Spreadsheet.Row colRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<DataColumn> columns = new List<DataColumn>();
foreach (System.Data.DataColumn column in table.Columns)
{
columns.Add(column);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue("<b>"+ column.ColumnName + "</b>");
colRow.AppendChild(cell);
}
sheetData.AppendChild(colRow);
string valueN;
foreach (System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (DataColumn col in columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
if (col.DataType.Equals(typeof(byte[])) == true)
{
valueN = ConvertByteArraytoInteger(dsrow[col.ColumnName]).ToString();
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(valueN);
}
else
{
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
}
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
}
}
But now i have requirements in which data to be exported can be up to 10 lakh records with substantial number of columns. So i had to implement OpenXml Sax type approach as DOM approach is bit slow and memory consuming in case of large data.
I couldn't find any helpful link which shows me how to use that approach and convert data table/data set to excel. In most link they are using existing spreadsheet to create new excel. Can anyone provide changes to be made to above code to implement SAX approach
or any link regarding the same.
Any help is deeply appreciated
Thanks