First introduced into jar package, with maven
1 <dependency> 2 <groupId>org.apache.poi</groupId> 3 <artifactId>poi</artifactId> 4 <version>3.9</version> 5 </dependency> 6 7 <dependency> 8 <groupId>org.apache.poi</groupId> 9 <artifactId>poi-ooxml</artifactId> 10 <version>3.9</version> 11 </dependency>
Next is the basic operation of poi, so that output can be, the style is too much trouble operating table, although not difficult, it does not describe the program generated document can not see on the list. Ha ha ha
@Test public void Print () throws IOException { // Set the search criteria Factory's Factory = new new Factory's (); factory.setState(1); List<Factory> dataList = FactoryMapper.find(factory); String [] title = new new String [] { "factory name", "abbreviation", "Contacts", "telephone", "mobile phone", "FAX", "Notes" }; Wb Workbook = new new HSSFWorkbook (); // create a workbook Sheet Sheet = wb.createSheet (); // create a workbook SHEET Row nRow = sheet.createRow(0);//新建行对象 Cell nCell=null; for (int i = 0; i < title.length; i++) { ncell = nRow.createCell (I); // new cell object I-th row 0 nCell.setCellValue (title [I]); // cell assignment } //写数据 int nrow=1; int ncell=0; for(int j=0;j<dataList.size();j++){ ncell = 0 ; // initialize Factory's dataList.get F = (J); // acquired each manufacturer records // initialization start of the second line, so it should be the initial row 1, and then runs from plus nRow = sheet.createRow (nrow ++ ); nRow.setHeightInPoints(21); // first cell 1 starts, the cell is 0, it runs from plus ncell = nRow.createCell (ncell ++ ); nCell.setCellValue(f.getFullName()); nCell = nRow.createCell(ncell++); nCell.setCellValue(f.getFactoryName()); nCell = nRow.createCell(ncell++); nCell.setCellValue(f.getContractor()); nCell = nRow.createCell(ncell++); nCell.setCellValue(f.getPhone()); nCell = nRow.createCell(ncell++); nCell.setCellValue(f.getMobile()); nCell = nRow.createCell(ncell++); nCell.setCellValue(f.getFax()); nCell = nRow.createCell(ncell++); nCell.setCellValue(f.getCnote()); } // output path a FileOutputStream fos = new new a FileOutputStream ( new new File ( "G: //a.xls" )); // WB write stream wb.write (fos); // output stream and then close the refresh fos.flush () ; fos.close(); }
Note that the output file name can only be the end of xls!