Baidu is the simplest instance of the entire network, Java will write information to Excel file instances just understand this on it. To save you energy.
Turn from: http://www.cnblogs.com/chenyq/p/5530970.html
Package excel.write;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.util.ArrayList;
Import java.util.List;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import Org.apache.poi.hssf.util.HSSFColor;
Import Org.apache.poi.ss.usermodel.Cell;
Import Org.apache.poi.ss.usermodel.CellStyle;
Import Org.apache.poi.ss.usermodel.Font;
Import Org.apache.poi.ss.usermodel.Row;
Import Org.apache.poi.ss.usermodel.Sheet;
Import Org.apache.poi.ss.usermodel.Workbook;
Import org.apache.poi.ss.util.CellRangeAddress;
Import Org.apache.poi.xssf.usermodel.XSSFWorkbook;
/** * reading data from Excel/writing Excel to Excel with table headers, contents of header per column corresponding to entity class properties * * @author NAGSH */public class Excelmanage {
public static void Main (string[] args) throws IOException {String path = "D:/test";
String fileName = "List of insured persons (ADD) 04";
String FileType = "xlsx"; list<insuraceexcelbean> list = new arraylist<> ();
for (int i=0; i<6; i++) {Insuraceexcelbean bean = new Insuraceexcelbean ();
Bean.setinsuraceuser ("test" +i);
Bean.setbankcardid ("4444444444" +i+ "," + "55544444444444" +i+ "," + "999999999999999" +i);
Bean.setidcard ("666666" +i);
Bean.setbuytime ("2016-05-06");
Bean.setinsendtime ("2016-05-07");
Bean.setinsstarttime ("2017-05-06");
Bean.setmoney ("20,000");
Bean.settype ("card");
List.add (Bean);
} String title[] = {"Insured name", "Identity card number", "Account Type", "bank card number", "Insurance amount (yuan)", "Purchase Time", "policy Effective Time", "policy Expiration Time"};
Createexcel (add) to the list of e:/insured persons. xlsx ", Sheet1", filetype,title);
Writer (path, fileName, filetype,list,title); @SuppressWarnings ("resource") public static void writer (string path, string filename,string filetype,list <InsuraceExcelBean> list,string titlerow[]) throws IOException {workbook WB = null;
String Excelpath = path+file.separator+filename+ "."
+filetype;
File File = new file (Excelpath);
Sheet Sheet =null; Create a working Document object if (!file.exists ()) {if (Filetype.equals ("xls")) {wb = new Hssfworkboo
K ();
else if (filetype.equals ("xlsx")) {wb = new Xssfworkbook ();
else {throw new IOException ("Incorrect file format");
}//Create sheet object sheet = (sheet) wb.createsheet ("Sheet1");
OutputStream outputstream = new FileOutputStream (Excelpath);
Wb.write (OutputStream);
Outputstream.flush ();
Outputstream.close ();
else {if (filetype.equals ("xls")) {wb = new Hssfworkbook ();
else if (filetype.equals ("xlsx")) {wb = new Xssfworkbook (); else {throw new IOException ("Incorrect file format");
}//Create sheet object if (sheet==null) {sheet = (sheet) wb.createsheet ("Sheet1");
//Add table header row row = Sheet.createrow (0);
Cell cell = Row.createcell (0);
Row.setheight ((short) 540); Cell.setcellvalue ("List of insured persons"); Create the first line cellstyle style = Wb.createcellstyle ();
Style object//Set cell background color is light blue style.setfillforegroundcolor (HSSFColor.PALE_BLUE.index); Style.setverticalalignment (Cellstyle.vertical_center)//Vertical style.setalignment (cellstyle.align_center); /horizontal Style.setwraptext (TRUE);//Specifies that when the contents of the cell are not displayed, the line wraps Cell.setcellstyle (style);
Style, center font font = Wb.createfont ();
Font.setboldweight (Font.boldweight_bold);
Font.setfontname ("Song Body");
Font.setfontheight ((short) 280); StYle.setfont (font);
Cell merge//Four parameters are: Start row, start column, end row, end column sheet.addmergedregion (new cellrangeaddress (0, 0, 0, 7));
Sheet.autosizecolumn (5200); row = Sheet.createrow (1);
Create a second line for (int i = 0;i < titlerow.length;i++) {cell = Row.createcell (i);
Cell.setcellvalue (Titlerow[i]); Cell.setcellstyle (style);
Style, centered sheet.setcolumnwidth (i, 20 * 256);
} row.setheight ((short) 540);
Loops write row data for (int i = 0; i < list.size (); i++) {row = (row) sheet.createrow (i+2);
Row.setheight ((short) 500);
Row.createcell (0). Setcellvalue (List.get (i)). Getinsuraceuser ());
Row.createcell (1). Setcellvalue (List.get (i)). Getidcard ());
Row.createcell (2). Setcellvalue (List.get (i)). GetType ());
Row.createcell (3). Setcellvalue (List.get (i)). Getbankcardid ()); Row.createcell (4). Setcellvalue (List.get (i)). Getmoney ());
Row.createcell (5). Setcellvalue (List.get (i)). Getbuytime ());
Row.createcell (6). Setcellvalue (List.get (i)). Getinsstarttime ());
Row.createcell (7). Setcellvalue (List.get (i)). Getinsendtime ());
//Create file stream OutputStream stream = new FileOutputStream (Excelpath);
Writes data wb.write (stream);
Closes the file stream stream.close ();
}
}
Effect Chart: