首页 新闻 赞助 找找看

使用NPIO操作excel2007出现的问题??求解

0
悬赏园豆:10 [已解决问题] 解决于 2013-11-28 15:52
浏览器上输入excel2007文件,就是在浏览器可以保存excel2007文件,我把IWorkbook写到内存里,之后总是提示流已经关闭了。不能在输出了.
问题补充:

string strPath=@"F:\a.xlsx";

XSSFWorkbook xssfworkbook = new XSSFWorkbook(strPath);

//加了数据之后

MemoryStream ms = new MemoryStream();

xssfworkbook.write(ms);//写到内存流

再对ms操作的时候,提示ms已经关闭了,输出不到浏览器中

fulai_xy的主页 fulai_xy | 初学一级 | 园豆:5
提问于:2013-10-04 16:52
< >
分享
最佳答案
0

1、贴代码;

2、NPOI对07的支持比较弱。

收获园豆:10
幻天芒 | 高人七级 |园豆:37175 | 2013-10-05 13:49

string strPath=@"F:\a.xlsx";

XSSFWorkbook xssfworkbook = new XSSFWorkbook(strPath);

//加了数据之后

MemoryStream ms = new MemoryStream();

xssfworkbook.write(ms);//写到内存流

再对ms操作的时候,提示ms已经关闭了,输出不到浏览器中

 

 

fulai_xy | 园豆:5 (初学一级) | 2013-10-06 00:02

@fulai_xy: 换个方式下载可否?Response.Write(filePath)

幻天芒 | 园豆:37175 (高人七级) | 2013-10-06 20:42

@幻天芒: 可以啊 ,能写主要的代码吗??

fulai_xy | 园豆:5 (初学一级) | 2013-10-07 09:54

@fulai_xy: Response.WriteFile(filePath)就是主要代码。

FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();
幻天芒 | 园豆:37175 (高人七级) | 2013-10-07 14:56

@幻天芒: 哦,这样也可以吧

我是这样的

  MemoryStream ms = new MemoryStream();
  workbook.Write(ms);//调用这个后会关于文件流,在HSSFWorkbook不会关闭所以在处理时应注意

   Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("22.xlsx", Encoding.UTF8).ToString());
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            //Response.Write(ms.ToString());
            Response.BinaryWrite(ms.ToArray());

不过有个问题 ,打开以保存的excel文件,总是会提示是否恢复此工作的内容

 

fulai_xy | 园豆:5 (初学一级) | 2013-10-07 18:52

@fulai_xy: 试试去掉编码选项...还有ContentType,这两个会影响。

幻天芒 | 园豆:37175 (高人七级) | 2013-10-07 22:54
其他回答(1)
0

你应该把文件输出的那段代码贴出来

hello*boy | 园豆:310 (菜鸟二级) | 2013-10-04 20:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册