DataSet ds;
DataTable dt=ds.Tables["table1"];
StringWriter sw=new StringWriter();
sw.WriteLine("自动编号,姓名,年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
我现在用上面的这种方法导excel,有这么一个问题,我的dataset中有像0002081318这样的数据,导出excel后打开后变成了2081318这样,请问如何解决?
DataTable dt=ds.Tables["table1"];
StringWriter sw=new StringWriter();
sw.WriteLine("自动编号,姓名,年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
我现在用上面的这种方法导excel,有这么一个问题,我的dataset中有像0002081318这样的数据,导出excel后打开后变成了2081318这样,请问如何解决?
在SQL语句中加上强制字符转换(实际上是对EXCEL的)
strSql="select ''''+id as id ,''''+name as name from tb";
这样在取出的数值上加上了一个"'",而"'"在excel被认为是字符型的数据.会以字符型显示,否则导出时excel把它当成数字了.导入数据时也是一样处理.虽然方法很笨,但可以解决这个问题.