我怎样才能从HSSFWorkbook对象时的输入流 [英] How can I get an Input Stream from HSSFWorkbook Object

查看:1301
本文介绍了我怎样才能从HSSFWorkbook对象时的输入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的web应用程序用户下载一些数据作为一个Excel文件。

I want my web application users to download some data as an Excel file.

我有一个功能在响应对象发送的输入流。

I have the next function to send an Input Stream in the response object.

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对象时转换为输入流,并把它传递给previous方法。

I would like to transform my HSSFWorkbook Object to an input stream and pass it to the previous method.

public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate the excel object
    return null; // TODO. return the wb as InputStream 
}

<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html\">http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html

推荐答案

你的问题的问题是,你是混合OutputStreams和InputStreams。一个InputStream是你读的东西和一个OutputStream是你写的东西。

The problem with your question is that you are mixing OutputStreams and InputStreams. An InputStream is something you read from and an OutputStream is something you write to.

这是我写的一个POI对象到输出流。

This is how I write a POI object to the output stream.

// 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
  }
}

如果你想重新使用现有的code你必须在某处储存设施数据,然后把它转换成输入流。这会通过写入到一个ByteArrayOutputStream,然后用ByteArrayInputStream进行读取这些字节很容易做到的,但我不会推荐它。您现有的方法是作为一种通用的管道实施方案,在那里你可以管从InputStream来和OutputStream数据,但你并不需要用写的POI对象更加有用。

If you wanted to re-use your existing code you'd have to store the POI data somewhere then turn THAT into an input stream. That'd be easily done by writing it to a ByteArrayOutputStream, then reading those bytes using a ByteArrayInputStream, but I wouldn't recommend it. Your existing method would be more useful as a generic Pipe implementation, where you can pipe the data from an InputStream to and OutputStream, but you don't need it for writing POI objects.

这篇关于我怎样才能从HSSFWorkbook对象时的输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆