Java利用JXL产生Excel(.xls)

 Java利用JXL产生Excel(.xls)

 

JXL Maven依赖

<dependency>
      <groupId>jexcelapi</groupId>
      <artifactId>jxl</artifactId>
      <version>2.4.2</version>
    </dependency>

 

核心渲染文件:

private String getMuiltpleSheetContent2(List<SheetVo> dataList) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        WritableWorkbook wwb = null;
        try {
            // create WritableWorkbook
            wwb = Workbook.createWorkbook(out);
            if (wwb != null && null != dataList) {
                SheetVo sheetVo = null;
                List<String> columnNames = null;
                List<Map<String,Object>> rows = null;
                Map<String,Object> row = null;
                int size = dataList.size();
                for (int index = 0;index < size;index++) {
                    sheetVo = dataList.get(index);
                    columnNames = sheetVo.getColumnNames();
                    rows = sheetVo.getRows();
                    // createSheet has to param,the first one is sheet name,the
                    // second is position
                    WritableSheet ws = wwb.createSheet(sheetVo.getSheetName(), index);
                    // generate cells for sheet
                    int columnsSize = columnNames.size();
                    // generate header row
                    for (int i = 0;i < columnsSize;i++) {
                        // add cell to sheet new Label(column,row,content)
                        ws.addCell(new Label(i, 0, columnNames.get(i)));
                    }
                    // generate content rows
                    int vSize = rows.size();
                    for (int i = 0;i < vSize;i++) {
                        row = rows.get(i);
                        for (int j = 0;j < columnsSize;j++) {
                            // add cell to sheet new Label(column,row,content)
                            ws.addCell(new Label(j, i + 1, row.get(columnNames.get(j)).toString()));
                        }
                    }
                }
                wwb.write();
                wwb.close();
            }
            return RDSUtil.encodeToBase64String(out.toByteArray());
        } catch (Exception e) {
            LOG.error("RDS ERROR!", e);
            return "";
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                LOG.error("RDS ERROR!", e);
            }
        }
    }

 

 Base64字节编码和解码:

public static String encodeToBase64String(byte[] bytes) {
        return Base64Utils.tob64(bytes);
    }

    public static byte[] decodeToBase64Byte(String str) {
        return Base64Utils.fromb64(str);
    }

 引用Vo文件:

public class SheetVo implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7307728417389899304L;

    private String sheetName;
    private String titleActive;
    private List<String> columnNames;
    private List<Map<String,Object>> rows;

    public SheetVo() {
    }

    public SheetVo(List<String> columnNames, List<Map<String,Object>> rows) {
        new SheetVo(null, null, columnNames, rows);
    }

    public SheetVo(String sheetName, String titleActive, List<String> columnNames, List<Map<String,Object>> rows) {
        super();
        this.sheetName = sheetName;
        this.titleActive = titleActive;
        this.columnNames = columnNames;
        this.rows = rows;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public String getTitleActive() {
        return titleActive;
    }

    public void setTitleActive(String titleActive) {
        this.titleActive = titleActive;
    }

    public List<String> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(List<String> columnNames) {
        this.columnNames = columnNames;
    }

    public List<Map<String,Object>> getRows() {
        return rows;
    }

    public void setRows(List<Map<String,Object>> rows) {
        this.rows = rows;
    }

}

 

你可能感兴趣的:(Excel)