am using the following code to export the data to Excel from c#. I will Provide one sample image with this to know better what i needed.
NOt only for a single column we cannot say this type is coming to a particular column in excel. during exporting of diffrent data
the code am using is
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
Create(savingFileName, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
// Add a WorksheetPart to the WorkbookPart.
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
var bold1 = new System.Windows.Documents.Bold();
DocumentFormat.OpenXml.Spreadsheet.CellFormat cf = new DocumentFormat.OpenXml.Spreadsheet.CellFormat();
// Add Sheets to the Workbook.
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets;
sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(new DocumentFormat.OpenXml.Spreadsheet.Sheets());
// Append a new worksheet and associate it with the workbook.
var sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = sheetId,
Name = "Sheet" + sheetId
};
sheets.Append(sheet);
//Add Header Row.
var headerRow = new Row();
headerRow.RowIndex = 10;
var cell=new Cell();
foreach (DataColumn column in dtCollection.Columns)
{
cell = new Cell
{
DataType = CellValues.String, CellValue = new CellValue(column.ColumnName.ToUpper())
};
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (DataRow row in dtCollection.Rows)
{
var newRow = new Row();
foreach (DataColumn col in dtCollection.Columns)
{
cell = new Cell
{
DataType = CellValues.String,
CellValue = new CellValue(row[col].ToString())
};
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
workbookpart.Workbook.Save();
spreadsheetDocument.Close();
in this above pic you can see Debit and credit column it should be number but with my code it can be only string
Column Type
- Credit-Number
- Debit-Number
- Date- Date-time
- Code-String
- opening Balance-Number
with the above requirement i need to export the datatable to excel in excel each column should be differnt datatype. how can i do this can any one help me to complete my work