Exemplo n.º 1
0
 public static void addRow(
     WritableSheet sheet,
     int startRow,
     CellValue[] cellValues,
     WritableCellFormat cellFormat,
     WritableCellFormat intCellFormat,
     WritableCellFormat doubleCellFormat,
     WritableCellFormat dateFormat)
     throws WriteException {
   for (int i = 0; i < cellValues.length; i++) {
     if (cellValues[i] != null && cellValues[i].getValue() != null) {
       if (cellValues[i].getType().equals(CellDataType.STRING)) {
         Label label =
             new Label(i, startRow, String.valueOf(cellValues[i].getValue()), cellFormat);
         sheet.addCell(label);
       } else if (cellValues[i].getType().equals(CellDataType.INT)) {
         Number number =
             new Number(i, startRow, (Integer) cellValues[i].getValue(), intCellFormat);
         sheet.addCell(number);
       } else if (cellValues[i].getType().equals(CellDataType.DOUBLE)) {
         Number number =
             new Number(i, startRow, (Double) cellValues[i].getValue(), doubleCellFormat);
         sheet.addCell(number);
       } else if (cellValues[i].getType().equals(CellDataType.DATE)) {
         Date now = new Date(((Timestamp) cellValues[i].getValue()).getTime());
         DateTime dateCell = new DateTime(i, startRow, now, dateFormat);
         sheet.addCell(dateCell);
       }
     } else {
       Label label = new Label(i, startRow, "", cellFormat);
       sheet.addCell(label);
     }
   }
 }
Exemplo n.º 2
0
 public static int nextFreeColNumber(WritableSheet sheet) {
   if (sheet == null) return 0;
   int colCount = sheet.getColumns();
   Cell[] cells;
   int i = 0;
   for (; i < colCount; i++) {
     cells = sheet.getColumn(i);
     for (Cell cell : cells) {
       if (cell.getContents() == null || cell.getContents().isEmpty()) return i;
     }
   }
   return i;
 }
Exemplo n.º 3
0
 public static void writeToCell(WritableSheet sheet, String value, int col, int row) {
   if (sheet == null) return;
   Label label = new Label(col, row, value);
   try {
     sheet.addCell(label);
   } catch (WriteException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 4
0
  public static void addRow(
      WritableSheet sheet, int startRow, CellValue[] cellValues, WritableFont writableFont)
      throws WriteException {

    WritableCellFormat cellFormat = new WritableCellFormat(writableFont);
    cellFormat.setWrap(true);
    cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

    NumberFormat towdps = new NumberFormat("#,##0");
    WritableCellFormat towdpsFormat = new WritableCellFormat(writableFont, towdps);
    towdpsFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

    NumberFormat towdps2 = new NumberFormat("#,##0.00");
    WritableCellFormat towdpsFormat2 = new WritableCellFormat(writableFont, towdps2);
    towdpsFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);

    DateFormat customDateFormat = new DateFormat("dd MMM yyyy");
    WritableCellFormat dateFormat = new WritableCellFormat(writableFont, customDateFormat);
    dateFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

    for (int i = 0; i < cellValues.length; i++) {
      if (cellValues[i] != null && cellValues[i].getValue() != null) {
        if (cellValues[i].getType().equals(CellDataType.STRING)) {
          Label label =
              new Label(i, startRow, String.valueOf(cellValues[i].getValue()), cellFormat);
          sheet.addCell(label);
        } else if (cellValues[i].getType().equals(CellDataType.INT)) {
          Number number = new Number(i, startRow, (Integer) cellValues[i].getValue(), towdpsFormat);
          sheet.addCell(number);
        } else if (cellValues[i].getType().equals(CellDataType.DOUBLE)) {
          Number number = new Number(i, startRow, (Double) cellValues[i].getValue(), towdpsFormat2);
          sheet.addCell(number);
        } else if (cellValues[i].getType().equals(CellDataType.DATE)) {
          Date now = new Date(((Timestamp) cellValues[i].getValue()).getTime());
          DateTime dateCell = new DateTime(i, startRow, now, dateFormat);
          sheet.addCell(dateCell);
        }
      } else {
        Label label = new Label(i, startRow, "", cellFormat);
        sheet.addCell(label);
      }
    }
  }
Exemplo n.º 5
0
  // public method to extract and write the Publication information
  public static void WritePublication(String publicationId, int rowNum) {

    WritableSheet sheet2 = copy.getSheet(1);

    try {
      String query =
          "SELECT address,author,publicationTitle,chapter,edition,editor,month,day,note,number,publisher,"
              + "series,title,volume,year,isbn,issn,pages FROM Publication WHERE id="
              + publicationId;
      System.out.println(query);

      ResultSet result_query = statement.executeQuery(query);
      ResultSetMetaData metadata_query = result_query.getMetaData();
      int numberOfRows = 0;
      if (result_query.last()) numberOfRows = result_query.getRow();
      result_query.first();
      if (numberOfRows > 0 && metadata_query.getColumnCount() > 0) {
        String aut_edi = result_query.getString(2);
        System.out.println("autor is " + aut_edi);
        if (result_query.getString(6).compareTo("") != 0)
          aut_edi = aut_edi + " / " + result_query.getString(6) + ",eds.";
        Label label = new Label(7, rowNum, aut_edi);
        sheet2.addCell(label);
        label = new Label(8, rowNum, result_query.getString(13));
        sheet2.addCell(label);
        String publication_name = result_query.getString(3);
        if (result_query.getString(12).compareTo("") != 0)
          publication_name = publication_name + ", ser. " + result_query.getString(12);
        if (result_query.getString(14).compareTo("0") != 0)
          publication_name = publication_name + ", vol. " + result_query.getString(14);
        if (result_query.getString(10).compareTo("0") != 0)
          publication_name = publication_name + ",no. " + result_query.getString(10);
        label = new Label(9, rowNum, publication_name);
        sheet2.addCell(label);
        label = new Label(12, rowNum, result_query.getString(11));
        sheet2.addCell(label);
        label = new Label(13, rowNum, result_query.getString(1));
        sheet2.addCell(label);
        label = new Label(15, rowNum, result_query.getString(9));
        sheet2.addCell(label);
        label = new Label(16, rowNum, result_query.getString(16));
        sheet2.addCell(label);
        label = new Label(17, rowNum, result_query.getString(17));
        sheet2.addCell(label);
        label = new Label(14, rowNum, result_query.getString(18));
        sheet2.addCell(label);

        String date = result_query.getString(15);
        String month = "Jan";
        System.out.println("month is " + result_query.getString(7));
        if (result_query.getString(7).compareTo("") != 0) month = result_query.getString(7);
        month = func.ConvertMonth(month);
        date = month + "/" + date;
        String day = "01";
        if (result_query.getString(8).compareTo("0") != 0) day = result_query.getString(8);
        if (day.length() == 1) day = "0" + day;
        date = day + "/" + date;
        label = new Label(10, rowNum, date);
        sheet2.addCell(label);
        label = new Label(11, rowNum, date);
        sheet2.addCell(label);
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  } // end of WritePublication
Exemplo n.º 6
0
  // public method to find child information
  public void FindChildInformation(String tsn, String[] arguments, AdditionalFunctions func) {

    WritableSheet sheet2 = copy.getSheet(1);
    Label label = null;
    String children[] = null;
    String temp = "", current_tsn = "", dateCreated = "";
    int dateCheck = 1;
    temp = "SELECT distinct tsn from Tree where parent_tsn='" + tsn + "'";
    try {
      result = statement.executeQuery(temp);
      metadata = result.getMetaData();
      int numberOfRows = 0;
      if (result.last()) numberOfRows = result.getRow();
      if (numberOfRows > 0 && metadata.getColumnCount() > 0) {
        result.first();
        children = new String[numberOfRows];
        for (int j = 0; j < numberOfRows; j++) {
          children[j] = result.getString(1);
          result.next();
        }
      }
      for (int j = 0; j < numberOfRows; j++) {
        int current_tsnId = Integer.parseInt(children[j]);
        if (current_tsnId >= 999000000) {
          temp =
              "SELECT distinct Tree.tsn,unit_name1,unit_name2,unit_name3,unit_name4,scientificName,`usage`,"
                  + "taxon_author,parent_tsn,rank_name, pages, publicationId, userId, dateCreated, nameType FROM Tree,"
                  + " BaseObject, TaxonConcepts, TaxonAuthors,TaxonUnitTypes"
                  + " WHERE Tree.tsn=TaxonConcepts.tsn AND Tree.tsn='"
                  + current_tsnId
                  + "' AND "
                  + "BaseObject.id=TaxonConcepts.id AND TaxonAuthors.taxon_author_id=Tree.taxon_author_id AND "
                  + "Tree.rank_id=TaxonUnitTypes.rank_id";
          System.out.println(temp);

          result = statement.executeQuery(temp);
          metadata = result.getMetaData();
          int RowNumber = 0;
          if (result.last()) RowNumber = result.getRow();
          if (RowNumber > 0 && metadata.getColumnCount() > 0) {
            current_tsn = result.getString(1);
            dateCreated = result.getString(14);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = df.parse(dateCreated);
            Date d2 = null;
            if (date_from.compareTo("all") != 0) {
              d2 = df.parse(date_from);
              if (d2.before(d1) || d2.equals(d1)) dateCheck = 1;
              else dateCheck = 0;
            }
            if (date_to.compareTo("today") != 0) {
              d2 = df.parse(date_to);
              if (d1.before(d2) || d1.equals(d2)) dateCheck = 1;
              else dateCheck = 0;
            }
            if (result.getString(7).compareTo("public") == 0
                && result.getString(15).compareTo("Regular scientific name") == 0
                && result.getString(12).compareTo("") != 0
                && dateCheck == 1) {

              String scientificName = result.getString(6);
              String taxon_author = result.getString(8);
              String publicationId = result.getString(12);
              for (int k = 0; k < 4; k++) {
                if (result.getString(k + 2).compareTo("null") != 0) {
                  label = new Label(k, row + 1, result.getString(k + 2));
                  sheet2.addCell(label);
                }
              }
              label = new Label(4, row + 1, taxon_author);
              sheet2.addCell(label);
              label = new Label(5, row + 1, result.getString(10));
              sheet2.addCell(label);

              String parent_tsn = result.getString(9);
              String userId = result.getString(13);
              WritePublication(publicationId, row + 1);
              label = new Label(18, row + 1, func.GetSubmitter(userId));
              sheet2.addCell(label);
              label = new Label(6, row + 1, func.GetParent(parent_tsn));
              sheet2.addCell(label);

              row = row + 1;
            }
          }
        }
        continue;
      }
      for (int j = 0; j < numberOfRows; j++) {
        if (HasChildren(children[j]) == true) FindChildInformation(children[j], arguments, func);
        else continue;
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  } // end of FindChildInformation
  private void exportReport2Excel(
      ExportMaterialReportBean bean,
      ExportMaterialReportDTO results,
      HttpServletRequest request,
      HttpServletResponse response) {
    try {
      String outputFileName =
          "/files/temp/BaoCaoNhapXuatTonNPL" + System.currentTimeMillis() + ".xls";
      String reportTemplate =
          request
              .getSession()
              .getServletContext()
              .getRealPath("/files/export/ExportMaterialReport.xls");
      String export2FileName = request.getSession().getServletContext().getRealPath(outputFileName);

      Workbook templateWorkbook = Workbook.getWorkbook(new File(reportTemplate));
      WritableWorkbook workbook =
          Workbook.createWorkbook(new File(export2FileName), templateWorkbook);

      WritableSheet sheet = workbook.getSheet(0);

      WritableFont normalFont =
          new WritableFont(
              WritableFont.TIMES,
              12,
              WritableFont.NO_BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK);
      WritableCellFormat normalFormat = new WritableCellFormat(normalFont);
      normalFormat.setAlignment(Alignment.LEFT);
      normalFormat.setWrap(true);
      normalFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

      WritableFont boldFont =
          new WritableFont(
              WritableFont.TIMES,
              12,
              WritableFont.BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK);
      WritableCellFormat headerFormat = new WritableCellFormat(boldFont);
      headerFormat.setAlignment(Alignment.CENTRE);
      headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
      headerFormat.setWrap(true);
      headerFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
      headerFormat.setBackground(jxl.format.Colour.GRAY_25);

      WritableCellFormat boldFormat = new WritableCellFormat(boldFont);
      boldFormat.setAlignment(Alignment.CENTRE);
      boldFormat.setWrap(true);
      boldFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

      DecimalFormat decimalFormat = new DecimalFormat("###,###.##");

      Date from = bean.getFromDate();
      Label fromCell =
          new Label(
              1, 1, from != null ? DateUtils.date2String(from, "dd/MM/yyyy") : "", boldFormat);
      sheet.addCell(fromCell);

      Date to = bean.getToDate();
      Label toCell =
          new Label(1, 2, to != null ? DateUtils.date2String(to, "dd/MM/yyyy") : "", boldFormat);
      sheet.addCell(toCell);

      int startRow = 5;
      List<ExportMaterialReportDetailDTO> initialValue = results.getInitialValue();
      Map<String, Double> mapImportValue = results.getMapImportValue();
      Map<String, Double> mapExportUtilDateValue = results.getMapExportUtilDateValue();
      Map<String, Double> mapExportDuringDateValue = results.getMapExportDuringDateValue();
      int index;
      CellValue[] resValue;
      Double iVal;
      Double exportToVal;
      Double importVal;
      Double exportVal;
      Double remainVal;
      String key, origin;
      for (ExportMaterialReportDetailDTO initVal : initialValue) {
        origin = initVal.getOrigin() != null ? initVal.getOrigin().getOriginID().toString() : "";
        key = initVal.getMaterial().getMaterialID() + "_" + origin;
        exportToVal =
            mapExportUtilDateValue.get(key) != null ? mapExportUtilDateValue.get(key) : 0d;
        iVal =
            initVal.getQuantity() != null ? initVal.getQuantity() - exportToVal : 0 - exportToVal;
        importVal = mapImportValue.get(key) != null ? mapImportValue.get(key) : 0d;
        exportVal =
            mapExportDuringDateValue.get(key) != null ? mapExportDuringDateValue.get(key) : 0d;
        remainVal = iVal + importVal - exportVal;
        index = 0;
        resValue = new CellValue[10];

        resValue[index++] =
            new CellValue(
                CellDataType.STRING,
                initVal.getOrigin() != null ? initVal.getOrigin().getName() : "");
        resValue[index++] =
            new CellValue(
                CellDataType.STRING,
                initVal.getMaterial() != null ? initVal.getMaterial().getName() : "");
        resValue[index++] =
            new CellValue(
                CellDataType.STRING,
                StringUtils.isNotBlank(initVal.getCode()) ? initVal.getCode() : "");
        resValue[index++] =
            new CellValue(
                CellDataType.STRING,
                initVal.getMaterial() != null
                    ? initVal.getMaterial().getUnit() != null
                        ? initVal.getMaterial().getUnit().getName()
                        : ""
                    : "");
        resValue[index++] = new CellValue(CellDataType.STRING, decimalFormat.format(iVal));
        resValue[index++] = new CellValue(CellDataType.STRING, decimalFormat.format(importVal));
        resValue[index++] = new CellValue(CellDataType.STRING, decimalFormat.format(exportVal));
        resValue[index++] = new CellValue(CellDataType.STRING, decimalFormat.format(remainVal));
        resValue[index++] = new CellValue(CellDataType.STRING, "");
        resValue[index++] =
            new CellValue(
                CellDataType.STRING,
                initVal.getImportDate() != null
                    ? DateUtils.date2String(initVal.getImportDate(), "dd/MM/yyyy")
                    : "");

        ExcelUtil.addRow(
            sheet, startRow++, resValue, normalFormat, normalFormat, normalFormat, normalFormat);
      }
      workbook.write();
      workbook.close();
      response.sendRedirect(
          request.getSession().getServletContext().getContextPath() + outputFileName);
    } catch (Exception ex) {
      logger.error(ex.getMessage(), ex);
    }
  }
Exemplo n.º 8
0
  public static void main(String args[]) {
    try {
      // 打开文件
      WritableWorkbook book = Workbook.createWorkbook(new File("/home/uc/local/sandy/测试.xls"));
      // 生成名为“第一页”的工作表,参数0表示这是第一页
      WritableSheet sheetOne = book.createSheet("第一页", 0);

      /** 定义单元格样式 */
      WritableFont wf_title =
          new WritableFont(
              WritableFont.ARIAL,
              11,
              WritableFont.NO_BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
      WritableFont wf_head =
          new WritableFont(
              WritableFont.ARIAL,
              11,
              WritableFont.NO_BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
      WritableFont wf_table =
          new WritableFont(
              WritableFont.ARIAL,
              11,
              WritableFont.NO_BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色

      WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义
      wcf_title.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色
      wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
      wcf_title.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK); // 设置边框

      WritableCellFormat wcf_title1 = new WritableCellFormat(wf_title); // 单元格定义
      wcf_title1.setBackground(jxl.format.Colour.LIGHT_GREEN); // 设置单元格的背景颜色
      wcf_title1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
      wcf_title1.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK); // 设置边框

      WritableCellFormat wcf_title2 = new WritableCellFormat(wf_title); // 单元格定义
      wcf_title2.setBackground(jxl.format.Colour.YELLOW2); // 设置单元格的背景颜色
      wcf_title2.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
      wcf_title2.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK); // 设置边框

      WritableCellFormat wcf_head1 = new WritableCellFormat(wf_head);
      wcf_head1.setBackground(jxl.format.Colour.LIGHT_GREEN);
      wcf_head1.setAlignment(jxl.format.Alignment.CENTRE);
      wcf_head1.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);

      WritableCellFormat wcf_head2 = new WritableCellFormat(wf_head);
      wcf_head2.setBackground(jxl.format.Colour.YELLOW2);
      wcf_head2.setAlignment(jxl.format.Alignment.CENTRE);
      wcf_head2.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);

      WritableCellFormat wcf_table1 = new WritableCellFormat(wf_table);
      wcf_table1.setBackground(jxl.format.Colour.LIGHT_GREEN);
      wcf_table1.setAlignment(jxl.format.Alignment.CENTRE);
      wcf_table1.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);

      WritableCellFormat wcf_table2 = new WritableCellFormat(wf_table);
      wcf_table2.setBackground(jxl.format.Colour.YELLOW2);
      wcf_table2.setAlignment(jxl.format.Alignment.CENTRE);
      wcf_table2.setBorder(
          jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN, jxl.format.Colour.BLACK);

      sheetOne.setColumnView(0, 15); // 设置列的宽度
      sheetOne.setColumnView(1, 15); // 设置列的宽度
      sheetOne.setColumnView(2, 15); // 设置列的宽度
      sheetOne.setColumnView(3, 15); // 设置列的宽度
      sheetOne.setColumnView(4, 15); // 设置列的宽度
      sheetOne.setColumnView(5, 15); // 设置列的宽度
      sheetOne.setColumnView(6, 15); // 设置列的宽度
      sheetOne.setColumnView(7, 15); // 设置列的宽度
      sheetOne.setColumnView(8, 15); // 设置列的宽度
      sheetOne.setColumnView(9, 15); // 设置列的宽度
      sheetOne.setColumnView(10, 15); // 设置列的宽度
      sheetOne.setColumnView(11, 15); // 设置列的宽度
      sheetOne.setColumnView(12, 15); // 设置列的宽度
      sheetOne.setColumnView(13, 15); // 设置列的宽度

      // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
      // 以及单元格内容为test
      Label title = new Label(0, 0, "统计", wcf_title);
      Label titleOne = new Label(0, 1, "统计1", wcf_title1);
      Label titleTwo = new Label(2, 1, "统计2", wcf_title2);

      Label column1 = new Label(0, 2, "姓名", wcf_head1);
      Label column2 = new Label(1, 2, "所选课程", wcf_head1);

      Label column3 = new Label(2, 2, "姓名", wcf_head2);
      Label column4 = new Label(3, 2, "所选课程", wcf_head2);

      // 或者WritableCell cell =  new jxl.write.Number(column, row, value, wcf)
      // 将定义好的单元格添加到工作表中
      sheetOne.addCell(title);
      sheetOne.addCell(titleOne);
      sheetOne.addCell(titleTwo);

      sheetOne.addCell(column1);
      sheetOne.addCell(column2);
      sheetOne.addCell(column3);
      sheetOne.addCell(column4);

      // 合: 第1列第1行  到 第13列第1行
      sheetOne.mergeCells(0, 0, 3, 0);
      sheetOne.mergeCells(0, 1, 1, 1);
      sheetOne.mergeCells(2, 1, 3, 1);

      /*动态数据   */
      Label content1 = new Label(0, 3, "张三", wcf_table1);
      Label content2 = new Label(0, 4, "张三", wcf_table1);
      Label content3 = new Label(0, 5, "张三", wcf_table1);
      Label kecheg1 = new Label(1, 3, "语文", wcf_table1);
      Label kecheg2 = new Label(1, 4, "数学", wcf_table1);
      Label kecheg3 = new Label(1, 5, "英语", wcf_table1);

      sheetOne.addCell(content1);
      sheetOne.addCell(content2);
      sheetOne.addCell(content3);
      sheetOne.addCell(kecheg1);
      sheetOne.addCell(kecheg2);
      sheetOne.addCell(kecheg3);

      sheetOne.mergeCells(0, 3, 0, 2 + 3);

      Label content11 = new Label(2, 3, "李四", wcf_table2);
      Label content22 = new Label(2, 4, "李四", wcf_table2);
      Label content33 = new Label(2, 5, "李四", wcf_table2);
      Label kecheg11 = new Label(3, 3, "语文", wcf_table2);
      Label kecheg22 = new Label(3, 4, "数学", wcf_table2);
      Label kecheg33 = new Label(3, 5, "英语", wcf_table2);

      sheetOne.addCell(content11);
      sheetOne.addCell(content22);
      sheetOne.addCell(content33);
      sheetOne.addCell(kecheg11);
      sheetOne.addCell(kecheg22);
      sheetOne.addCell(kecheg33);

      sheetOne.mergeCells(2, 3, 2, 2 + 3);

      // 写入数据并关闭文件
      book.write();
      book.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
Exemplo n.º 9
0
  /**
   * 业务逻辑实现 - 点击统计下载excel
   *
   * @param ws 工作表
   * @param totals 待写入数据
   * @throws WriteException
   */
  @SuppressWarnings("unchecked")
  private static void addLabelExcel(WritableSheet ws, List totals, List columnList)
      throws WriteException {
    int row = 0, column = 0;
    // 设置Excel列中文标题
    Iterator itColumnList = columnList.iterator();
    while (itColumnList.hasNext()) {
      DownloadAttribute columnTitle = (DownloadAttribute) itColumnList.next();
      ws.addCell(new Label(column, row, columnTitle.getExcelName()));
      column++;
    }
    if (totals != null && totals.size() != 0) {
      Iterator itTotals = totals.iterator();
      WritableFont detFont =
          new WritableFont(
              WritableFont.ARIAL,
              10,
              WritableFont.NO_BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK);
      NumberFormat pnf = new NumberFormat("0.00"); // 用于Price Number的格式
      WritableCellFormat priceFormat = new WritableCellFormat(detFont, pnf);
      NumberFormat nf = new NumberFormat("0"); // 用于Number的格式
      WritableCellFormat numberFormat = new WritableCellFormat(detFont, nf);

      WritableFont stringFont =
          new WritableFont(
              WritableFont.ARIAL,
              10,
              WritableFont.NO_BOLD,
              false,
              UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK);
      WritableCellFormat stringFormat = new WritableCellFormat(stringFont);
      WritableCellFormat stringWrapFormat = new WritableCellFormat(stringFont);
      stringWrapFormat.setWrap(true);

      row = 1;
      while (itTotals.hasNext()) {
        Object obj = itTotals.next();
        Map subTotals = obj instanceof Map ? (HashMap) obj : ObjectUtil.transform(obj);
        Iterator itColumnName = columnList.iterator();
        column = 0;
        while (itColumnName.hasNext()) {
          DownloadAttribute columnInfo = (DownloadAttribute) itColumnName.next();
          String columnType = columnInfo.getExcelType();
          String columnName = columnInfo.getRowName();
          Object columnValue = subTotals.get(columnName);
          if (columnValue == null) {
            column++;
            continue;
          }
          String strValue = null;
          if (columnValue instanceof Date) {
            strValue = DateTimeUtil.getFormatDateTime((Date) columnValue);
          } else {
            strValue = columnValue.toString();
          }
          // 设置内容
          if (columnType.equals("Label")) {
            if (columnValue.toString().indexOf("\r\n") > 0) {
              ws.addCell(new Label(column, row, strValue, stringWrapFormat));
            } else {
              ws.addCell(new Label(column, row, strValue, stringFormat));
            }
          }
          if (columnType.equals("Number"))
            ws.addCell(
                new jxl.write.Number(column, row, Double.parseDouble(strValue), numberFormat));
          if (columnType.equals("Price"))
            ws.addCell(
                new jxl.write.Number(column, row, Double.parseDouble(strValue), priceFormat));
          column++;
        }
        row++;
      }
    }
  }
Exemplo n.º 10
0
 private void addLabel(WritableSheet sheet, int column, int row, String s)
     throws WriteException, RowsExceededException {
   Label label;
   label = new Label(column, row, s, times);
   sheet.addCell(label);
 }
Exemplo n.º 11
0
 private void addCaption(WritableSheet sheet, int column, int row, String s)
     throws RowsExceededException, WriteException {
   Label label;
   label = new Label(column, row, s, timesBoldUnderline);
   sheet.addCell(label);
 }