JXL简单操作xls表格文件--写入文件

工作时用到写入Excel的场景,发现有Apache POI提供的jar包与JXL的jar包。实际操作时,个人感觉poi比较高大上,基本上能够实现excel的大部分功能,包括字体设置、表格设置等内容,但相对门槛较高,操作复杂。相比POI,JXL操作简单,容易入门,相比而言得到的EXCEL文档也是比简单,不需要特殊的格式化。

    public static void write2xlsStr(WritableWorkbook book, String[] title,
            List<String[]> lst) throws RowsExceededException, WriteException,
            IOException {

        WritableSheet sheet = book.createSheet("Page_First", 0);

        int colSize = title.length;
        int rowSize = lst.size();

        for (int i = 0; i < colSize; i++)
            sheet.addCell(new Label(i, 0, title[i]));

        for (int i = 0; i < rowSize; i++)
            for (int j = 0; j < colSize; j++)
                sheet.addCell(new Label(j, i + 1, lst.get(i)[j]));
        book.write();
        book.close();

    }

    public static void write2xlsLst(WritableWorkbook book, String[] title,
            List<List<String>> lst) throws RowsExceededException,
            WriteException, IOException {

        WritableSheet sheet = book.createSheet("Page_First", 0);

        int colSize = title.length;
        int rowSize = lst.size();

        for (int i = 0; i < colSize; i++)
            sheet.addCell(new Label(i, 0, title[i]));

        for (int i = 0; i < rowSize; i++)
            for (int j = 0; j < colSize; j++)
                sheet.addCell(new Label(j, i + 1, lst.get(i).get(j)));
        book.write();
    }

    public static WritableWorkbook getWorkBook(String filename)
            throws IOException {
        return Workbook.createWorkbook(new File(filename));
    }

    public static void write2xlsMap(WritableWorkbook book, String sheetname,
            int index, List<Map<String, Object>> lst)
            throws RowsExceededException, WriteException, IOException {

        WritableSheet sheet = book.createSheet(sheetname, index);
        int colSize = lst.get(0).keySet().size();
        int rowSize = lst.size();

        if (colSize < 1)
            return;
        if (rowSize < 1)
            return;

        int ind = 0;
        String[] title = new String[colSize];
        for (String str : lst.get(0).keySet()) {
            sheet.addCell(new Label(ind, 0, str));
            title[ind++] = str;
        }

        int col_index = 0;
        int row_index = 0;
        int names = 1;
        for (int i = 0; i < rowSize; i++) {
            col_index = 0;
            for (int j = 0; j < colSize; j++) {
                sheet.addCell(new Label(col_index, row_index + 1, ""
                        + lst.get(row_index).get(title[col_index])));
                col_index++;
            }
            if (row_index++ > MAX_ITEMS) {
                row_index = 0;
                sheet = book.createSheet(sheetname + "add_" + (names), index
                        + (names++));
            }
        }
        book.write();
    }

调用时,可参考如下方式:

WritableWorkbook book = getWorkBook("Filepath");
// your data format: list for list<map> data
write2xlsMap(book,...);
book.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值