POI Export Excel

Source: Internet
Author: User

First, Introduction

HSSF is the implementation of the POI project's pure Java EXCEL97 (-2007) file format. XSSF is a Java implementation of the POI project's Excel OOXML (. xlsx) file format. HSSF and XSSF provides several ways to read spreadsheets to create, modify, read, and write XLS spreadsheets.

It provides the following features:

    • Low-level structure for special needs
    • An efficient read-only access event model API
    • An API that provides a full user model for creating, reading, and modifying XLS files.

The project URL is as follows: Http://poi.apache.org/spreadsheet/index.html.

Second, examples

1. Related dependencies are as follows:

<dependency>    <groupId>org.apache.poi</groupId>    <artifactid>poi</artifactid >    <version>3.12</version></dependency><dependency>    <groupId> org.apache.poi</groupid>    <artifactId>poi-ooxml</artifactId>    <version>3.12 </version></dependency>

2. The sample code is as follows:

Package Com.test.excel;import Java.io.fileoutputstream;import java.util.arraylist;import java.util.List;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.Sheet ; Import Org.apache.poi.xssf.streaming.SXSSFWorkbook;/** * *excle export*/ Public classExcelcreate {/** * Excel generated*/     Public Static voidMain (string[] args) throws Throwable {//Table HeaderList<string> headerlist =NewArraylist<> (Ten);  for(inti =1; I <=Ten; i++) {Headerlist.add ("Table Header"+i); }        //DataList<string[]> DataRowList2 =NewArraylist<string[]> (Ten);  for(inti =1; I <= headerlist.size (); i++) {Datarowlist2.add (NewString[] {"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+ I,"Data"+i}); } Sxssfworkbook Workbook=NewSxssfworkbook ( -); Sheet Sheet= Workbook.createsheet ("Export"); intRowNum =0; //Table HeaderRow row = Sheet.createrow (rownum++);  for(inti =0; I < headerlist.size (); i++) {cell cell=Row.createcell (i); Cell.setcellvalue (headerlist.Get(i)); }        //Data         for(string[] array:datarowlist2) {row= Sheet.createrow (rownum++); String[] Arraya=(string[]) array;  for(inti =0; I < datarowlist2.size (); i++) {cell cell=Row.createcell (i);            Cell.setcellvalue (Arraya[i]); }} fileoutputstream os=NewFileOutputStream ("c:/test/testexcel.xlsx"); //The following code is available for file download//Response.reset ();//Response.setcontenttype ("Application/octet-stream; Charset=utf-8 ");//Response.setheader ("content-disposition", "attachment; Filename= "+encodes.urlencode (" testexcel.xlsx "));//Write (Response.getoutputstream ());workbook.write (OS);    Workbook.dispose (); }}

3. The result of operation is as follows:

POI Export Excel

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.