Thanks for the sample project.
I have evaluated your scenario/ case using your sample project a bit. Well, the problem is MS Excel automatically converts the numeric data (numbers with more than 11 digits) to scientific notations for bigger values. Also, it stores DateTime data in numeric values automatically. So, for your specific needs, you have to tweak your sample code a bit. First you got to convert your specific columns’ data type to string (as you need the data to be pasted as they are seen), you may do it by cloning your DataTable, you will import all your records to your cloned DataTable. Now you will use Aspose.Cells APIs (Cells.ImportDataTable()) to import data from cloned DataTable. This would work fine. I have written the following sample code to accomplish your task, please refer to the updated code segment and you may add/update your code in your original project/ scenario accordingly for your needs:
DataTable dt = new DataTable(“table”);
dt.Columns.Add(new DataColumn(“Id”, typeof(int)));
dt.Columns.Add(new DataColumn(“Text”, typeof(decimal)));//this column must be display like a string
dt.Columns.Add(new DataColumn(“Value”, typeof(string)));
dt.Columns.Add(new DataColumn(“Date”, typeof(DateTime)));//this column must be display like a string
dt.Rows.Add(new object[] { 1234567, 1234567891234567, “zdffff”, “31.05.2015 00:00:00” });
dt.Rows.Add(new object[] { 1234567, 1234567891234567, “zdffff”, “31.05.2015 00:00:00” });
dt.Rows.Add(new object[] { 1234567, 1234567891234567, “zdffff”, “31.05.2015 00:00:00” });
dt.Rows.Add(new object[] { 1234567, 1234567891234567, “zdffff”, “31.05.2015 00:00:00” });
Workbook workBook = new Workbook(FileFormatType.Xlsx);
Worksheet worksheet = workBook.Worksheets[0];
//Convert your underlying columns’ data type to string
//get all records in a duplicated data table
DataTable dtCloned = dt.Clone();
dtCloned.Columns[1].DataType = typeof(string);
dtCloned.Columns[3].DataType = typeof(string);
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
//Import data from your cloned DataTable to worksheet cells.
worksheet.Cells.ImportDataTable(dtCloned, true, 0, 0, true, false);
//Set the style/formatting for background color for your desired cells
Style style = workBook.CreateStyle();
style.Pattern = BackgroundType.Solid;
style.ForegroundColor = Color.Red;
worksheet.Cells[“B3”].SetStyle(style);
worksheet.Cells[“D3”].SetStyle(style);
//Auto-fit the columns
worksheet.AutoFitColumns();
workBook.Save(@“e:\test2\out1.xlsx”);
Hope, this helps you a bit.
Thank you.