Back up data in tabular form
(1) Import the POI jar package
Put under Lib: webroot\web-inf\lib\Poi-3.2-final-20081019.jar
(2) StringBuffer converted to a two-dimensional array
Define a stringbuffer to \ r \ n A one-dimensional array with \ t divided into two-dimensional arrays
StringBuffer Strff = new StringBuffer ("name \ t age \ t sex \ r \ n Small fairy \t18\t female \ r \ n");
convert StringBuffer data to one-dimensional arrays: differentiate object[by Row Records ] Dataline = strff.tostring (). Split ("\ r \ n"); //New for(int i=0; i<dataline.length; i++// The row data is assigned to each column of a one-dimensional array according to \ t split data[i] = dataline[ I].tostring (). Split ("\ t "
(3) Use example one: (Easy to understand)
The data is output in tabular form, and the data is stored in a 123.xls file under the F drive .
PackageTestpoi;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; Public classExportexcel { Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {hssfworkbook WB=NewHssfworkbook (); Hssfsheet sheet= Wb.createsheet ("table");//Create a table workbookObject[][] Datas = {{"area", "total sales (million)", "total profit (million) Simple Table"}, {"Jiangsu province", 9045, 2256}, {"Jiangsu province", 9045, 2256}, {"Guangdong province", 3000, 690}}; Hssfrow Row; Hssfcell cell; for(inti = 0; i < datas.length; i++) {row= Sheet.createrow (i);//Create a table row for(intj = 0; J < Datas[i].length; J + +) {cell= Row.createcell (j);//create cells based on table rowsCell.setcellvalue (string.valueof (datas[i][j)); }} wb.write (NewFileOutputStream ("F:/123.xls")); System.out.println ("Hello poi"); }}
(4) Use example two:
Note: Here the data content Strff inside the data format similar to this:111\t222\t333\t444 \r\n555 \t666 \t777 \t888 r\n
Convert to Excel data:111 222 333 444
555 666 777 888
/*** Convert data of txt type to XLS required data type *@paramData contents in the Strff file *@paramFILENAME1: File name *@paramFilePath: File path *@return*@throwsApplicationException *@throwsFileNotFoundException *@throwsIOException*/PublicHssfworkbook txtdata_to_xlsdata (stringbuffer content,string filename,string FilePath)ThrowsApplicationException {Try{//Logger.info ("Enter XLS data Backup"); Hssfworkbook WB =NewHssfworkbook (); Hssfsheet sheet = wb.createsheet ("table");//Create a table workbookHssfrow Row; //Defining Rows in table tablesHssfcell Cell; //Defining Columns in table tables//Convert StringBuffer data to one-dimensional arrays: differentiate by Row Recordsobject[] Dataline = content.tostring (). Split ("\ r \ n");//Defines a two-dimensional array that converts data of the StringBuffer type to a two-dimensional arrayobject[][] data = NewObject[dataline.length][];Forint i=0; i<dataline.length; i++) {//The row data is assigned to each column of a one-dimensional array according to the \ t split data[i] = Dataline[i].tostring (). Split ("\ T"); } logger.info ("Start XLS data Backup");Forint i = 0; i < data.length; i++) {row = Sheet.createrow (i);//Create a table rowForint j = 0; J < Data[i].length; J + +) {cell = Row.createcell (j);//Create cells based on table rowsCell.setcellvalue (string.valueof (data[i][j)); }}//Logger.info ("Start XLS data backup, write to File"); Wb.write (New FileOutputStream (filepath+FileName)); Logger.info ("Start XLS data backup, write end"); return WB;} Catch(FileNotFoundException e) {logger.error (new StringBuilder ("Backup Data exception: file does not exist"). Append (E). ToString ()); throw New applicationexception (E.getmessage ());} Catch (IOException e) {logger.error (new StringBuilder ("Backup Data exception: IOException"). Append (E). ToString ()); throw New applicationexception (E.getmessage ());}}
Java uses the POI plugin to back up data as Excel