您的位置:首页 > 其它

通过Response.write输出Excel时,乱码

2009-08-30 15:55 330 查看
我作成一个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列单元格出现乱码,六列单元格就没有问题,百思不得其解。请知者告诉原委,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: