I am working with some data in a DataSet
and trying to import it to an excel file using OpenXml
in C#
and I was successful in doing so but in my DataSet
I do have some columns which are of type DateTime
, integer
and double
but I code is importing all the columns as plain text and making it impossible for me to sort them by their value on the worksheet. I am using the following code
public void ExportDataSet()
{
try
{
string fromFormat = "dd/MM/yyyy";
string toFormat = "MM-dd-yyyy";
DateTime newDate = DateTime.ParseExact(DateTime.Today.ToString(fromFormat), fromFormat, null);
string filedate = newDate.ToString(toFormat);
string destination = @"Z:\Physical DB Data " + filedate + ".xls";
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();
DataSet ds = new DataSet();
ds = GetPhysicalGrainReportAutomation();
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 headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
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 ex)
{
MessageBox.Show(ex.Message);
}
}
What should I do for this code to work as normal and not import those fields as their own type? It worked fine when I looped and imported the dataset using interop
but it is taking very long time as my DataSet
is very huge of about 15 columns
and more than 50000 rows
.
I thought the issue was at declaring the columns List as String as follows
List<String> columns = new List<string>();
and
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
But not sure how to deal with this.
I would recommend you to use NPOI XSSF/HSSF in order to avoid this because it facilitates cell value property to set as integer, datetime, formula etc. and it is fast too. so you only be left with checking your dataset value and set the cell property according to the type. For more information visit https://npoi.codeplex.com/SourceControl/latest
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.