/ **
** target cell value
* /
public class Cells { / ** * * row * / Private int Row; / ** * column * / Private int column; / ** * value of the cell * / Private Val Object; public int the getRow () { return Row; } public void setRow ( int Row) { the this .Row = Row; } public int getColumn () { return column; } public void setColumn(int column) { this.column = column; } public Object getVal() { return val; } public void setVal(Object val) { this.val = val; } public Cells(int row, int column, Object val) { this.row = row; this.column = column; this.val = val; } public Cells() {} }
/**
**表空间对象
**/
public class Sheet { private String sheetName; public String getSheetName() { return sheetName; } public void setSheetName(String sheetName) { this.sheetName = sheetName; } public List<Cells> getCells() { return cells; } public void setCells(List<Cells> cells) { this.cells = cells; } private List<Cells> cells=Lists.newArrayList(); public Sheet(String sheetName, List<Cells> cells) { this.sheetName = sheetName; this.cells = cells; } public Sheet() {} }
. 1 public Final class ExcelUntil { 2 . 3 / ** . 4 * The template derived Excel . 5 * @param the templatePath template path . 6 * @param sheets sheet setting table cell specific value of the object space . 7 * @param ExportPath export path 8 * @ throws Exception . 9 * / 10 @SuppressWarnings ( "Resource" ) . 11 public static void exportExcelByTemplate (String the templatePath, List <Sheet> sheets, String ExportPath) 12 is throws Exception { 13 if (Strings.isStringEmpty(templatePath) || CollectionUtils.isEmpty(sheets) || Strings.isStringEmpty(exportPath)) { 14 return; 15 } 16 InputStream in = ExcelUntil.class.getResourceAsStream(templatePath); 17 POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in); 18 HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem); 19 for (int i = 0; i < sheets.size(); i++) { 20 HSSFSheet sheet = workbook.getSheetAt(i); 21 sheet.setForceFormulaRecalculation(true); 22 List<Cells> cells = sheets.get(i).getCells(); 23 cellSetValue(sheet, cells); 24 } 25 FileOutputStream out = new FileOutputStream(exportPath); 26 workbook.write(out); 27 out.close(); 28 } 29 30 31 public static HSSFWorkbook exportExcel(String templatePath, List<Sheet> sheets, String exportPath) 32 throws Exception { 33 InputStream in = ExcelUntil.class.getResourceAsStream(templatePath); 34 POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in); 35 HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem); 36 for (int i = 0; i < sheets.size(); i++) { 37 HSSFSheet sheet = workbook.getSheetAt(i); 38 sheet.setForceFormulaRecalculation(true); 39 List<Cells> cells = sheets.get(i).getCells(); 40 cellSetValue (Sheet, cells); 41 is } 42 is return Workbook; 43 is 44 is } 45 46 is 47 / ** 48 * set value of the specific cell 49 * @param Sheet particular tablespace 50 * @param particular cells arranged in the cell 51 * / 52 is Private static void cellSetValue (HSSFSheet Sheet, List <cells> cells) { 53 is // set the ranks of the data and the set value 54 is cells C = null ; 55 Object Val = null; 56 HSSFCell cell = null; 57 for (int i = 0; i < cells.size(); i++) { 58 c = cells.get(i); 59 if(c==null) { 60 continue; 61 } 62 val = c.getVal(); 63 cell = sheet.getRow(c.getRow() - 1).getCell(c.getColumn() - 1); 64 if (val instanceof Integer) { 65 cell.setCellValue((Integer) val); 66 } else if (val instanceof Double) { 67 cell.setCellValue((Double) val); 68 } else if (val instanceof Date) { 69 cell.setCellValue((Date) val); 70 } else if (val instanceof Short) { 71 cell.setCellValue((short) val); 72 } else { 73 cell.setCellValue(val + ""); 74 } 75 } 76 } 77 }