我作成一个Html页面(使用aspx:Table控件生成表格),然后把这个页面以Excel格式发送到客户端,打开它,数据多时,里面是乱码,数据少时时正确的。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownLoadExcel.aspx.cs"
Inherits="RealEstate.Page.Common.DownLoadExcel" EnableTheming="false" Theme="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=SHIFT_JIS">
<style>
.style0
{
number-format: General;
text-align: general;
vertical-align: middle;
white-space: nowrap;
rotate: 0;
background-source: auto;
pattern: auto;
color: windowtext;
font-size: 11.0pt;
font-weight: 400;
font-style: normal;
text-decoration: none;
font-family: "MS Pゴシック" , monospace;
font-charset: 128;
border: none;
protection: locked visible;
style-name: 標準;
style-id: 0;
}
.xl25
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: 1.0pt solid black;
border-bottom: 1.0pt solid black;
border-left: none;
white-space: normal;
}
.xl26
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl27
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl28
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: .5pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl29
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: .5pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl30
{
style-parent: style0;
border-top: .5pt solid black;
border-right: .5pt solid black;
border-bottom: .5pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl31
{
style-parent: style0;
border: .5pt solid black;
white-space: normal;
}
.xl32
{
style-parent: style0;
border-top: .5pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl33
{
style-parent: style0;
border-top: .5pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl34
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: 1.0pt solid black;
border-bottom: .5pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl35
{
style-parent: style0;
border-top: .5pt solid black;
border-right: 1.0pt solid black;
border-bottom: .5pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl36
{
style-parent: style0;
border-top: .5pt solid black;
border-right: 1.0pt solid black;
border-bottom: 1.0pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 500px;">
<tr>
<td colspan="3">
<asp:Table ID="Table1" runat="server">
<asp:TableRow runat="server">
<asp:TableCell runat="server" ColumnSpan="3">◆実行年月日</asp:TableCell>
<asp:TableCell runat="server" ColumnSpan="4"><%=DateTime.Now.ToString("yyyy年MM月dd日 HH時mm分")%></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell runat="server" ColumnSpan="3">◆集計条件</asp:TableCell>
</asp:TableRow>
</asp:Table>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Table ID="Table2" runat="server">
</asp:Table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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列单元格出现乱码,六列单元格就没有问题,百思不得其解。请知者告诉原委,谢谢!
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownLoadExcel.aspx.cs"
Inherits="RealEstate.Page.Common.DownLoadExcel" EnableTheming="false" Theme="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=SHIFT_JIS">
<style>
.style0
{
number-format: General;
text-align: general;
vertical-align: middle;
white-space: nowrap;
rotate: 0;
background-source: auto;
pattern: auto;
color: windowtext;
font-size: 11.0pt;
font-weight: 400;
font-style: normal;
text-decoration: none;
font-family: "MS Pゴシック" , monospace;
font-charset: 128;
border: none;
protection: locked visible;
style-name: 標準;
style-id: 0;
}
.xl25
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: 1.0pt solid black;
border-bottom: 1.0pt solid black;
border-left: none;
white-space: normal;
}
.xl26
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl27
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl28
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: .5pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl29
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: .5pt solid black;
border-bottom: .5pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl30
{
style-parent: style0;
border-top: .5pt solid black;
border-right: .5pt solid black;
border-bottom: .5pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl31
{
style-parent: style0;
border: .5pt solid black;
white-space: normal;
}
.xl32
{
style-parent: style0;
border-top: .5pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: 1.0pt solid black;
white-space: normal;
}
.xl33
{
style-parent: style0;
border-top: .5pt solid black;
border-right: .5pt solid black;
border-bottom: 1.0pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl34
{
style-parent: style0;
border-top: 1.0pt solid black;
border-right: 1.0pt solid black;
border-bottom: .5pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl35
{
style-parent: style0;
border-top: .5pt solid black;
border-right: 1.0pt solid black;
border-bottom: .5pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
.xl36
{
style-parent: style0;
border-top: .5pt solid black;
border-right: 1.0pt solid black;
border-bottom: 1.0pt solid black;
border-left: .5pt solid black;
white-space: normal;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 500px;">
<tr>
<td colspan="3">
<asp:Table ID="Table1" runat="server">
<asp:TableRow runat="server">
<asp:TableCell runat="server" ColumnSpan="3">◆実行年月日</asp:TableCell>
<asp:TableCell runat="server" ColumnSpan="4"><%=DateTime.Now.ToString("yyyy年MM月dd日 HH時mm分")%></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell runat="server" ColumnSpan="3">◆集計条件</asp:TableCell>
</asp:TableRow>
</asp:Table>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Table ID="Table2" runat="server">
</asp:Table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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列单元格出现乱码,六列单元格就没有问题,百思不得其解。请知者告诉原委,谢谢!