Click here to Skip to main content
15,885,932 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am doing a export to excel using infragistics excel . And I want to set the column type of the excel same as the column type of the db.Means if column type is datetime it should create datetime column in the infragistics excel sheet.

I tried below code .

C#
Infragistics.Documents.Excel.Workbook workbook = new Infragistics.Documents.Excel.Workbook();

        // Create the worksheet to represent this data table
        Infragistics.Documents.Excel.Worksheet worksheet = workbook.Worksheets.Add(dtReportData.TableName);

        // Create column headers for each column
        for (int columnIndex = 0; columnIndex < dtReportData.Columns.Count; columnIndex++)
        {
            worksheet.Rows[0].Cells[columnIndex].Value = dtReportData.Columns[columnIndex].ColumnName;
        }

        // Starting at row index 1, copy all data rows in
        // the data table to the worksheet
        int rowIndex = 1;
        foreach (DataRow dataRow in dtReportData.Rows)
        {
            Infragistics.Documents.Excel.WorksheetRow row = worksheet.Rows[rowIndex++];

            for (int columnIndex = 0; columnIndex < dataRow.ItemArray.Length; columnIndex++)
            {
                row.Cells[columnIndex].Value = dataRow.ItemArray[columnIndex];

                if (dtReportData.Columns[columnIndex].DataType == Type.GetType("System.Decimal"))
                {
                    //Here column should be of type decimal.   
                }
                else if (dtReportData.Columns[columnIndex].DataType == Type.GetType("System.DateTime"))
                {
                   //Here column type should be of type datetime.                        
                }

            }

Can anybody help me on this..
Posted
Updated 21-Aug-15 4:18am
v3
Comments
Michael_Davies 21-Aug-15 10:13am    
Set the .NumberFormat to a date string; "dd/MM/yyyy".

One quick way to discover commands is to use excel to record a macro and perform the action, stop recording and take a look at the results.
Sandeepost 21-Aug-15 10:23am    
I tried your suggestion but the excel column is showing all # like '######' but not the date.
Michael_Davies 21-Aug-15 10:28am    
That means your cell contents have overflowed and can not be displayed, widen the column.

Useful page: http://www.c-sharpcorner.com/Blogs/6821/using-numberformats-in-excel.aspx
Sandeepost 21-Aug-15 10:34am    
THanks alot for your help.
But can I expand the cell width by code?
Michael_Davies 21-Aug-15 11:08am    
"One quick way to discover commands is to use excel to record a macro and perform the action, stop recording and take a look at the results."

Columns("A:A").EntireColumn.AutoFit

Most of this you can find using one of the now famous search engines, try asking; c# excel change column width

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900