有的页用这代码不乱码,一样的代码在下面的面就乱码,求高手。
public void ExpExcle( GridView gridView)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gridView.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End(); }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="YfckMxList.aspx.cs" Inherits="yf_YfckMxList" %><!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>
<link href="../Css/Global.css" rel="stylesheet" type="text/css" />
<link href="../Css/DataGrid.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../Js/Prototype.js"></script>
<script type="text/javascript" src="../Js/Functions.js"></script>
</head>
<body>
<form id="form1" runat="server">
<table cellpadding="2" cellspacing="1" id="Control">
<tr>
<td class="Header">>>药房出库明细
<asp:TextBox ID="txtId" runat="server" Visible=false></asp:TextBox>
<asp:TextBox ID="txtLb" runat="server" Visible=false></asp:TextBox>
<asp:TextBox ID="txtId2" runat="server" Visible=false></asp:TextBox>
<td class="Header">
<input id="Button2" type="button" value="查询" class="XPButton" onclick="openSearchWin('YFCKMX');" />
</td>
<td class="Header"> <asp:Button id="ButtonExcle" runat="server" Text="导出" class="XPButton" OnClick="ButtonExcle_Click"></asp:Button></td>
</tr>
</table>
<div id=MainDiv>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" CssClass="DataGrid"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" PageSize="20" Width="100%" Height="100%">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ypmc" HeaderText="品名" SortExpression="ypmc" />
<asp:BoundField DataField="ypgg" HeaderText="规格" SortExpression="ypgg" />
<asp:BoundField DataField="BHYP" HeaderText="项目编号" SortExpression="BHYP" />
<asp:BoundField DataField="LBYP" HeaderText="项目类别" SortExpression="LBYP" />
<asp:BoundField DataField="DWRK" HeaderText="出库单位" SortExpression="DWRK" />
<asp:BoundField DataField="JGRK" HeaderText="出库价格" SortExpression="JGRK" />
<asp:BoundField DataField="SLRK" HeaderText="出库数量" SortExpression="SLRK" />
<asp:BoundField DataField="JERK" HeaderText="出库金额" SortExpression="JERK" />
<asp:BoundField DataField="HSL" HeaderText="换算率" SortExpression="HSL" />
<asp:BoundField DataField="RQRK" HeaderText="出库时间" SortExpression="RQRK" />
<asp:BoundField DataField="CZY" HeaderText="操作员" SortExpression="CZY" />
<asp:BoundField DataField="LBRK" HeaderText="出库分类" SortExpression="LBRK" />
<asp:BoundField DataField="DKCCZ" HeaderText="出库编号" SortExpression="DKCCZ" HeaderStyle-Width="20"/>
<asp:BoundField DataField="IDKCMX" HeaderText="出库明细编号" SortExpression="IDKCMX" />
<asp:BoundField DataField="XH" HeaderText="序号" SortExpression="XH" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:yyConnectionString %>"
SelectCommand="SELECT a.*,y.mc as ypmc,y.gg as ypgg FROM [V_YFRKMX] a,yp y where a.bhyp=y.bh and a.lbyp=y.lb ">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<script language="javascript">
//打开查询窗体
function openSearchWin(lb)
{
var ss ="../publicFiles/Search.aspx?lb="+lb ;
openWindowA(ss);
}
//打开查询窗体后查询q
function reLoadPage(url)
{
var surl='../yf/YfckMxList.aspx?sql=' + url ;
window.location.href=surl ;
}
</script>
public void ExpExcle( GridView gridView)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gridView.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End(); }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="YfckMxList.aspx.cs" Inherits="yf_YfckMxList" %><!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>
<link href="../Css/Global.css" rel="stylesheet" type="text/css" />
<link href="../Css/DataGrid.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../Js/Prototype.js"></script>
<script type="text/javascript" src="../Js/Functions.js"></script>
</head>
<body>
<form id="form1" runat="server">
<table cellpadding="2" cellspacing="1" id="Control">
<tr>
<td class="Header">>>药房出库明细
<asp:TextBox ID="txtId" runat="server" Visible=false></asp:TextBox>
<asp:TextBox ID="txtLb" runat="server" Visible=false></asp:TextBox>
<asp:TextBox ID="txtId2" runat="server" Visible=false></asp:TextBox>
<td class="Header">
<input id="Button2" type="button" value="查询" class="XPButton" onclick="openSearchWin('YFCKMX');" />
</td>
<td class="Header"> <asp:Button id="ButtonExcle" runat="server" Text="导出" class="XPButton" OnClick="ButtonExcle_Click"></asp:Button></td>
</tr>
</table>
<div id=MainDiv>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" CssClass="DataGrid"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" PageSize="20" Width="100%" Height="100%">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ypmc" HeaderText="品名" SortExpression="ypmc" />
<asp:BoundField DataField="ypgg" HeaderText="规格" SortExpression="ypgg" />
<asp:BoundField DataField="BHYP" HeaderText="项目编号" SortExpression="BHYP" />
<asp:BoundField DataField="LBYP" HeaderText="项目类别" SortExpression="LBYP" />
<asp:BoundField DataField="DWRK" HeaderText="出库单位" SortExpression="DWRK" />
<asp:BoundField DataField="JGRK" HeaderText="出库价格" SortExpression="JGRK" />
<asp:BoundField DataField="SLRK" HeaderText="出库数量" SortExpression="SLRK" />
<asp:BoundField DataField="JERK" HeaderText="出库金额" SortExpression="JERK" />
<asp:BoundField DataField="HSL" HeaderText="换算率" SortExpression="HSL" />
<asp:BoundField DataField="RQRK" HeaderText="出库时间" SortExpression="RQRK" />
<asp:BoundField DataField="CZY" HeaderText="操作员" SortExpression="CZY" />
<asp:BoundField DataField="LBRK" HeaderText="出库分类" SortExpression="LBRK" />
<asp:BoundField DataField="DKCCZ" HeaderText="出库编号" SortExpression="DKCCZ" HeaderStyle-Width="20"/>
<asp:BoundField DataField="IDKCMX" HeaderText="出库明细编号" SortExpression="IDKCMX" />
<asp:BoundField DataField="XH" HeaderText="序号" SortExpression="XH" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:yyConnectionString %>"
SelectCommand="SELECT a.*,y.mc as ypmc,y.gg as ypgg FROM [V_YFRKMX] a,yp y where a.bhyp=y.bh and a.lbyp=y.lb ">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
<script language="javascript">
//打开查询窗体
function openSearchWin(lb)
{
var ss ="../publicFiles/Search.aspx?lb="+lb ;
openWindowA(ss);
}
//打开查询窗体后查询q
function reLoadPage(url)
{
var surl='../yf/YfckMxList.aspx?sql=' + url ;
window.location.href=surl ;
}
</script>
Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
Response.Write("</body></html>");
Response.ContentEncoding = System.Text.Encoding.UTF7;这样可以保证Excel 2003以下版本可以打开.2007不可以.
再在你的web.config上加上
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>