首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从HSSFWorkbook对象获取输入流

如何从HSSFWorkbook对象获取输入流
EN

Stack Overflow用户
提问于 2008-12-18 19:15:01
回答 4查看 29.3K关注 0票数 12

我希望我的web应用程序用户以Excel文件的形式下载一些数据。

我有下一个函数在response对象中发送一个输入流。

代码语言:javascript
复制
public static void sendFile(InputStream is, HttpServletResponse response) throws IOException {
        BufferedInputStream in = null;
        try {
            int count;
            byte[] buffer = new byte[BUFFER_SIZE];
            in = new BufferedInputStream(is);
            ServletOutputStream out = response.getOutputStream();
            while(-1 != (count = in.read(buffer)))
                out.write(buffer, 0, count);
            out.flush();            
        }   catch (IOException ioe) { 
            System.err.println("IOException in Download::sendFile"); 
            ioe.printStackTrace();
        } finally {
            if (in != null) {
                try { in.close(); 
                } catch (IOException ioe) { ioe.printStackTrace(); }
            }   
        }
    }

我想将我的HSSFWorkbook对象转换为输入流,并将其传递给前面的方法。

代码语言:javascript
复制
public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate the excel object
    return null; // TODO. return the wb as InputStream 
}

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2008-12-18 21:08:41

你问题的问题是你把OutputStreams和InputStreams混在一起了。InputStream是您读取的内容,而OutputStream是您写入的内容。

这就是我将POI对象写入输出流的方式。

代码语言:javascript
复制
// this part is important to let the browser know what you're sending
response.setContentType("application/vnd.ms-excel");
// the next two lines make the report a downloadable file;
// leave this out if you want IE to show the file in the browser window
String fileName = "Blah_Report.xls";
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 

// get the workbook from wherever
HSSFWorkbook wb = getWorkbook();
OutputStream out = response.getOutputStream();
try {
   wb.write(out);
}       
catch (IOException ioe) { 
  // if this happens there is probably no way to report the error to the user
  if (!response.isCommited()) {
    response.setContentType("text/html");
    // show response text now
  }
}

如果您希望重用现有代码,则必须将POI数据存储在某个位置,然后将其转换为输入流。只需将其写入ByteArrayOutputStream,然后使用ByteArrayInputStream读取这些字节,就可以很容易地做到这一点,但我不建议这样做。您现有的方法作为通用管道实现会更有用,您可以通过管道将数据从InputStream传递到OutputStream,但不需要它来编写POI对象。

票数 10
EN

Stack Overflow用户

发布于 2018-03-16 15:12:43

您可以从对象创建InputStream。

代码语言:javascript
复制
public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate a InputStream from the excel object
    return new ByteArrayInputStream(excelFile.getBytes());
}
票数 2
EN

Stack Overflow用户

发布于 2020-01-20 19:18:21

我的解决方案是首先将HSSFWorkbook传输到ByteArrayOutputStream,然后从ByteArrayOutputStream创建一个InputStream:

代码语言:javascript
复制
        HSSFWorkbook wb = ...

        // Fill an empty output stream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        wb.write(baos);

        // Close the document
        wb.close();

        // Create the input stream (do not forget to close the inputStream after use)
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/378883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档