Java background Excel file is converted to byte stream after generation

Source: Internet
Author: User

After the Java background Excel file is generated into a byte stream, Java Excel uses the POI component, Hssfworkbook WorkBook = new Hssfworkbook (); For workbook to generate a byte stream, it is easy to find a workbook. GetBytes (), however, is not available, the download will not open, if downloaded, the correct notation for the Workbook.write (response.getoutputstream ()); If converted to a byte stream: Bytearrayoutputstream os = new Bytearrayoutputstream ();Try{workbook.write (OS); ByteArray bytes = Os.tobytearray (); String FileName ="Xxx.xls";                Response.reset (); Response.setcontenttype ("Application/msexcel;charset=utf-8"); Response.setheader ("Content-disposition","Attachment;filename="+ FileName);               Response.getoutputstream (). Write (Bytes.getrawbytes ());               Response.getoutputstream (). Flush ();           Response.getoutputstream (). Close ();          } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); The code is validated and feasible. Java Excel uses the POI component, Hssfworkbook WorkBook = new Hssfworkbook (), and for WorkBook to generate a byte stream, it is easy to see that there is a workBook. GetBytes (), however, is not available, The download will not open, if downloaded, the correct wording for Workbook.write (Response.getoutputstream ()); If you convert to a byte stream: Bytearrayoutputstream OS = new Bytearrayoutputstream ();Try{workbook.write (OS); ByteArray bytes = Os.tobytearray (); String FileName ="Xxx.xls"; Rundata.getresponse (). reset (); Rundata.getresponse (). setContentType ("Application/msexcel;charset=utf-8"); Rundata.getresponse (). SetHeader ("Content-disposition","Attachment;filename="+ fileName); Rundata.getresponse (). Getoutputstream (). Write (Bytes.getrawbytes ()); Rundata.getresponse (). Getoutputstream (). Flush (); Rundata.getresponse (). Getoutputstream (). Close (); catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} The wording of DWR: bytearrayoutputstream os = new Bytearrayoutputstream ();Try{hssfworkbook.write (OS);returnNew Filetransfer (New String ("Xxxx.xls". GetBytes (),"Iso8859-1"),"Application/ms-excel", Os.tobytearray ());  } catch (IOException e) {e.printstacktrace (); Here, we use the Bytearrayoutputstream and Bytearrayinputstream classes...The idea of processing is to write Hssfworkbook to Bytearrayoutputstream. Then use Bytearrayoutputstream to convert to a byte stream. Then convert the byte stream to Bytearrayinputstream. At this point, we are converting Excel into an input stream in memory. The code is as follows Bytearrayoutputstream OS = new Bytearrayoutputstream ();Try{wb.write (OS);  } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();  } byte[] B = Os.tobytearray (); Bytearrayinputstreaminch= new Bytearrayinputstream (b); All the code is too much to stick up. I believe everyone is willing to use this method. Because of the use of the previously mentioned method of generating a random file name. More tedious...Summing up first, we have to make clear what we want to achieve. We've already completed the Excel build using SHHF, and now it's the job to convert this excel into an input stream for struts2 for download...That means we're going to convert into an input stream .... After we have identified the goals, we will begin to design...Our idea is to create an output stream, write this Excel into the output stream, and then go through the output stream to get the input stream we need....Here we try two ways. The first is the use of temporary files. The output stream we created first writes the content to the temporary file, and then we read the input stream from the temporary file. This is the way we use FileOutputStream. and FileInputStream. But the problem with temporary files makes the program very complicated, so our goal is to complete the conversion of Excel to the input stream in memory. So we used the bytearrayinputstream. and Bytearrayoutputstream. It's really that simple. Finally, attach the input stream method from the Excel template to get the Excel file you want to export. Java Code Public InputStream Getexcelisforas (list<object> List) throws Exception {InputStream Excelstrea            m = null; FileInputStream fis = new FileInputStream (TemplatePath +"/template.xls");            Hssfworkbook wb = new Hssfworkbook (new Poifsfilesystem (FIS));            Fis.close ();            Generateexcelforas (list, WB);//Fill Excel content method Bytearrayoutputstream out = new Bytearrayoutputstream ();            Hssfworkbook HSSWB = (hssfworkbook) WB;          Hsswb.write (out);          Excelstream = new Bytearrayinputstream (Out.tobytearray ()); Out.close ();returnExcelstream; }

Java background Excel file is converted to byte stream after generation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.