利用NPOI组件,将DataTable导出成EXCEL

.Net技术 码拜 9年前 (2015-07-11) 2165次浏览 0个评论
 

利用NPOI组件,将DataTable导出成EXCEL时,如何设置EXCEL单元格值的数据类型,
是否能够自动识别呢?比如根据数据库的字段类型,缺点EXCEL单元格值的数据类型,

 
NPOI.SS.UserModel.ICell cell = dataRow.CreateCell(j,NPOI.SS.UserModel.CellType.NUMERIC);cell.SetCellValue(Convert.ToDouble(dgv.Rows[i].Cells[j].Value));

http://blog.csdn.net/chinacsharper/article/details/12999435

40分

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里就都按文本类型处理了

 

原来DataTable列还有这个属性啊,谢谢


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明利用NPOI组件,将DataTable导出成EXCEL
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!