当前位置: 代码迷 >> Web前端 >> java 写excel 资料
  详细解决方案

java 写excel 资料

热度:426   发布时间:2012-08-13 13:21:53.0
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();
??? ??? ??? ??? }
??? ??? ??? }
??? ???
??? }

  相关解决方案