Java poi processes multiple sheets in excel and performs sorting.
Requirement: there is a full data dictionary summary table, where the first sheet is the directory, including the number and table name, and the second is the table details later. The sheet name is the table name but unordered. The field numbers in the sheet are unordered and empty rows.
Now, we need to extract the format columns, field names, and table names from the 101,104,107,111 tables to generate batch statements. The output must be sorted by the given number, and the field output must be sorted and narrow.
The output result is as follows:
Insert into t_export (export_id, owner, table_name, col_name, format) values (101, 't_ A', 'Scott ', 'id', '123 ');
Insert into t_export (export_id, owner, table_name, col_name, format) values (101, 't_ A', 'Scott ', 'sal', '123 ');
Insert into t_export (export_id, owner, table_name, col_name, format) values (101, 't_ A', 'Scott ', '20', 'zhang san ');
Insert into t_export (export_id, owner, table_name, col_name, format) values (101, 't_ A', 'Scott ', 'remark', 'Hello ');
Insert into t_export (export_id, owner, table_name, col_name, format) values (101, 't_ A', 'Scott ', 'birthday', '123 ');
Insert into t_export (export_id, owner, table_name, col_name, format) values (101, 't_ A', 'Scott ', 'age', '20 ');
.
.
.
To 111
Excel is as follows:
Download poi package poi-3.17.jar and introduce eclipse java project, only need RowInfo. java, ShowExcel. java
RowInfo. java:
Package pu; public class RowInfo implements Comparable <RowInfo> {// sort private int rownumb; private int expId; private String tableName; private String columnName; private String formatInfo; public RowInfo (int rownumb, int expId, String tableName, String columnName, String formatInfo) {super (); this. rownumb = rownumb; this. expId = expId; this. tableName = tableName; this. columnName = columnName; this. formatInfo = formatInfo;} @ Override public String toString () {return "insert into t_export (export_id, owner, table_name, col_name, format) values ('" + this. expId + "', 'Scott', '" + this. tableName + "','" + this. columnName + "','" + this. formatInfo + "');" ;}@ Override public int compareTo (RowInfo row) {// rewrite the sorting method // TODO Auto-generated method stub return this. rownumb-row. rownumb;} public int getExpId () {return expId;} public void setExpId (int expId) {this. expId = expId;} public String getTableName () {return tableName;} public void setTableName (String tableName) {this. tableName = tableName;} public String getFormatInfo () {return formatInfo;} public void setFormatInfo (String formatInfo) {this. formatInfo = formatInfo;} public int getRownumb () {return rownumb;} public void setRownumb (int rownumb) {this. rownumb = rownumb;} public String getColumnName () {return columnName;} public void setColumnName (String columnName) {this. columnName = columnName ;}}
ShowExcel. java
Package pu; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. printStream; import java. util. arrayList; import java. util. collections; import java. util. hashMap; import java. util. map; import java. util. treeMap; import org. apache. poi. hssf. usermodel. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. HSSFWorkbook; import org. apache. poi. ss. usermodel. cell; import org. apache. poi. ss. usermodel. row; public class ShowExcel {public static void showExcelSheet (Map <Integer, ArrayList> map) throws Exception {// processing method HashMap hashmap = new HashMap ();
// Add the required table name hashmap. put ("T_A", "101"); hashmap. put ("T_D", "104"); hashmap. put ("T_G", "107 ");
Hashmap. put ("T_K", "111"); HSSFWorkbook workbook = new HSSFWorkbook (new FileInputStream (new File ("E: \ data \ Dictionary.xls "))); HSSFSheet sheet = null; FileOutputStream fs = new FileOutputStream (new File ("E: \ output \ temp.txt"), false ); // overwrite PrintStream p = new PrintStream (fs); p. println ("sheet amount =" + workbook. getNumberOfSheets (); for (int I = 0; I <workbook. getNumberOfSheets (); I ++) {// obtain each Sheet Table sheet = workbook. getSheetAt (I); ArrayList <RowInfo> list_rows = new ArrayList <> (); // a new list if (hashmap. containsKey (sheet. getSheetName () {p. println ("reading the sheet" + sheet. getSheetName (); for (int j = 0; j <sheet. getPhysicalNumberOfRows (); j ++) {// get each row HSSFRow row = sheet. getRow (j); if (row = null) {// skip the empty row System. out. println ("wowowo"); continue;} System. out. print ("row amount =" + sheet. GetPhysicalNumberOfRows () + "and now row" + j + "\ t"); // p. print ("table_name _" + sheet. getSheetName () + "\ t"); if (j> 2 & row. getCell (1 )! = Null) {// skip the first three rows of p. print ("table_name _" + sheet. getSheetName () + "row =" + j + "= \ t"); p. print (hashmap. get (sheet. getSheetName ()). toString () + "\ t"); p. print (sheet. getSheetName () + "\ t"); p. print (row. getCell (1 ). toString (). toUpperCase () + "\ t"); p. print (row. getCell (5) + "\ t"); p. println ();
// Load the serial number of each row, field name, format: list_rows.add (new RowInfo (int) row. getCell (0 ). getNumericCellValue (), Integer. parseInt (hashmap. get (sheet. getSheetName ()). toString (), sheet. getSheetName (). toString (), row. getCell (1 ). toString (). toUpperCase (). trim (), row. getCell (5 ). toString ();} for (int k = 0; k <row. getPhysicalNumberOfCells (); k ++) {// obtain each cell if (row. getCell (k )! = Null) {System. out. print (row. getCell (k) + "\ t"); // p. print (row. getCell (k) + "\ t");} else System. out. print ("row. getCell (k) is nullnull "+" \ t ");} // p. println ("row over"); System. out. println ("--- Sheet table" + I + "processed ---");} p. println ("--- Sheet table" + I + "processed ---"); // sort Collections. sort (list_rows );
// Load a map after reading one. put (Integer. parseInt (hashmap. get (sheet. getSheetName ()). toString (), list_rows) ;}} p. close ();} public static void main (String [] args) throws Exception {FileOutputStream f = new FileOutputStream (new File ("E: \ output \ result.txt "), false); PrintStream rs = new PrintStream (f); try {Map <Integer, ArrayList> map = new HashMap <> (); showExcelSheet (map); Map <Integer, arrayList> treemap = new TreeMap <> (); // The result table objects are sorted by export_id. putAll (map); for (int k: treemap. keySet () {System. out. println (k);} for (ArrayList <RowInfo> tab: treemap. values () {for (RowInfo row: tab) {rs. println (row. toString () ;}} catch (Exception e) {e. printStackTrace ();}}}