gridview导出excel2007,中文乱码

导出excel的类文件
using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Text;

/// <summary>
/// Excel文件处理类
/// </summary>
internal class TExcelHelper : IExport
{
Page m_page;
public event dlg_BindData OnBindData;

/// <summary>
/// 构造器
/// </summary>
public TExcelHelper( Page page )
{
this.m_page = page;
}

#region IExport 成员

/// <summary>
/// 名 称:导出文件
/// 功 能:将GridView表格中的数据导出到Excel中
/// 作 者:Hasen
/// 创建时间:2007-11-20
/// 最近修改:2007-11-20
/// 使用说明:
/// </summary>
/// <param name="srcGridView">原 GridView 网格控件(从客户程序传入该引用)</param>
/// <param name="FileName">默认导出文件名称</param>
public void Export( GridView srcGridView , string FileName )
{
m_page.Response.ClearContent();
m_page.Response.Charset = "gb2312";
m_page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
m_page.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls\"");
m_page.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad);
HtmlTextWriter hw = new HtmlTextWriter(tw);
srcGridView.AllowPaging = false;
if (OnBindData != null)
OnBindData();
srcGridView.RenderControl(hw);
m_page.Response.Write(tw.ToString());
m_page.Response.End();
}
}


#endregion

...全文
589 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangyuelei01 2009-09-16
  • 打赏
  • 举报
回复
m_page.Response.Charset = "gb2312";
m_page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
查询编码为GB2312,可内容编码又以UTF-8,不明白?
chaozi_249 2009-09-16
  • 打赏
  • 举报
回复
试试吧,主要是编码问题,都试下。
zzxap 2009-09-15
  • 打赏
  • 举报
回复
[code=C#]
把dataset数据保存到excel
public void CreateExcel(DataSet ds, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据 
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据  
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
resp.Write(ls_item);
ls_item = "";
}
resp.End();
}

[/CODE]
红街咖啡 2009-09-15
  • 打赏
  • 举报
回复
我最后用的是UTF-7导出成功过的。
红街咖啡 2009-09-15
  • 打赏
  • 举报
回复
我最后用的是UTF-7导出成功过的。
红街咖啡 2009-09-15
  • 打赏
  • 举报
回复
我也是和你一样的情况。我晕。我导出2003没有问题。2007有问题。。GridView里面不能存在文本框CheckBox等控件
还有你可以设置GBK GB2312 UTF-7 UTF-8都试验一下
wuxing2006 2009-08-28
  • 打赏
  • 举报
回复
gb2312
  • 打赏
  • 举报
回复
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:if(this.bgColor=='#c4d2ea'){this.bgColor='white'}else{this.bgColor='#c4d2ea';}");
//excel导出,文本化
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("class", "text");
}
}


不知道为什么导出的EXCEL总是乱码,但又不是所有的页面都乱,只有这一个窗体有问题。
本来以为是动态绑定列的问题,但是其他的绑定列却有好的,不知道为什么,高手回答,分有的是,就看大家能不能帮上忙了。
  • 打赏
  • 举报
回复
protected void imgBtnExcel_Click(object sender, ImageClickEventArgs e)
{
IExport iexport = TFileHelperFactory.CreateExcelHelper(Page);
if (iexport != null)
{
iexport.OnBindData += new dlg_BindData(InitPage);

iexport.Export(gvList, "生产信息统计");
DataAccess.SystemLog.InsertLogInfo(LoggingType.Select, "生产信息统计", "Excel导出生产信息", "");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StatProductionList.aspx.cs" Inherits="ProductionManagement_StatProductionList" EnableEventValidation="false" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!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>
<title>产量信息统计</title>

</head>
<body>
<form id="form1" runat="server">
<link href="../skins/<%=style%>" type="text/css" rel="stylesheet" />
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table id="Table1" class="td" width="100%">
<tr>
<td style="height: 20px" colspan="3">
<span style="color: blue">
<asp:HyperLink ID="HyperLink1" runat="server" Font-Underline="True" ForeColor="Blue" NavigateUrl="~/ProductionManagement/ProductionManagementMain.aspx" Text="<%$ Resources:ProductionManage, ProductionManagement %>"></asp:HyperLink>--<asp:HyperLink ID="HyperLink2" runat="server" Font-Underline="True" ForeColor="Blue" NavigateUrl="~/ProductionManagement/PlanAnalyseMain.aspx" Text="<%$ Resources:ProductionManage, PlanAnalyse %>"></asp:HyperLink>--<asp:Label ID="Label1" runat="server" Text="<%$ Resources:ProductionManage, ProductionInformationStat %>"></asp:Label>--<asp:Label ID="Label" runat="server"></asp:Label></span></td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label></td>
<td style="width:10%" align="left">
<asp:ImageButton ID="imgBtnExcel" runat="server" ImageUrl="<%$ Resources:ProductionManage, ToExcel %>" OnClick="imgBtnExcel_Click" /></td>
<td style="width:10%" align="left">
<asp:ImageButton ID="imgBtnReturn" runat="server" ImageUrl="<%$ Resources:Public, ReturnButton %>" OnClick="imgBtnReturn_Click" /></td>
</tr>
</table>
<table id="Table2" class="td" width="100%">
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvList" runat="server" AllowSorting="True" Width="98%" OnRowDataBound="gvList_RowDataBound" OnSorting="gvList_Sorting" CssClass="GridBackColor" ShowFooter="True" OnPageIndexChanged="gvList_PageIndexChanged" OnPageIndexChanging="gvList_PageIndexChanging">
<HeaderStyle CssClass="GridHeadBackColor" />
<FooterStyle CssClass="GridHeadBackColor" HorizontalAlign="Center" VerticalAlign="Middle"/>
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel> </tr>
</table>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvcollect" runat="server" AllowSorting="True" CssClass="GridBackColor" ShowFooter="True" OnRowDataBound="gvcollect_RowDataBound" OnSorting="gvcollect_Sorting" Visible="False" Width="40%" >
<HeaderStyle CssClass="GridHeadBackColor" HorizontalAlign="Center" VerticalAlign="Middle" />
<FooterStyle CssClass="GridHeadBackColor" HorizontalAlign="Center" VerticalAlign="Middle" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
  • 打赏
  • 举报
回复
为什么没有人回答啊

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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