java - 属性文件到 Excel 文件

标签 java apache-poi

我们有一个属性文件,我想从 Excel 工作簿中的属性文件获取数据!问题是,我只有一个属性文件,几乎所有数据都在那里,现在我需要过滤该数据并将其存储在 Excel 工作簿中的不同工作表上!是否可以?我需要 5 张,我创建了 5 张,我将属性文件拆分为字符串数组!问题是,我需要编写相同的代码 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,通过它我将数据分割成表!

最佳答案

The problem is, I need to write same code 5 times!

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

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 ....
}

关于java - 属性文件到 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47987046/

相关文章:

Java正则表达式获取零件号

java - 链表设置引用

java - 如何通过 Apache POI 设置 Excel Sunburst Chart 中各个数据标签的文本属性?

java - 从逗号分隔的文本文件中读取数组

java - 开发Eclipse插件时出现ClassNotFoundException

excel - 如何使用POI获取excel中不同单元格值的总和

java - 无法将 XSSF 与 Excel 2007 一起使用

java - 通过 quartz 多次执行后,Java Excel POI停止

java - 如何使用java检查Excel文件是否有更新

java - 在 Java 中抛出多个检查异常有什么问题?