【问题标题】:How can I get an Input Stream from HSSFWorkbook Object如何从 HSSFWorkbook 对象获取输入流
【发布时间】:2010-09-27 14:11:51
【问题描述】:

我希望我的 Web 应用程序用户将一些数据下载为 Excel 文件。

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

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 对象转换为输入流并将其传递给上一个方法。

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

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    我想我理解你想要做的事情(不过,也许我做得不够好)

    你真的不需要那么多代码 - 查看 write 方法 -

    HSSFWorkbook wb = new HSSFWorkBook();
    //populate
    
    ServletOutputStream out = response.getOutputStream();
    try {
       wb.write(out);
       out.flush();
    }       
    catch (IOException ioe) { 
       //whatever
    }
    out.close();
    

    据我所知,当我使用 POI 工作时,我就是这样做的。如果您在 Web 框架内,您可能必须对其进行调整,以便框架在您关闭它后不会尝试对 ServletOutputStream 执行某些操作。如果它尝试,你会得到一个异常抛出,告诉你输出流已经关闭。

    【讨论】:

      【解决方案2】:

      您的问题是您正在混合 OutputStreams 和 InputStreams。 InputStream 是您读取的内容,OutputStream 是您写入的内容。

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

      // 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 对象。

      【讨论】:

      • +1 例如。但是我怎样才能得到 HSSFWorkbook 的文件大小呢?我想添加一个标题Content-Length
      • @MyTitle 我认为如果您想设置内容长度标头,您需要先将流写入 ByteArrayOutputStream,然后获取其长度,然后将其写入 HTTP 响应。跨度>
      【解决方案3】:

      您可以从对象创建 InputStream。

      public InputStream generateApplicationsExcel() {
          HSSFWorkbook wb = new HSSFWorkbook();
          // Populate a InputStream from the excel object
          return new ByteArrayInputStream(excelFile.getBytes());
      }
      

      【讨论】:

        【解决方案4】:

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

                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());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多