if (dt.Columns[j].DataType == typeof(int))
{
cell.SetCellValue((int)dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(float))
{
cell.SetCellValue((float)dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(double))
{
cell.SetCellValue((double)dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(string))
{
cell.SetCellValue(dt.Rows[i][j].ToString());
}
SetCellValue()方法有重载的,你可以根据传入的DataTable的列的类型,调用某个重载
如果你将数据简单的ToString()传进去,那么当然Excel里就都按文本类型处理了