属性文件到excel文件

问题描述 投票:0回答:1

我们有一个属性文件,我想从excel工作簿中的属性文件中获取数据!问题是,我只有一个属性文件,其中几乎所有数据都存在,现在我需要过滤该数据并将其存储在excel工作簿中的不同工作表上!可能吗?我需要5张,我创建了5张,我将属性文件分割成String数组!问题是,我需要编写相同的代码5次!谁能帮我吗?我正在使用XSSFWorkbook!

  while ((str = br.readLine()) != null) {
    fullData = fullData + "\n" + str;
  }
  String[] sheetData = fullData.split("//Split");
  int rows = 0, cols = 0;
  for (String details : sheetData) {
    String name = "", value = "";
    if (details.contains("Home Page")) {
      lines = details.split("\n");
      for (int j = 0; j < lines.length; j++) {
        if (lines[j].contains("=")) {
          int lrows = sheet1.getLastRowNum();
          row = sheet1.createRow(lrows + 1);
          name = lines[j].substring(0, lines[j].indexOf("="));
          value = lines[j].substring(lines[j].indexOf("=") + 1);
          for (int i = 0; i < 2; i++) {
            col = row.createCell(cols);
            col.setCellValue(name);
            col = row.createCell(cols + 1);
            col.setCellValue(value);  
          }  
          cols = 0;
        }  
      }
    }
    else if (details.contains("ManualTest") || details.contains("Expand camera")) {

      lines = details.split("\n");
      for (int j = 0; j < lines.length; j++) {
        if (lines[j].contains("=")) {
          int lrows = sheet3.getLastRowNum();
          row = sheet3.createRow(lrows + 1);
          name = lines[j].substring(0, lines[j].indexOf("="));
          value = lines[j].substring(lines[j].indexOf("=") + 1);
          for (int i = 0; i < 2; i++) {
            col = row.createCell(cols);
            col.setCellValue(name);
            col = row.createCell(cols + 1);
            col.setCellValue(value);

          }
          rows++;
          cols = 0;
        }  
      }
    }
  }

这是2张,像这样,我有5张!我使用了一行// split,我将数据拆分成表格!

java apache-poi
1个回答
1
投票

问题是,我需要编写相同的代码5次!

如果使用方法,则不必编写整个代码五次:

private void fillSheet(String[] lines, HSSFSheet sheet) {
    for (int j = 0; j < lines.length; j++) {
        if (lines[j].contains("=")) {
            int cols = 0;
            int lrows = sheet.getLastRowNum();
            int row = sheet.createRow(lrows + 1);
            String name = lines[j].substring(0, lines[j].indexOf("="));
            String value = lines[j].substring(lines[j].indexOf("=") + 1);
            for (int i = 0; i < 2; i++) {
                HSSFCell col = row.createCell(cols);
                col.setCellValue(name);
                col = row.createCell(cols + 1);
                col.setCellValue(value);
            }
            cols = 0;
        }
    }
}

并像这样使用它:

while ((str = br.readLine()) != null) {
    fullData = fullData + "\n" + str;
}
String[] sheetData = fullData.split("//Split");
for (String details : sheetData) {
      String[] lines = details.split("\n");
      if (details.contains("Home Page")) {
        fillSheet(lines, sheet1);
    }
    else if (details.contains("ManualTest") || details.contains("Expand camera")) {
        fillSheet(lines, sheet3);
    }
      // else if ....
}
© www.soinside.com 2019 - 2024. All rights reserved.