excel出力 乱码问题

yeyuhongchch 2011-11-15 06:40:53
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gridview_download.aspx.cs" Inherits="Gridview_download" %>

<!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" >
<meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8"/>
<head runat="server">
<title>文件download之Excel出力</title>
<script type="text/javascript" src='<%=ResolveUrl("~/js/lib/jquery.js") %>'></script>

<script type="text/javascript" language="javascript">
// イメージを変更する
function changeImage(id)
{
if(document.getElementById(id).getAttribute("src",2) == "../Resource/1.png")
{
document.getElementById(id).src = "../Resource/0.png";
$('#'+id + " ~ input").val("0");
}
else
{
document.getElementById(id).src = "../Resource/1.png";
$('#'+id + " ~ input").val("1");
}
}
function setHeader()
{
var t = document.getElementById("<%=GridView1.ClientID%>");
var t2 = t.cloneNode(true);
for(i = t2.rows.length -1;i > 0;i--){
t2.deleteRow(i);
}
t.deleteRow(0);
t2.style.width="377px";
t2.rows[0].cells[2].style.width="137px";
document.getElementById("div_head").appendChild(t2);
}

</script>

</head>
<body>
<form id="form1" runat="server">
<div style="height: 310px; width: 400; position: relative; left: 0px; top: 0px;">
    
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="一览下载" /><br />
<br />
<div id="div_head" style="width: 377;">
</div>
<div id="div_info" style="overflow-y: scroll; height: 180px; width: 381px;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="360px"
BackColor="White" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px"
CellPadding="0" ForeColor="Black" GridLines="Vertical">
<Columns>
<asp:BoundField DataField="NAME" HeaderText="姓名">
<HeaderStyle HorizontalAlign="Center" Height="40px" Width="120px" />
<ItemStyle HorizontalAlign="Center" Height="35px" Width="120px" />
</asp:BoundField>
<asp:BoundField HeaderText="团队编号" DataField="TEAM">
<HeaderStyle HorizontalAlign="Center" Height="40px" Width="120px" />
<ItemStyle HorizontalAlign="Center" Height="35px" Width="120px" />
</asp:BoundField>
<asp:TemplateField>
<HeaderTemplate>
 所属团队
</HeaderTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"TEAM").ToString() == "1"?"~/Resource/1.png":"~/Resource/0.png" %>'
onclick="changeImage(this.id);" Style="cursor: pointer; border-bottom: solid 1px black;
padding-bottom: 2px;" />
<asp:HiddenField ID="HiddenField1" runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="40px" Width="120px" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="35px" Width="120px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="LightGray" Height="40px" Font-Bold="True" ForeColor="Black" />
<RowStyle BackColor="#CDE4EA" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</div>
</form>
</body>
</html>
...全文
124 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoqiangsass 2012-03-20
  • 打赏
  • 举报
回复
试问LZ + LS ,若日语系统中,出力中文版的excel 怎么办?
我现在的情况是,里面的内容都正常,就是在IE浏览器下出力的excel名字里面的 文字为乱码.
有什么好办法解决么?
幕夜 2011-11-18
  • 打赏
  • 举报
回复
加多一句看下!
幕夜 2011-11-18
  • 打赏
  • 举报
回复
StringBuilder sb = new StringBuilder();
sb.Append("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
StringWriter sw = new StringWriter(sb);
yeyuhongchch 2011-11-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lian_2011 的回复:]

大哥,这哪里是乱码啊,是你把编码弄成日本的了,纳闷
[/Quote]本来就是日文编码,要输入的就是日文文档~
yeyuhongchch 2011-11-16
  • 打赏
  • 举报
回复
有兴趣的可以帮忙试试这个程序,完整代码,只是一试~~~
lian_2011 2011-11-16
  • 打赏
  • 举报
回复
大哥,这哪里是乱码啊,是你把编码弄成日本的了,纳闷
yeyuhongchch 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 qgqch2008 的回复:]

Response.Charset = "Shift_JIS";
试试呢,不一定对啊。。
[/Quote]
我这个是从一个大工程中截取的一段方法,工程中就好使,这里就莫名其妙了~
qgqch2008 2011-11-15
  • 打赏
  • 举报
回复
Response.Charset = "Shift_JIS";
试试呢,不一定对啊。。
yeyuhongchch 2011-11-15
  • 打赏
  • 举报
回复
没有人遇到过这个问题吗?
yeyuhongchch 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 thinkingforever 的回复:]

utf-8改为gb2312呢
[/Quote]日文环境,改那个更错了~
yeyuhongchch 2011-11-15
  • 打赏
  • 举报
回复
上面是完整的例子,只是没有图片文件,我想实现excel文件下载,功能基本实现,但是下载后是乱码。我这里用的都是日文环境,里面也没有汉字了,但仍然都是乱码,不知什么原因,希望各位能帮忙看看~~~
thinkingforever 2011-11-15
  • 打赏
  • 举报
回复
utf-8改为gb2312呢
yeyuhongchch 2011-11-15
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

public partial class Gridview_download : System.Web.UI.Page
{
private DataTable CreateDataSource()
{

DataTable dt = new DataTable();
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("CODE", typeof(string));
dt.Columns.Add("TEAM", typeof(string));
DataRow row = dt.NewRow();
row["NAME"] = "王一";
row["CODE"] = "001";
row["TEAM"] = "1";
dt.Rows.Add(row);

row = dt.NewRow();
row["NAME"] = "王五";
row["CODE"] = "002";
row["TEAM"] = "0";
dt.Rows.Add(row);

row = dt.NewRow();
row["NAME"] = "王帆";
row["CODE"] = "003";
row["TEAM"] = "1";
dt.Rows.Add(row);

row = dt.NewRow();
row["NAME"] = "李儲";
row["CODE"] = "004";
row["TEAM"] = "0";
dt.Rows.Add(row);

row = dt.NewRow();
row["NAME"] = "徐恋";
row["CODE"] = "005";
row["TEAM"] = "1";
dt.Rows.Add(row);
GridView1.DataSource = dt;
Session["dtCopy"] = dt;
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
GridView1.Attributes.Add("style", "table-layout:fixed");
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
ClientScript.RegisterStartupScript(this.GetType(), "message", "<script language='javascript'>setHeader();</script>");
}
}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
Page page = new Page();
HtmlForm form = new HtmlForm();
GridView1.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
// Sessionからデータを取得する
DataTable dtGridview = (DataTable)Session.Contents["dtCopy"];

//データセットを初期化する
DataSet ds = new DataSet();
//データテーブルを初期化する
DataTable dt = new DataTable();
//データテーブルのカラムを設定する
dt.Columns.Add("名前");

dt.Columns.Add("TEAMCODE");
dt.Columns.Add("TEAMMARK");

//データテーブルの値を設定する
for (int i = 0; i < dtGridview.Rows.Count; i++)
{
dt.Rows.InsertAt(dt.NewRow(), i);
DataRow dr = dt.Rows[i];

dr["名前"] = dtGridview.Rows[i][0];
dr["TEAMCODE"] = dtGridview.Rows[i][2];

//デジタルが1の場合○を設定する
if ( Convert .ToInt32(dtGridview.Rows[i][2]) == 1)
{
dr["TEAMMARK"] = "○";
}
//デジタルが1の場合×を設定する
else
{
dr["TEAMMARK"] = "×";
}

}
GridView gv = new GridView();
ds.Tables.Add(dt);
gv.DataSource = ds;
gv.DataBind();

//ヘッドの顔色を設定する
for (int j = 0; j < 3; j++)
{
gv.HeaderRow.Cells[j].Attributes.Add("style", "background-color:#93CCDD");
}
//セルの書式を設定する
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
gv.Rows[i].Cells[0].Attributes.Add("style", "text-align:center");
gv.Rows[i].Cells[1].Attributes.Add("style", "text-align:center");
gv.Rows[i].Cells[2].Attributes.Add("style", "text-align:center");

}

DateTime DtTime = DateTime.Now;
//システム時間を取得する
string strTime = string.Format("{0:yyyyMMddHHmmss}", DtTime);

form.Controls.Add(gv);
page.RenderControl(htw);
Response.Clear();
Response.Buffer = true;
string strFileName = "文档一覧" + strTime + ".xls";
string strEncodefileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename = " + strEncodefileName);
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.HeaderEncoding = System.Text.Encoding.UTF8;
Response.Write(sb.ToString());
Response.End();
}
catch
{
}
finally
{
}
}
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧