简体   繁体   中英

How can we specify the columns DataType to string, number Datetime in Excel/SpreadSheet when Export from c#

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();

The image is as Follows 在此处输入图片说明

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

  1. Credit-Number
  2. Debit-Number
  3. Date- Date-time
  4. Code-String
  5. 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

Ok then here are two options I can come up quickly.

  1. If you have few options of column choice, then map columnName <--> NumberFormat
  2. If you have too much column possibility, then map primitiveType <--> NumberFormat (However you can only have one floating format for ie all Decimal columns, regardless of the business need of the specific column)

Here is an example for first option: Use a dictionary to lookup which number format you should inject into each column.

var headerFormatMap = new Dictionary<string, string>(); // <columnHeader, NumberFormat>
// i.e fill it like this
headerFormatMap.Add("TransactionDate", "MM/DD/YYYY");
headerFormatMap.Add("Debit", "##.000"); // Display 37.75 as 37.750.

Give formatting of all your possible columns like this. And when the time comes to export, get the correct numberFormat and inject it to its column:

// Assuming your first row is header. (I remember excel indexes start from 1)
// Do this foreach column
Range range = (Excel.Range)worksheetobject.Cells[1,1];
var header = (string)range.Value;
range.EntireColumn.NumberFormat = headerFormatMap[header];    

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM