Tue Dec 12, 2017 10:04 pm
Hi Mr. Yang,
I copied my code as below. Could you let me know why I got the waring message when I open the created excel? Thank you!
// Create a DataTable
DataTable dt = new DataTable("MyDataTable");
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
DataRow row1 = dt.NewRow();
row1["Id"] = 1;
row1["Date"] = new DateTime(2017, 12, 1);
row1["Name"] = "Michelle";
row1["Price"] = 122.12;
dt.Rows.Add(row1);
DataRow row2 = dt.NewRow();
row2["Id"] = 2;
row2["Date"] = new DateTime(2017, 12, 15);
row2["Name"] = "Chen";
row2["Price"] = 15000.45;
dt.Rows.Add(row2);
// Create instance of CellEXport
CellExport myCellExport = new CellExport();
WorkSheet sheet = new WorkSheet();
sheet.DataExported = false;
sheet.SheetName = "ABC";
sheet.ItemType = CellItemType.Row;
sheet.AutoFitColWidth = true;
this._cellExport.Sheets.Add(sheet);
// Add Excel Header row
Cell cell1 = new Cell();
cell1.Row = 1;
cell1.Column = 1;
cell1.Value = "Id";
sheet.Cells.Add(cell1);
Cell cell2 = new Cell();
cell2.Row = 1;
cell2.Column = 2;
cell2.Value = "Date";
sheet.Cells.Add(cell2);
Cell cell3 = new Cell();
cell3.Row = 1;
cell3.Column = 3;
cell3.Value = "Name";
sheet.Cells.Add(cell3);
Cell cell4 = new Cell();
cell4.Row = 1;
cell4.Column = 4;
cell4.Value = "Price";
sheet.Cells.Add(cell4);
// Add Excel content rows
int currentRow = 1;
foreach (DataRow dr in dt.Rows)
{
currentRow++;
object[] a = dr.ItemArray;
for (int i = 0; i < dt.Columns.Count; i++)
{
Cell cell = new Cell();
cell.Row = currentRow;
cell.Column = i+1;
string Michelle = a[i].GetType().ToString();
string Michelle_DateTime = typeof(DateTime).ToString();
string Michelle_decimal = typeof(decimal).ToString();
if (typeof(DateTime) == a[i].GetType())
{
cell.Value = Convert.ToDateTime(a[i]).ToString();
}
else if (typeof(Int16) == a[i].GetType() || typeof(Int32) == a[i].GetType())
{
cell.CellType = CellType.Numeric;
cell.NumericFormat = String.Empty;
cell.Value = a[i];
}
else if (typeof(double) == a[i].GetType() || typeof(decimal) == a[i].GetType())
{
cell.CellType = CellType.Numeric;
cell.NumericFormat = "$#,###,###,##0.00";
cell.Value = a[i];
}
else if (typeof(byte[]) == a[i].GetType())
{
cell.Value = this.ByteToString((byte[])a[i]);
}
else
{
cell.Value = a[i].ToString();
}
sheet.Cells.Add(cell);
}
}
myCellExport.Sheets.Add(sheet);
myCellExport.SaveToFile("C:\\Temp2\\MyTest.xls");