Column Data Type to Date Time


Hi

I have one Date time column in the data table. when i do importdataTable to Excel it will display the datetime value in integer format.

In Data Table it will be in the date time format only.

so how can i avoid that, is there any property we need to set before importing into the excel or for that perticuler column we can set property to accept the Data Time Values.

Please help me out to resolve this issue.

Regads
Mohammed Irfan


Hi Irfan,

Thank you for considering Aspose.

Please use the following overload method to convert DateTime into your desired format.

Cells.ImportDataTable(DataTable 1 dataTable,bool isFieldNameShown, int firstRow,

int firstColumn, int rowNumber, int columnNumber, bool insertRows,string dateFormatString, bool convertStringToNumber);

For parameter details, please see the following documentation link,

Also, you can see the following sample code for how to implement the above mentioned code:

//Instantiating a “Products” DataTable object

DataTable dataTable = new DataTable(“Products”);

//Adding columns to the DataTable object

dataTable.Columns.Add(“Product ID”, typeof(Int32));

dataTable.Columns.Add(“Product Name”, typeof(string));

dataTable.Columns.Add(“Stock In Date”, typeof(DateTime));

//Creating an empty row in the DataTable object

DataRow dr = dataTable.NewRow();

//Adding data to the row

dr[0] = 1;

dr[1] = “Aniseed Syrup”;

dr[2] = DateTime.Now;

//Adding filled row to the DataTable object

dataTable.Rows.Add(dr);

//Creating another empty row in the DataTable object

dr = dataTable.NewRow();

//Adding data to the row

dr[0] = 2;

dr[1] = “<?xml:namespace prefix = st1 ns = “urn:schemas-microsoft-com:office:smarttags” />Boston Crab Meat”;

dr[2] = DateTime.Now;

//Adding filled row to the DataTable object

dataTable.Rows.Add(dr);

Workbook workbook = new Workbook();

workbook.Worksheets[0].Cells.ImportDataTable(dataTable,true,0,0,2,3,true,“dd/mm/yyyy”,true);

workbook.Save(“C:\dateformat.xls”);

Thank You & Best Regards,