asp.net 导出 excelle 出现中文乱码
StringWritersw=newStringWriter();HtmlTextWriterhtw=newHtmlTextWriter(sw);this.GvData....
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GvData.RenderControl(htw);
string strHtml = sw.ToString().Trim();
string ExcelFileName = "商品" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls";
string FilePhysicialPathName = Request.PhysicalApplicationPath + "/xls";
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))//如果有此文件,先删除
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GBK"));
Response.Charset = "GBK";
bw.Write(strHtml);
bw.Close();
fs.Close();
Response.Redirect("../xls/商品管理" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls"); 展开
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GvData.RenderControl(htw);
string strHtml = sw.ToString().Trim();
string ExcelFileName = "商品" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls";
string FilePhysicialPathName = Request.PhysicalApplicationPath + "/xls";
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))//如果有此文件,先删除
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GBK"));
Response.Charset = "GBK";
bw.Write(strHtml);
bw.Close();
fs.Close();
Response.Redirect("../xls/商品管理" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls"); 展开
4个回答
展开全部
Response.Clear()
Response.Buffer = True
Response.Charset = "GB2312"
Response.AppendHeader("Content-Disposition", "attachment;filename=ColourVision_Shop.xls")
Response.ContentEncoding = System.Text.Encoding.UTF7
Response.ContentType = "application/ms-excel"
Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.GridView1.RenderControl(oHtmlTextWriter)
Response.Output.Write(oStringWriter.ToString)
Response.Flush()
Response.End()
Response.Buffer = True
Response.Charset = "GB2312"
Response.AppendHeader("Content-Disposition", "attachment;filename=ColourVision_Shop.xls")
Response.ContentEncoding = System.Text.Encoding.UTF7
Response.ContentType = "application/ms-excel"
Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.GridView1.RenderControl(oHtmlTextWriter)
Response.Output.Write(oStringWriter.ToString)
Response.Flush()
Response.End()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将 Encoding.GetEncoding("GBK") 改为 ASCIIEncoding.GetEncoding("gb2312") 试试!
Response.Charset = "GBK" 也改成 Response.Charset = "gb2312" ; 试试
Response.Charset = "GBK" 也改成 Response.Charset = "gb2312" ; 试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵,这个问题我也遇到过。你把:gb2312改为utf-7就ok了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询