Пример #1
0
  public static void writeExcel(String excelPath, String testCase) throws Exception {
    /**
     * create Excel file in excelPath build two sheet which names are "TestSummary" and testcase
     * detailinfo
     */
    FileOutputStream fos = new FileOutputStream(excelPath);
    HSSFWorkbook wb = new HSSFWorkbook();

    HSSFSheet s1 = wb.createSheet();
    wb.setSheetName(0, "TestSummary");
    HSSFSheet s2 = wb.createSheet();
    wb.setSheetName(1, testCase + "_DetailInfo");

    HSSFRow s1row = s1.createRow(0);
    HSSFRow s2row = s2.createRow(0);

    s1row.createCell(0).setCellValue("Total TCs");
    s1row.createCell(1).setCellValue("Run TCs");
    s1row.createCell(2).setCellValue("Passed TCs");
    s1row.createCell(3).setCellValue("Failure TCs");
    s1row.createCell(4).setCellValue("NoRun TCs");

    s2row.createCell(0).setCellValue("SubTestCase");
    s2row.createCell(1).setCellValue("TestResult");
    s2row.createCell(2).setCellValue("AssertPointCheck");
    s2row.createCell(3).setCellValue("ScreenShot");
    s2row.createCell(4).setCellValue("Processor");

    wb.write(fos);
    fos.close();
  }
Пример #2
0
  public void createExcelFile(String filename) throws Exception {
    out = new FileOutputStream(filename);
    wb = new HSSFWorkbook();
    ws = this.wb.createSheet();
    cs1 = this.wb.createCellStyle();
    cs2 = this.wb.createCellStyle();
    cs3 = this.wb.createCellStyle();
    df = this.wb.createDataFormat();
    f1 = this.wb.createFont();
    f2 = this.wb.createFont();

    f1.setFontHeightInPoints((short) 10);
    f1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

    f2.setFontHeightInPoints((short) 13);
    f2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
    f2.setColor(HSSFFont.COLOR_RED);

    cs1.setFont(f1);
    cs1.setDataFormat(df.getFormat("text"));

    cs2.setFont(f2);
    cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));

    cs3.setFont(f1);
    cs3.setDataFormat(df.getFormat("#,##0.0"));

    wb.setSheetName(0, "QryDetail", HSSFWorkbook.ENCODING_UTF_16);
  }
Пример #3
0
  public void beforeBody(TableModel model) {
    logger.debug("XlsView.init()");

    moneyFormat =
        model
            .getPreferences()
            .getPreference(PreferencesConstants.TABLE_EXPORTABLE + "format.money");
    if (StringUtils.isEmpty(moneyFormat)) {
      moneyFormat = DEFAULT_MONEY_FORMAT;
    }
    percentFormat =
        model
            .getPreferences()
            .getPreference(PreferencesConstants.TABLE_EXPORTABLE + "format.percent");
    if (StringUtils.isEmpty(percentFormat)) {
      percentFormat = DEFAULT_PERCENT_FORMAT;
    }

    wb = new HSSFWorkbook();
    sheet = wb.createSheet();
    wb.setSheetName(0, "Export Workbook");

    styles = initStyles(wb);
    ps = sheet.getPrintSetup();

    sheet.setAutobreaks(true);
    ps.setFitHeight((short) 1);
    ps.setFitWidth((short) 1);

    createHeader(model);
  }
Пример #4
0
  /**
   * 生成文件
   *
   * @throws IOException
   */
  public byte[] run() throws IOException {
    // 初始化参数
    initParameter();

    // 实例化decorator
    initDecorator();

    // 初始化
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    sheet.setDefaultColumnWidth((short) 20); // 设置默认宽度

    try {
      wb.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16);
    } catch (Exception ex) {
      logger.error("生成表单时出错:怀疑是表单名导致。说明:poi对中文的支持不好,可能会有问题(仅表单名)如中文?--中文字加英文标点的情况");
    }

    baseCellStyle = wb.createCellStyle();
    baseCellStyle.setAlignment(align);
    baseCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));

    dateCellStyle = wb.createCellStyle();
    dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(dateFormat));

    // 创建标题行
    HSSFRow row = sheet.createRow((short) 0);

    // 创建标题
    for (int i = 0; i < nameList.size(); i++) {
      HSSFCell cell = row.createCell((short) i);
      writeCell(cell, (String) nameList.get(i));
    }

    // 创建数据
    for (int i = 0; i < dataList.size(); i++) {
      row = sheet.createRow((short) i + 1);
      for (int j = 0; j < fieldList.size(); j++) {
        HSSFCell cell = row.createCell((short) j);
        Object value = getValue(dataList.get(i), (String) fieldList.get(j));
        writeCell(cell, value);
      }
    }

    // 写入文件
    // FileOutputStream fileOut = new FileOutputStream(fileName);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    wb.write(out);
    return out.toByteArray();
    // fileOut.close();
  }
  /**
   * ファイル情報をExcelへ出力する
   *
   * @param outputFilePath 出力ファイルパス
   */
  public void makeXls(String outputFilePath) {
    FileOutputStream out = null;

    try {

      // 新規ワークブックを作成する
      HSSFWorkbook wb = new HSSFWorkbook();
      out = new FileOutputStream(outputFilePath);
      // 新規ワークシートを作成する
      HSSFSheet sheet = wb.createSheet();

      // 作成したシート名を変更
      // 日本語シート名を指定する場合には
      // エンコーディング設定は必須
      wb.setSheetName(0, "sheet1");

      // 行オブジェクトの作成(行番号は0スタート)
      HSSFRow header = sheet.createRow(0);

      // セルオブジェクトの作成(セル番号は0スタート)
      // 引数はshort型でキャストしなければならない点に注意
      HSSFCell cell1 = header.createCell(0);
      HSSFCell cell2 = header.createCell(1);
      HSSFCell cell3 = header.createCell(2);

      // セルに値を設定する
      cell1.setCellValue("テストクラス");
      cell2.setCellValue("テストメソッド");
      cell3.setCellValue("シート名");

      for (int i = 1; i < fileInfoList.size(); i++) {

        EmulFileSheetInfo fileInfo = fileInfoList.get(i);

        HSSFRow dataRow = sheet.createRow(i);
        HSSFCell c_class = dataRow.createCell(0);
        HSSFCell c_method = dataRow.createCell(1);
        HSSFCell c_sheet = dataRow.createCell(2);

        c_class.setCellValue(fileInfo.getClassName());
        c_method.setCellValue(fileInfo.getMethodName());
        c_sheet.setCellValue(toCsv(fileInfo.getSheetNameList()));
      }

      // 作成したワークブックを保存する
      wb.write(out);

    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    } finally {
      try {
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        // TODO 自動生成された catch ブロック
        e.printStackTrace();
      }
    }
  }
  /**
   * 在服务端生成Excel文件
   *
   * @param sourcedata
   * @param sheetName
   * @param fileName
   * @param path
   * @param otherParams
   */
  public void createExcel(
      List sourcedata, String sheetName, String fileName, String path, String otherParams) {
    String xmlData = "";
    long rownum = 0;
    int colnum = 0;
    String xml = "";
    String filterrow = "";
    String rows = ""; // 总行数,客户端分页时使用
    int gridType = 0; // 列表类型
    int listKind = 0; // 列表性质
    int isOperater = 0; // 是否有操作列
    int isTotalRow = 0; // 是否有统计行
    String tcolname = ""; // 统计列的列名
    int isStatusCol = 0; // 是否有行状态列
    String colVal;
    String tmp1[] = null;
    String tmp2[] = null;
    String tmp3[] = null;
    String tmp4[] = null;

    String[] colName = null; // 列名
    int[] isToalcol = null; // 对应的列是否进行合计
    double[] colTotal = null; // 对应的列的合计值

    List outXml = new ArrayList();

    long srdrows = sourcedata.size();
    System.out.println("导出数据到excel文件时查询的记录条数:" + (srdrows - 1));
    if (srdrows < 2) {
      return;
    }
    try {
      /*分析客户端提交的综合参数信息
       *如:gridType^3@@@listKind^101@@@isOperater^1@@@isTotalRow^1@@@goodsid^0,,goodsname^0,goodstype^0,goodsunit^0,,prodarea^0,,sutotal^1,,lastsudate^0
       */
      if (otherParams != null) {
        tmp1 = otherParams.split("@@@");
        for (int m = 0; m < tmp1.length; m++) {
          tmp2 = tmp1[m].split("\\^");
          if (m < (tmp1.length - 1)) {
            if (tmp2[0].equalsIgnoreCase("gridType")) {
              if (tmp2[1] == null || tmp2[1] == "") {
                gridType = 0;
                System.out.println("不能没有列表类型!");
                return;
              } else {
                gridType = Integer.valueOf(tmp2[1]).intValue();
              }

            } else if (tmp2[0].equalsIgnoreCase("listKind")) {
              if (tmp2[1] == null || tmp2[1] == "") {
                listKind = 0;
                System.out.println("不能没有列表性质!");
                return;
              } else {
                listKind = Integer.valueOf(tmp2[1]).intValue();
              }

            } else if (tmp2[0].equalsIgnoreCase("isOperater")) {
              if (tmp2[1] == null || tmp2[1] == "") {
                isOperater = 0;
              } else {
                isOperater = Integer.valueOf(tmp2[1]).intValue();
              }

            } else if (tmp2[0].equalsIgnoreCase("isTotalRow")) {
              if (tmp2[1] == null || tmp2[1] == "") {
                isTotalRow = 0;
              } else {
                isTotalRow = Integer.valueOf(tmp2[1]).intValue();
              }

            } else if (tmp2[0].equalsIgnoreCase("isStatusCol")) {
              if (tmp2[1] == null || tmp2[1] == "") {
                isStatusCol = 0;
              } else {
                isStatusCol = Integer.valueOf(tmp2[1]).intValue();
              }
            }
          } else {
            tmp3 = tmp1[m].split(",");
            // 初始化数组

            colName = new String[tmp3.length];
            isToalcol = new int[tmp3.length];
            colTotal = new double[tmp3.length];
            for (int n = 0; n < tmp3.length; n++) {
              tmp4 = tmp3[n].split("\\^");
              colName[n] = tmp4[0];
              if (tmp4[1] == null) {
                System.out.println("判断列是否进行合计时出错了!");
                return;
              }
              isToalcol[n] = Integer.valueOf(tmp4[1]).intValue();
            }
          }
        }
      }
      tmp4 = null;
      // 创建新的Excel 工作簿

      HSSFWorkbook workbook = new HSSFWorkbook();
      // 创建工作簿字体

      HSSFFont font = workbook.createFont();
      // 设置字体颜色为 ,粗体
      font.setColor(HSSFFont.COLOR_NORMAL);
      font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

      // 新建指定名称的工作表
      HSSFSheet sheet = workbook.createSheet();
      // 设置工作表的名称,字符集

      workbook.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16);
      // 创建单元格格式

      HSSFCellStyle cellstyle = workbook.createCellStyle();
      // 定义全部单元格字体样式

      cellstyle.setFont(font);
      for (int i = 0; i < sourcedata.size(); i++) {
        if (i == sourcedata.size() - 1) {
          // 总数据行数,不参与数据的导出
          Map map = (Map) sourcedata.get(i);
          Set keys = map.keySet();
          for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
            String key = (String) iter.next();
            rows = map.get(key).toString();
          }
          continue;
        }
        Map map = (LinkedHashMap) sourcedata.get(i);
        Set keys = map.keySet();
        // 记录行号
        rownum++;
        colnum = 0;
        // 在索引i的位置创建行
        HSSFRow row = sheet.createRow((short) i);

        // 增加各列的列值
        for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
          String key = (String) iter.next();
          tmp4 = key.split("\\.");
          tcolname = tmp4[1];
          // 进行指定列的列值的合计
          if (isTotalRow == 1) {
            for (int w = 0; w < colName.length; w++) {
              if (tcolname.equalsIgnoreCase(colName[w]) && isToalcol[w] == 1) {
                colTotal[w] = Double.valueOf(map.get(key).toString()).doubleValue() + colTotal[w];
              }
            }
          }
          // 列值

          colVal = (String) map.get(key).toString();
          // 去掉左右的空格

          colVal = colVal.trim();
          // 替换回车
          colVal = colVal.replaceAll("[\\n\\r]*", "");
          // 在索引0的位置创建单元格
          HSSFCell cell = row.createCell((short) colnum);
          /*
                         //创建单元格格式

          HSSFCellStyle cellstyle = workbook.createCellStyle();
          //定义全部单元格字体样式

          cellstyle.setFont(font);
          */
          // 应用单元格格式

          cell.setCellStyle(cellstyle);
          cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          // 第一个单元格中的值

          cell.setCellValue(colVal);

          // 记录列数
          colnum++;
        }
      }
      // 将Excel文件存盘
      FileOutputStream fileOut = new FileOutputStream(path + fileName);
      workbook.write(fileOut);
      fileOut.flush();
      // 导出结束,关闭输出流

      fileOut.close();

    } catch (Exception ex) {
      System.out.println("系统生成EXCEL发生异常" + ex.getMessage());
      ex.printStackTrace();
    }
  }