小弟最近在搞一个Web项目。其中有一个功能,将数据导出Excel表。
导出代码如下:
/// <summary>
/// 执行导出
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>
public static void DataSetToExcel(DataSet oDS, string fileName)
{
if (oDS == null || oDS.Tables[0] == null || oDS.Tables[0].Rows.Count == 0) { return; }
HttpContext.Current.Response.Clear();
//Encoding pageEncode = Encoding.GetEncoding(PageEncode);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType = "application/vnd-excel";//"application/vnd.ms-excel";
//Response.ContentType = "application/x-octet-stream";//"application/vnd.ms-excel";
HttpContext.Current.Response.ContentType = "text/csv";//"application/vnd.ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
System.IO.StringWriter oSW = new System.IO.StringWriter();
HtmlTextWriter oHW = new HtmlTextWriter(oSW);
DataGrid dg = new DataGrid();
dg.DataSource = oDS.Tables[0];
dg.DataBind();
dg.RenderControl(oHW);
HttpContext.Current.Response.Write(oSW.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
BindDatalist("work");
if (ProductInfoManager.GetProductInfoAllByStatus("work").Tables[0].DefaultView.Count > 0)
{ DataSetToExcel(ProductInfoManager.GetProductInfoAllByStatus("work"), "ss");
}
else
{
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "提示", "<script type='text/javascript'>alert('没有数据');</script>");
} }在简体系统中,这样导出是不会报错,不会乱码的。
可我需要在繁体系统中导出数据。导出时乱码了。
能否在导出的时候,将简体字转换成繁体了?
请各位大哥给点提示。
导出代码如下:
/// <summary>
/// 执行导出
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>
public static void DataSetToExcel(DataSet oDS, string fileName)
{
if (oDS == null || oDS.Tables[0] == null || oDS.Tables[0].Rows.Count == 0) { return; }
HttpContext.Current.Response.Clear();
//Encoding pageEncode = Encoding.GetEncoding(PageEncode);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType = "application/vnd-excel";//"application/vnd.ms-excel";
//Response.ContentType = "application/x-octet-stream";//"application/vnd.ms-excel";
HttpContext.Current.Response.ContentType = "text/csv";//"application/vnd.ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
System.IO.StringWriter oSW = new System.IO.StringWriter();
HtmlTextWriter oHW = new HtmlTextWriter(oSW);
DataGrid dg = new DataGrid();
dg.DataSource = oDS.Tables[0];
dg.DataBind();
dg.RenderControl(oHW);
HttpContext.Current.Response.Write(oSW.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
BindDatalist("work");
if (ProductInfoManager.GetProductInfoAllByStatus("work").Tables[0].DefaultView.Count > 0)
{ DataSetToExcel(ProductInfoManager.GetProductInfoAllByStatus("work"), "ss");
}
else
{
this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "提示", "<script type='text/javascript'>alert('没有数据');</script>");
} }在简体系统中,这样导出是不会报错,不会乱码的。
可我需要在繁体系统中导出数据。导出时乱码了。
能否在导出的时候,将简体字转换成繁体了?
请各位大哥给点提示。
来改变
System.Text.Encoding.GetEncoding("GB2312");
System.Text.Encoding.GetEncoding("GB2312");是输出流为简体的
输出流是繁体的,应该是什么了?
肯定乱码。还不如不设置呢。
比如是我项目里面是简体中文。
但是我导出,要将简体中文转变为繁体中文,在Excel里面显示。
比如是我项目里面是简体中文。
但是我导出,要将简体中文转变为繁体中文,在Excel里面显示。