Java POI Import Export Excel__java

Source: Internet
Author: User
A certain version of the POI associated jar package used must be the same ...

The jar pack that Maven uses

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

private void Exportexcel (List orderlist) {
if (orderlist = null | | orderlist.size () = 0) {
return;
}
try {
String[] Header = {"Order number", "Transaction type", "Unit Price", "Transaction Quantity", "Transaction Amount", "Transaction Time"};
Sxssfworkbook Sxssfworkbook = Createsxssfworkbook (orderlist, header);
//write output
FileOutputStream out = new FileOutputStream ("d://Order statistics. xlsx");
Sxssfworkbook.write (out);
Out.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

Private Sxssfworkbook Createsxssfworkbook (list<usdtordermodel> dataList, string[] header) {Sxssfworkboo 
    K Sxssfworkbook = new Sxssfworkbook (500);
    Sheet Sheet = null;
    row row = null;
    int max_row = 500;
    int count = 0; if (dataList!= null &&!datalist.isempty ()) {for (Usdtordermodel model:datalist) {if (cou NT% Max_row = 0) {sheet = Sxssfworkbook.createsheet ("Transaction Details _" + (Count/max_row)
                );
                Create a new row and add several cells.
                row = Sheet.createrow (0); Create a cell, set the cell's value for (int i = 0; i < header.length; i++) {Row.createcell (i). setc
                Ellvalue (Header[i]);
            } row = Sheet.createrow (count% max_row + 1);
            Row.createcell (0). Setcellvalue (Model.getorderid ());
            Row.createcell (1). Setcellvalue (Model.getordertype ()); Row.createcell (2). Setcellvalue (Model.gettransactionprice ());
            Row.createcell (3). Setcellvalue (Model.getdealcount ());
            Row.createcell (4). Setcellvalue (Model.gettransactiontotalamount ());
            Row.createcell (5). Setcellvalue (Model.gettransactiontime ());
        count++;
        } else {//Create a default header row if Excel cannot open sheet = Sxssfworkbook.createsheet ("Transaction Details") if it is not created when the list is empty
        row = Sheet.createrow (0);
        for (int i = 0; i < header.length i++) {Row.createcell (i) setcellvalue (header[i));
} return Sxssfworkbook;
 }

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.