Java uses POI to export data to Excel

Source: Internet
Author: User

Background:

The previous article on the use of JTDS connection database to obtain the corresponding data, this article how to use POI to the data everywhere in Excel, this program is application

Text:Third party POI jar package: Poi driver package DownloadCode Snippet:
/** * Export Data to Excel * @param data will be imported into Excel * @throws ioexception */public void Cratetempfile (Arraylist<linkedhash map<string, string>> data) throws IOException {//Create an Excel workbook Hssfworkbook wb = new Hssfworkbook ();// Establish a sheethssfsheet sheet = wb.createsheet ("test");//Create a cell style hssfcellstyle Commontitilestyle = Wb.createcellstyle () ;//Create a font style Hssffont font=wb.createfont ();//Set Font color Font.setcolor (HSSFColor.RED.index);//hssfcolor.violet.index// Font color//Set Font size font.setfontheightinpoints ((short) 12);//font Bold font.setboldweight (hssffont.boldweight_bold);// Put the font style in the cell style commontitilestyle.setfont (font);//Create a line Hssfrow Titlerow = sheet.createrow (0); map<string, string> titlemap = data.get (0); set<string> KeySet = Titlemap.keyset ();iterator<string> it = Keyset.iterator ();//sheet row index value int IndexOfRow = 0;//establishes the first row of the first cell Hssfcell Titilecell = Titlerow.createcell (Indexofrow); String titilkey = "test test (6072)";//Set cell width sheet.setcolumnwidth (indexofrow, 20 * 256);//Assign value to cell Titilecell.setcellvalUE (TITILKEY);//Set cell style Titilecell.setcellstyle (commontitilestyle); indexofrow = 1;//set sheet second row Hssfrow DataRow = Sheet.createrow (Indexofrow); while (It.hasnext ()) {//traversal establishes a second row of cell formats Hssfcell cell1 = Datarow.createcell (indexOfRow-1); String key1 = It.next ();//Change the field name in the database table to the required title name in Excel key1 = Dbcolumnnametoexcelcolumnname (Key1); Sheet.setcolumnwidth (Indexofrow), Cell1.setcellvalue (key1); indexofrow++;} Set the third line Indexofrow = 2;for (map<string, string> temp:data) {Hssfrow TitleRow1 = Sheet.createrow (Indexofrow); set<string> set = Temp.keyset ();iterator<string> it1 = Set.iterator (); int indexofcolumn = 0;while ( It1.hasnext ()) {String key = It1.next (); if (Key.equals ("Anchor_name") | | key.equals ("send_gift_user_name") | | Key.equals ("Add_date")) {String value = Temp.get (key), if (Key.equals ("Add_date")) {Date d = new Date (long.valueof (value) ); SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day HH:MM:SS"); value = Sdf.format (d);} Hssfcell cell = Titlerow1.createcell (indexofcolumn); Cell.setcellvalue (value);} else {Long value = long.valueof (Temp.get (key)); Hssfcell cell = Titlerow1.createcell (Indexofcolumn); Cell.setcellvalue (value);} indexofcolumn++;} indexofrow++;} FileOutputStream fos = new FileOutputStream (New File ("d://test//" + New Random (). Nextint (+) + ". xls"); Wb.write (FOS); Fos.close ();}
Effect:Create an Excel file in D:\test with the following styles:Experience:mainly use POI to export data to Excel. The detailed steps are:1. Create an Excel workbook before creating a sheet, 2. Set the value of each row in the established sheet for each column. detailed styling issues to view the API documentation provided by the POI

Java uses POI to export data to Excel

Related Article

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.