[Java] Write xls files

public String createFile(Object object) {
    try {
        Excel excel = (Excel) object;
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        for (SheetContent sheetContent:excel.getSheets()) {
            HSSFSheet sheet = hssfWorkbook.createSheet(sheetContent.getName());

            List<HSSFCellStyle> formats = new ArrayList<>();
            /** format */
            for (String format:sheetContent.getFormats()) {
                HSSFCellStyle cs = hssfWorkbook.createCellStyle();
                HSSFDataFormat df = hssfWorkbook.createDataFormat();
                cs.setDataFormat(df.getFormat(format));
                formats.add(cs);
            }

            /** header */
            HSSFRow headerRow = sheet.createRow(0);
            for (int i = 0; i < sheetContent.getHeaders().size(); i++) {
                HSSFCell cell = headerRow.createCell(i);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(sheetContent.getHeaders().get(i));
            }

            /** content */
            for (int i = 0; i < sheetContent.getRows().size(); i ++) {
                HSSFRow dataRow = sheet.createRow(i + 1);
                for (int j = 0; j < sheetContent.getRows().get(i).getCells().size(); j++) {
                    HSSFCell cell = dataRow.createCell(j);
                    if (StringUtils.isNotEmpty(sheetContent.getFormats().get(j))) {
                        cell.setCellValue(Double.parseDouble(sheetContent.getRows().get(i).getCells().get(j)));
                        cell.setCellStyle(formats.get(j));
                    } else {
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cell.setCellValue(sheetContent.getRows().get(i).getCells().get(j));
                    }
                }
            }

            /** auto size */
            for (int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++) {
                sheet.autoSizeColumn(i);
            }
        }

        FileOutputStream out = new FileOutputStream(new File("Excel.xls"));
        hssfWorkbook.write(out);
        out.close();

        return "ok";
    } catch (Exception e) {
        LOG.error(Constants.EXCEPTION_PREFIX, e);
        return null;
    }
}

public class Excel {
    List<SheetContent> sheets;

    public List<SheetContent> getSheets() {
        return sheets;
    }

    public Excel setSheets(List<SheetContent> sheets) {
        this.sheets = sheets;
        return this;
    }

    public Excel addSheets(SheetContent sheet) {
        if (this.sheets == null) {
            this.sheets = new ArrayList<>();
        }
        this.sheets.add(sheet);
        return this;
    }
}

public class SheetContent {
    private String name;
    private List<String> formats;
    private List<String> headers;
    private List<Row> rows;

    public String getName() {
        return name;
    }

    public SheetContent setName(String name) {
        this.name = name;
        return this;
    }


    public List<String> getFormats() {
        return formats;
    }

    public SheetContent setFormats(List<String> formats) {
        this.formats = formats;
        return this;
    }

    public List<String> getHeaders() {
        return headers;
    }

    public SheetContent setHeaders(List<String> headers) {
        this.headers = headers;
        return this;
    }

    public List<Row> getRows() {
        return rows;
    }

    public SheetContent setRows(List<Row> rows) {
        this.rows = rows;
        return this;
    }
}

public class Row {
    private List<String> cells;

    public List<String> getCells() {
        return cells;
    }

    public Row setCells(List<String> cells) {
        this.cells = cells;
        return this;
    }
}
This entry was posted in Excel, Java. Bookmark the permalink.