通过Response.write输出Excel时,乱码
2009-08-30 15:55
399 查看
我作成一个Html页面(使用aspx:Table控件生成表格),然后把这个页面以Excel格式发送到客户端,打开它,数据多时,里面是乱码,数据少时时正确的。
Code
protected void Page_Load(object sender, EventArgs e)
{
string str = Request["SessionID"];
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift-jis");
object data = Application[str + "DownLoadExcel"];
object con = Application[str + "SearchDic"];
string fileName = Application[str + "TemplateFilePath"].ToString();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
//Response.AppendHeader("Content-Disposition", "inline;filename=" + fileName);
System.Collections.Generic.Dictionary<string, string> condition = (System.Collections.Generic.Dictionary<string, string>)con;
TableItemStyle tableStyle = new TableItemStyle();
tableStyle.BorderStyle = BorderStyle.Solid;
tableStyle.BorderColor = System.Drawing.Color.Black;
TableItemStyle tableStyle2 = new TableItemStyle();
tableStyle2.BorderStyle = BorderStyle.Solid;
tableStyle2.BorderColor = System.Drawing.Color.Black;
foreach (string item in condition.Keys)
{
TableRow tr = new TableRow();
TableCell c1 = new TableCell();
c1.ColumnSpan = 3;
if (item.StartsWith(" ・検索条件"))
{
if (item.Equals(" ・検索条件0"))
{
c1.Text = " ・検索条件";
}
}
else
{
c1.Text = item;
}
TableCell c2 = new TableCell();
c2.Text = HttpUtility.HtmlEncode(condition[item]);
c2.ColumnSpan = 15;
tr.Cells.Add(c1);
tr.Cells.Add(c2);
this.Table1.Rows.Add(tr);
}
if (data != null)
{
System.Data.DataSet dataSource = (System.Data.DataSet)data;
if (dataSource.Tables[0].Rows.Count == 0)
{
this.lblResult.Text = "◆集計結果: 0件";
}
else
{
this.lblResult.Text = "◆集計結果";
TableRow header1 = new TableRow();
TableRow header2 = new TableRow();
header1.Cells.Add(new TableCell());
header2.Cells.Add(new TableCell());
for (int i = 0; i < dataSource.Tables[0].Columns.Count; i++)
{
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
if (i == 0)
{
c1.Text = "(単位:件)";
}
else
{
c1.Text = i.ToString();
}
c2.Text = HttpUtility.HtmlEncode(dataSource.Tables[0].Columns[i].ColumnName);
if (i == 0)
{
c2.CssClass = "xl26";
}
else if (i == dataSource.Tables[0].Columns.Count - 1)
{
c2.CssClass = "xl25";
}
else
{
c2.CssClass = "xl27";
}
header1.Cells.Add(c1);
header2.Cells.Add(c2);
}
this.Table2.Rows.Add(header1);
this.Table2.Rows.Add(header2);
}
for (int i = 0; i < dataSource.Tables[0].Rows.Count; i++)
{
TableRow row = new TableRow();
TableCell c1 = new TableCell();
// c1.ApplyStyle(tableStyle2);
if (i != dataSource.Tables[0].Rows.Count - 1)
{
c1.Text = (i + 1).ToString();
}
row.Cells.Add(c1);
for (int j = 0; j < dataSource.Tables[0].Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = HttpUtility.HtmlEncode(dataSource.Tables[0].Rows[i][j].ToString());
if (i == dataSource.Tables[0].Rows.Count - 1)
{
if (j == 0)
{
cell.CssClass = "xl26";
}
else if (j == dataSource.Tables[0].Columns.Count - 1)
{
cell.CssClass = "xl25";
}
else
{
cell.CssClass = "xl27";
}
}
else if (i == dataSource.Tables[0].Rows.Count - 2)
{
if (j == 0)
{
cell.CssClass = "xl32";
}
else if (j == dataSource.Tables[0].Columns.Count - 1)
{
cell.CssClass = "xl36";
}
else
{
cell.CssClass = "xl33";
}
}
else
{
if (j == 0)
{
cell.CssClass = "xl30";
}
else if (j == dataSource.Tables[0].Columns.Count - 1)
{
cell.CssClass = "xl35";
}
else
{
cell.CssClass = "xl31";
}
}
row.Cells.Add(cell);
}
this.Table2.Rows.Add(row);
}
}
else
{
this.lblResult.Text = "◆集計結果: 0件";
}
Application.Remove(str + "DownLoadExcel");
Application.Remove(str + "SearchDic");
Application.Remove(str + "TemplateFilePath");
}解决方案:
往html的header里追加<meta http-equiv="Content-Type" content="text/html;charset=SHIFT_JIS">就没有乱码了
但是为什么8列单元格出现乱码,六列单元格就没有问题,百思不得其解。请知者告诉原委,谢谢!
Code
protected void Page_Load(object sender, EventArgs e)
{
string str = Request["SessionID"];
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("shift-jis");
object data = Application[str + "DownLoadExcel"];
object con = Application[str + "SearchDic"];
string fileName = Application[str + "TemplateFilePath"].ToString();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
//Response.AppendHeader("Content-Disposition", "inline;filename=" + fileName);
System.Collections.Generic.Dictionary<string, string> condition = (System.Collections.Generic.Dictionary<string, string>)con;
TableItemStyle tableStyle = new TableItemStyle();
tableStyle.BorderStyle = BorderStyle.Solid;
tableStyle.BorderColor = System.Drawing.Color.Black;
TableItemStyle tableStyle2 = new TableItemStyle();
tableStyle2.BorderStyle = BorderStyle.Solid;
tableStyle2.BorderColor = System.Drawing.Color.Black;
foreach (string item in condition.Keys)
{
TableRow tr = new TableRow();
TableCell c1 = new TableCell();
c1.ColumnSpan = 3;
if (item.StartsWith(" ・検索条件"))
{
if (item.Equals(" ・検索条件0"))
{
c1.Text = " ・検索条件";
}
}
else
{
c1.Text = item;
}
TableCell c2 = new TableCell();
c2.Text = HttpUtility.HtmlEncode(condition[item]);
c2.ColumnSpan = 15;
tr.Cells.Add(c1);
tr.Cells.Add(c2);
this.Table1.Rows.Add(tr);
}
if (data != null)
{
System.Data.DataSet dataSource = (System.Data.DataSet)data;
if (dataSource.Tables[0].Rows.Count == 0)
{
this.lblResult.Text = "◆集計結果: 0件";
}
else
{
this.lblResult.Text = "◆集計結果";
TableRow header1 = new TableRow();
TableRow header2 = new TableRow();
header1.Cells.Add(new TableCell());
header2.Cells.Add(new TableCell());
for (int i = 0; i < dataSource.Tables[0].Columns.Count; i++)
{
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
if (i == 0)
{
c1.Text = "(単位:件)";
}
else
{
c1.Text = i.ToString();
}
c2.Text = HttpUtility.HtmlEncode(dataSource.Tables[0].Columns[i].ColumnName);
if (i == 0)
{
c2.CssClass = "xl26";
}
else if (i == dataSource.Tables[0].Columns.Count - 1)
{
c2.CssClass = "xl25";
}
else
{
c2.CssClass = "xl27";
}
header1.Cells.Add(c1);
header2.Cells.Add(c2);
}
this.Table2.Rows.Add(header1);
this.Table2.Rows.Add(header2);
}
for (int i = 0; i < dataSource.Tables[0].Rows.Count; i++)
{
TableRow row = new TableRow();
TableCell c1 = new TableCell();
// c1.ApplyStyle(tableStyle2);
if (i != dataSource.Tables[0].Rows.Count - 1)
{
c1.Text = (i + 1).ToString();
}
row.Cells.Add(c1);
for (int j = 0; j < dataSource.Tables[0].Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = HttpUtility.HtmlEncode(dataSource.Tables[0].Rows[i][j].ToString());
if (i == dataSource.Tables[0].Rows.Count - 1)
{
if (j == 0)
{
cell.CssClass = "xl26";
}
else if (j == dataSource.Tables[0].Columns.Count - 1)
{
cell.CssClass = "xl25";
}
else
{
cell.CssClass = "xl27";
}
}
else if (i == dataSource.Tables[0].Rows.Count - 2)
{
if (j == 0)
{
cell.CssClass = "xl32";
}
else if (j == dataSource.Tables[0].Columns.Count - 1)
{
cell.CssClass = "xl36";
}
else
{
cell.CssClass = "xl33";
}
}
else
{
if (j == 0)
{
cell.CssClass = "xl30";
}
else if (j == dataSource.Tables[0].Columns.Count - 1)
{
cell.CssClass = "xl35";
}
else
{
cell.CssClass = "xl31";
}
}
row.Cells.Add(cell);
}
this.Table2.Rows.Add(row);
}
}
else
{
this.lblResult.Text = "◆集計結果: 0件";
}
Application.Remove(str + "DownLoadExcel");
Application.Remove(str + "SearchDic");
Application.Remove(str + "TemplateFilePath");
}解决方案:
往html的header里追加<meta http-equiv="Content-Type" content="text/html;charset=SHIFT_JIS">就没有乱码了
但是为什么8列单元格出现乱码,六列单元格就没有问题,百思不得其解。请知者告诉原委,谢谢!
相关文章推荐
- response.write 输出乱码的解决方法
- response.binarywrite输出图片乱码问题
- 不生成Excel文件,将Datatable数据 Response.write 输出生成Excel (转载)
- 不生成Excel文件,将Datatable数据 Response.write 输出生成Excel (转载)
- flex 通过as3xls包来实现datagrid输出保存到excel。并处理中文乱码
- 通过Response输出方式导出Excel
- 通过Response输出方式导出Excel
- Servlet中直接response.getWriter().write() 输出乱码解决!
- 通过response的write留输出数据的问题
- 用jsp动态输出excel文档和中文乱码问题的解决
- weblogic容器通过JSP页面输出图片出现乱码Tomcat下却正常
- 通过response输出认证图片
- 【Servlet】Response的OutputStream与Writer输出数据乱码的问题
- Python中通过csv的writerow输出多余空行+py2.x &py3.兼容
- Response.Write("Sucess!!")不能输出数据的解决方法
- JSP动态输出Excel及中文乱码的解决
- response.getWriter().write("中文");乱码问题
- 为什么用Response.Write("dbcd")输出文本后,查看源码,显示出来的bdcd字符串在html代码的外面?— —服务器输出的缓存区
- response通过writer输出数据
- Asp.net中的页面乱码的问题和Asp.net页面输出到EXCEL