Entity class Excel
Importjava.util.List;/*** Function: * Description: *@author* @date 2015-3-19 pm 5:15:48*/ Public classExcel {PrivateString FileName;//name of the sheet PrivateString[] handers;//the title in sheet Privatelist<string[]> DataSet;//data sets in the sheet /** * */ PublicExcel (String filename,string[] handers,list<string[]>dataset) { This. FileName =FileName; This. handers =handers; This. DataSet =DataSet; } PublicString GetFileName () {returnFileName; } Public voidsetfilename (String fileName) { This. FileName =FileName; } Publicstring[] Gethanders () {returnhanders; } Public voidsethanders (string[] handers) { This. handers =handers; } PublicList<string[]>GetDataSet () {returnDataSet; } Public voidSetdataset (list<string[]>dataset) { This. DataSet =DataSet; } }
Specific implementation
ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;/*** Function: Export multiple sheet to the same out of Excel table * Description: *@author* @date 2015-3-19 pm 5:20:20*/ Public classTest { Public Static voidMain (string[] args) {String data1[]= {"111", "2222", "3333", "44444"}; String data2[]= {"111", "2222", "3333", "44444", "5555"}; String handers1[]= {"One", "two", "three", "four", "five"}; String handers2[]= {"One", "one", "three", "four", "five"}; String handers3[]= {"Vehicle", "sweater", "Cell phone", "teacup", "notebook"}; List<String[]> DataSet =NewArraylist<string[]>(); Dataset.add (DATA1); Dataset.add (DATA2); Excel E1=NewExcel ("First sheet", Handers1, DataSet); Excel E2=NewExcel ("Second sheet", Handers2, DataSet); Excel E3=NewExcel ("Third sheet", Handers3, DataSet); List<Excel> MySheet =NewArraylist<excel>(); Mysheet.add (E1); Mysheet.add (E2); Mysheet.add (E3); Test2 (MySheet); } Private Static voidTest () {List<Object> list =NewArraylist<object>(); String[] Handers= {"1", "2", "3", "4", "5"}; Hssfworkbook WB=NewHssfworkbook ();//Create a workbookHssfsheet sheet = wb.createsheet ("first sheet");//The first of the sheetHssfrow Rowfirst = sheet.createrow (0);//First Act title of first sheet//Write a title for(inti=0;i){ //get each cell of the first rowHssfcell cell =Rowfirst.createcell (i); //write data into cellsCell.setcellvalue (Handers[i]); } //Write data sets//Suppose data Set Poetry list collection for(intI=0;i<list.size (); i++){ //get list inside exists is DataSet objectObject obj =List.get (i); //To create a data rowHssfrow row = Sheet.createrow (i+1); //set the value of the corresponding cellRow.createcell (0). Setcellvalue ("Property 0" of "obj"); Row.createcell (1). Setcellvalue ("Property 1" of "obj")); Row.createcell (2). Setcellvalue ("Property 2" of "obj")); Row.createcell (3). Setcellvalue ("Property 3" of "obj")); Row.createcell (4). Setcellvalue ("Property 4" of "obj")); } //Write a file Try{outputstream OS=NewFileOutputStream (NewFile ("filepath")); Wb.write (OS); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /*** Specific sheet operation *@author* @date 2015-3-19 pm 6:12:57 *@parammysheets void*/ Private Static voidTest2 (list<excel>mysheets) {Hssfworkbook WB=NewHssfworkbook ();//Create a workbooklist<excel> sheets =mysheets; for(Excel excel:sheets) {//Create a new sheetHssfsheet sheet = wb.createsheet (Excel.getfilename ());//Gets the sheet namestring[] Handers= Excel.gethanders ();//gets the title name of the sheetHssfrow Rowfirst = sheet.createrow (0);//First Act title of first sheet//Write a title for(inti=0;i){ //get each cell of the first rowHssfcell cell =Rowfirst.createcell (i); //write data into cellsCell.setcellvalue (Handers[i]); } //Write data setslist<string[]> DataSet =Excel.getdataset (); for(intI=0;i<dataset.size (); i++) {string[] data= Dataset.get (i);//gets the object//To create a data rowHssfrow row = Sheet.createrow (i+1); for(intj=0;j<data.length;j++){ //set the value of the corresponding cellRow.createcell (j). Setcellvalue (Data[i]); } } } //Write a file Try{outputstream OS=NewFileOutputStream (NewFile ("D://test.xls")); Wb.write (OS); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } }
Import multiple sheet into the same Excel file