java 写excel 文件

    public void show(String savePath, UserShareLog userShareLogP,
                String s_addtime, String e_addtime) {
       
            FileOutputStream outSTr = null;
            try {
                outSTr = new FileOutputStream(new File(savePath));
                WritableWorkbook wbook = Workbook.createWorkbook(outSTr); // 建立excel文件
                // 设置Excel字体
                WritableFont wfont = new WritableFont(WritableFont.ARIAL, 12,
                        WritableFont.BOLD, false,
                        jxl.format.UnderlineStyle.NO_UNDERLINE,
                        jxl.format.Colour.BLACK);

                QueryResult<UserShareLog> queryResult = userShareLogService.search(
                        1, 8000, userShareLogP, s_addtime, e_addtime);

                        //创建一个sheet 如果要多个,可以改变后面数字
                        WritableSheet wsheet = wbook.createSheet("用户分享统计表--第1页", 0); // 工作表名称

                        WritableCellFormat titleFormat = new WritableCellFormat(
                                wfont);
                       
                        // 设置Excel表头
                        String[] title = { "分享类型", "手机号码", "点击时间", "下发时间", "UUID" };
                        for (int i = 0; i < title.length; i++) {
                            Label excelTitle = new Label(i, 0, title[i],
                                    titleFormat);
                            wsheet.addCell(excelTitle);
                        }
                        int c = 1; // 用于循环时Excel的行号
                        UserShareLog taskLog;
                        Iterator<UserShareLog> it = queryResult.getQueryList().iterator();
                        while (it.hasNext()) {
                            taskLog = (UserShareLog) it.next();
                            String stype = "";
                            int type = taskLog.getType();
                            if (type == 1) {
                                stype = "精品软件";
                            } else if (type == 2) {
                                stype = "推荐阅读";
                            } else if (type == 3) {
                                stype = "热门微博";
                            }


                            Label content1 = new Label(0, c, stype);
                            Label content2 = new Label(1, c, taskLog.getPhone());
                            Label content3 = new Label(2, c, UtilHelp.dateFormat(
                                    taskLog.getClickTime(), null));
                            Label content4 = new Label(3, c, UtilHelp.dateFormat(
                                    taskLog.getPushTime(), null));
                            Label content5 = new Label(4, c, taskLog.getSysUuid());
                            wsheet.addCell(content1);
                            wsheet.addCell(content2);
                            wsheet.addCell(content3);
                            wsheet.addCell(content4);
                            wsheet.addCell(content5);
                            c++;
                        }
                wbook.write(); // 写入文件
                wbook.close();
                outSTr.close();
            } catch (Exception e) {
                e.printStackTrace();// ("导出文件出错");
            } finally {
                try {
                    outSTr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
       
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值