gpt4 book ai didi

java - 属性文件到 Excel 文件

转载 作者:行者123 更新时间:2023-12-02 11:49:26 25 4
gpt4 key购买 nike

我们有一个属性文件,我想从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com