日期:2014-05-18  浏览次数:20419 次

[100分]导出Excel丢失css文件的问题
提的问题总是少有人回复,也在前面加个[100分],呵呵。

导出Excel时,如果页面使用了css文件,或者使用了skin,在打开excel后,数据显示之前,总是出个对话框,提示丢失文件。列的那几个文件都是css文件。
不知道大家是否遇到过这个问题?如何解决?

导出是在服务器端先生成文件,然后传到客户端。代码如下:

///   <summary>
///   导出文件
///   </summary>
///   <param   name= "p "> 页面引用 </param>
///   <param   name= "Res "> </param>
///   <param   name= "FileName "> 文件名 </param>
///   <param   name= "t "> 表格引用 </param>
///   <param   name= "strType "> 文档类别:excel,   word,   txt,   html </param>
static   public   void   ExportTo(Page   p,   HttpResponse   Res,   string   FileName,   Table   t,   string   strType)
{
//   文档类型,字符编码
Res.Clear();
Res.Buffer   =   true;
Res.Charset   =   "utf-8 ";

//   文件名称
Res.AppendHeader( "Content-Disposition ",   string.Format( "attachment;filename={0} ",   FileName));
Res.ContentEncoding   =   System.Text.Encoding.GetEncoding( "utf-8 ");

//   文档类型
Res.ContentType   =   string.Format( "application/ms-{0} ",   strType);;
p.EnableViewState   =   false;

//   输入流
System.IO.StringWriter   ioWriter   =   new   System.IO.StringWriter();
HtmlTextWriter   htmlWriter   =   new   HtmlTextWriter(ioWriter);

//   目标绑定到输入流输出
t.RenderControl(htmlWriter);
Res.Write(ioWriter.ToString());
Res.End();
}

------解决方案--------------------
红星的问题没权力回答,只有顶的份了
------解决方案--------------------
红星的问题没权力回答,只有顶的份了 JF
------解决方案--------------------
LZ,您说的不明不白,是在讨论还是在自己问自己。???
------解决方案--------------------
此类的应该直接用style= " "来设置样式,因为你用外部的CSS你没有导出
------解决方案--------------------
.css 没有导出来
------解决方案--------------------
写法没有错,我也是这样写的:

Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8 ";

Response.AppendHeader( "content-disposition ", "attachment;filename= " + excelName);
Response.ContentEncoding = System.Text.Encoding.GetEncoding( "utf-8 ");
Response.ContentType = "application/ms-excel ";
this.EnableViewState = false;

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);

rptMaster.RenderControl(hw);

Response.Write(sw.ToString);
Response.End();

不知道搂主的p、Res是怎么定义的?
------解决方案--------------------
另外,我也是引用的外部CSS文件:
<LINK href= "../CSS/Main.css " type= "text/css " rel= "stylesheet ">
------解决方案--------------------
导入代码没错~

LZ 是否在导出后提示用户 导入成功这样的提示框弹出`

如果是在弹出提示后 页面部分样式丢失的话

那先查看弹出页面后 右键查看源代码 看看文本内最顶端是否出现 alert( '提示信息 ')
这样的脚本代码~

如果有 解决方案: 要在提示框提示用户前 就加载完样式 可以在body任意位置放个lable或者其他显示控件text属性 如lable.text= " <script> alert( '提示信息! ') </script> ";