ASP.NET从数据库导出数据到EXCEL表,日期格式乱码,其它字段正常,求大神解救~
截图与后台代码如下,日期在数据库中我用的是smalldatetime类型/****************************导出数据****************...
截图与后台代码如下,日期在数据库中我用的是smalldatetime类型
/****************************导出数据******************************/
protected void btn_exp_Click(object sender, EventArgs e)
{
string FileName = "标题" + ".xls";
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.AddHeader("content-type", "application / ms - excel");//设置输出文件的类型
this.EnableViewState = false;
StringWriter tw = new StringWriter(); //System.IO
//循环每一条记录
paper_data.con();
das = paper_data.GetDataBase("select * from x_paper");
//打印表头
for (int k = 2; k < grd_paper.Columns.Count; k++)
tw.Write(grd_paper.Columns[k].HeaderText+ '\t');
tw.WriteLine();
//打印数据行
for (int i = 0; i < das.Tables[0].Rows.Count; i++)
{
for (int j = 1; j < das.Tables[0].Columns.Count; j++)
{
tw.Write(das.Tables[0].Rows[i][j].ToString() + '\t');
}
tw.WriteLine();
}
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//必须写这个;
} 展开
/****************************导出数据******************************/
protected void btn_exp_Click(object sender, EventArgs e)
{
string FileName = "标题" + ".xls";
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.AddHeader("content-type", "application / ms - excel");//设置输出文件的类型
this.EnableViewState = false;
StringWriter tw = new StringWriter(); //System.IO
//循环每一条记录
paper_data.con();
das = paper_data.GetDataBase("select * from x_paper");
//打印表头
for (int k = 2; k < grd_paper.Columns.Count; k++)
tw.Write(grd_paper.Columns[k].HeaderText+ '\t');
tw.WriteLine();
//打印数据行
for (int i = 0; i < das.Tables[0].Rows.Count; i++)
{
for (int j = 1; j < das.Tables[0].Columns.Count; j++)
{
tw.Write(das.Tables[0].Rows[i][j].ToString() + '\t');
}
tw.WriteLine();
}
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//必须写这个;
} 展开
3个回答
展开全部
在日期字符串前面加一个单引号'
追问
在建表时,我插入的日期格式是这样的,比如 ‘2013-08-13’,你所说的在日期字符串前面加一个单引号',是在建表时加呢,可是我已经有单引号了,如‘2013-08-13’,还是在哪里加呢?望解答一下,谢谢~
追答
在这里 for (int j = 1; j < das.Tables[0].Columns.Count; j++)
{
if(j==日期列)
{
tw.Write("'" +das.Tables[0].Rows[i][j].ToString()+ '\t');
}
else
{
tw.Write(das.Tables[0].Rows[i][j].ToString() + '\t');
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询