该方法把DataTable中的非string类型的数据改为string类型,以避免导出到Excel表时显示错误。
public DataTable changeDT(DataTable dt)
{
DataTable datatable = new DataTable();
datatable = dt.Clone();
foreach (DataColumn col in datatable.Columns)
{
if (col.DataType != typeof(string))
{
col.DataType = typeof(string);
}
}
foreach (DataRow row in dt.Rows)
{
DataRow nr = datatable.NewRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
nr[i] = row[i].ToString();
}
datatable.Rows.Add(nr);
}
return datatable;
}