如题:只要Excel文档名称超过二三十个字节或含特殊字符(比如:逗号,顿号之类的),直接点击打开就报名称过长或DDE错误,但保存就没有问题.("团省两级业务网络联调测试日志(甘肃)1128.xls"这个名称直接点击打开文档就会报错,但是点击保存就没问题.)
a.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %><!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" SkinID="verSkin"
OnRowCommand="GridView1_RowCommand" DataKeyNames="Auto_DocID" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<img src="../image/news_icon.gif" width="8" height="8" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="文档名称" ShowHeader="False">
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<a href="../news/tDoc.aspx?fileName=<%#Eval("DocShowName")%>&filepath=<%#Eval("DocPath") %>"
target="mainFrame">
<%# Eval("DocShowName") %>
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DocSize" HeaderText="大小" ShowHeader="False" SortExpression="docSize">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="realName" HeaderText="上传人" ShowHeader="False" SortExpression="userName">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SubmitTime" HeaderText="上传时间" ShowHeader="False" SortExpression="Res">
<ItemStyle HorizontalAlign="Left" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="weekDoc" HeaderText="周报栏目" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/image/sh.gif" CommandName="edit1"
CommandArgument='<%#Eval("Auto_DocID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Del" ImageUrl="~/image/sc.gif"
CommandArgument='<%#Eval("Auto_DocID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td></tr>
</table>
</div>
</form>
</body>
</html>b.aspx.cs
private void ExportDataGrid(string filepath, string filename)
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.WriteFile(filepath);
Response.End();
}
a.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %><!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" SkinID="verSkin"
OnRowCommand="GridView1_RowCommand" DataKeyNames="Auto_DocID" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<img src="../image/news_icon.gif" width="8" height="8" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="文档名称" ShowHeader="False">
<ItemStyle HorizontalAlign="Left" />
<ItemTemplate>
<a href="../news/tDoc.aspx?fileName=<%#Eval("DocShowName")%>&filepath=<%#Eval("DocPath") %>"
target="mainFrame">
<%# Eval("DocShowName") %>
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DocSize" HeaderText="大小" ShowHeader="False" SortExpression="docSize">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="realName" HeaderText="上传人" ShowHeader="False" SortExpression="userName">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SubmitTime" HeaderText="上传时间" ShowHeader="False" SortExpression="Res">
<ItemStyle HorizontalAlign="Left" Width="130px" />
</asp:BoundField>
<asp:BoundField DataField="weekDoc" HeaderText="周报栏目" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/image/sh.gif" CommandName="edit1"
CommandArgument='<%#Eval("Auto_DocID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CommandName="Del" ImageUrl="~/image/sc.gif"
CommandArgument='<%#Eval("Auto_DocID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td></tr>
</table>
</div>
</form>
</body>
</html>b.aspx.cs
private void ExportDataGrid(string filepath, string filename)
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.WriteFile(filepath);
Response.End();
}
解决方案 »
- 动态产生input控件,只输出一个值的怪现象
- PopupLayer怎么实现拖动效果
- window.open的问题
- 关于字符串转换成uniqueidentifier值的问题
- 关于新弹出页面的问题。
- asp.net如何做像新浪微博那视频效果?
- 这异常怎么回事:访问被拒绝:“System.EnterpriseServices.Thunk.dll”。
- 调用用户控件时。能不能给此用户控件里的某个子控件Attributes.Add("onclick","javascript:OnSel();")?
- 不知道这个问题有没有解决方法
- MasterPage.masterTAB选项卡的效果
- vs2005网站根目录中没有 BIN 文件夹,可我想用那个啥 freetxtbox啊
- gridview 编辑
( string filename , System.Web.UI.WebControls.DataGrid ToExcelGrid )
{
Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";
Response.AppendHeader ("Content-Disposition","attachment;filename="+Server.UrlEncode ( filename ) );
Response.ContentEncoding=System.Text.Encoding.Default;//设置输出流为简体中文
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);
ToExcelGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
但还是有些不明白.. System.Web.UI.WebControls.DataGrid ToExcelGrid 这个参数的用处是??我问题没说清哦... 现在问题是: 把已有的 .xls文件上传后..另一个页面把这些文件名放到一个GridView里面,通过向指定的页面传两个参数(文件名,路径)打开相应的.xls文件..
如果文件名不包含中文与非法字符,就没有问题.. 但一有这些字符后,就有些不正常了... 我想可能是通过Get方法传值后,编码上出了问题..
还有可能就是在 Response.AppendHeader 这里.. 谢谢各们大大们帮帮忙... 分不够我再开贴...