A good memory is better than a bad pen. 7-java Generating Excel files

Source: Internet
Author: User

Use POI Build EXCEL

in many cases, it is necessary to use EXCEL file. It is generally a tabular application, a simple example.

need to introduce a third party Jar Package: Poi_3.6.jar

Package com.daily;

Import Java.io.File;

Import Java.io.FileOutputStream;

Import Java.io.OutputStream;

Import java.util.ArrayList;

Import java.util.List;

Importorg.apache.poi.hssf.usermodel.HSSFCell;

importorg.apache.poi.hssf.usermodel.HSSFRichTextString;

Importorg.apache.poi.hssf.usermodel.HSSFRow;

Import Org.apache.poi.hssf.usermodel.HSSFSheet;

Importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

/**

 * Build Excel

* @author Fan Fangming

*/

public class Easyexcel {

public void Createexcelfile (String outfilename,list<content> xls) throws Exception {

       // get total number of columns

int countcolumnnum = Xls.size ();

       // Create an Excel document

Hssfworkbook HWB = new Hssfworkbook ();

Content content = NULL;

//sheet corresponds to a work page

Hssfsheet sheet = hwb.createsheet ("FFM");

Hssfrow firstrow = sheet.createrow (0); Start with a row labeled 0

hssfcell[] FirstCell = new Hssfcell[countcolumnnum];

string[] names = new String[countcolumnnum];

names[0] = " code ";

names[1] = "  name ";

for (int j = 0; J < Countcolumnnum; J + +) {

Firstcell[j] =firstrow.createcell (j);

Firstcell[j].setcellvalue (newhssfrichtextstring (names[j));

}

for (int i = 0; i < xls.size (); i++) {

            // Create a row

Hssfrow row = Sheet.createrow (i + 1);

            // get each record that you want to insert

Content = Xls.get (i);

for (int colu = 0; Colu <= 4;colu++) {

                // loop within a line

Hssfcell one =row.createcell (0);

One.setcellvalue (Content.getcode ());

Hssfcell =row.createcell (1);

Two.setcellvalue (Content.getname ());

}

}

       // Create a file output stream, prepare the output spreadsheet

OutputStream out = new FileOutputStream (new File (Outfilename));

Hwb.write (out);

Out.close ();

}

publicstatic void Main (string[] args) throws exception{

        // Initialize the data, the structure of the inner class is a little special

list<content>contents = new arraylist<content> ();

Contents.add (Neweasyexcel (). New Content ("", "name100"));

Contents.add (Neweasyexcel (). New Content ("101", "name101"));

Contents.add (Neweasyexcel (). New Content ("102", "name102"));

Longstart = System.currenttimemillis ();

Easyexcelexcel = new Easyexcel ();

Excel.createexcelfile ("D:/ffm83/easyexcel.xls", contents);

Longend = System.currenttimemillis ();

System.out.println (" generate Excel file time:" + (End-start) + "(ms)");

}

    // a simple internal class for saving simple information

Publicclass Content {

Privatestring Code;

private String name;

Public Content (String code,string name) {

This.code= Code;

This.name= name;

}

Publicstring GetCode () {

ReturnCode;

}

Publicvoid Setcode (String code) {

This.code= Code;

}

Publicstring GetName () {

Returnname;

}

Publicvoid setName (String name) {

This.name= name;

}

}

}

A good memory is better than a bad pen. 7-java Generating Excel files

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.