【发布时间】:2015-03-15 15:40:23
【问题描述】:
我正在尝试从 Excel 工作表中获取特定数据,数据是动态的。它可以是任何东西。列标题是我唯一可以用作占位符的东西,但列标题在工作表中的位置可能会有所不同。
例如,我有一张这样的表格:
|名称|姓氏|价值|
|酒吧 |便便 | 5|
|巴兹|富 | 7|
但是例如,我需要遍历工作表以获取姓氏列,然后如果我找到 surname = 'poo' 我必须拉取其对应的值,该值在工作表中位于下一个列中,但这是动态的。 surname 和 value 列并不总是彼此相邻,它们可以位于顶部的任何位置。但是,如果我在 surname 列中找到特定的“东西”,我需要提取它的值。
我已经设法遍历工作表并将所有数据存储在二维数组中并显示该数据。根据我所做的研究,这不是一种有效的方法,因为从工作表中遍历和存储大量数据会占用大量内存。我读过您可以阅读 excel 表,而不是将这些值存储在数组中,如果它们符合特定条件,您可以立即将它们写入另一张表。 EG:(伪) If(columnheader == surname && surname == foo ) 然后获取相应的值,然后将该值写入新工作表。
好的,我的问题是:
1.如何实现对工作表的迭代而不是将其存储在数组中并在匹配条件时将其直接写入另一个工作表?
2.从我下面的代码中,我如何实现对数组中的数据进行排序并查找surname = foo是否得到其对应的值?
就像我说的那样,表格中的数据是动态的,除了列标题,但作为标题的位置是动态的。
抱歉,帖子太长了,任何帮助将不胜感激。
package demo.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class test {
public static void main(String[] args) throws Exception {
File excel = new File("test.xlsx");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheetAt(0);
ws.setForceFormulaRecalculation(true);
int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
int surnameHeaderIndex = -1, valueHeaderIndex = -1;
//Read the headers first. Locate the ones you need
XSSFRow rowHeader = ws.getRow(0);
for (int j = 0; j < colNum; j++) {
XSSFCell cell = rowHeader.getCell(j);
String cellValue = cellToString(cell);
if("SURNAME".equalsIgnoreCase(cellValue)) {
surnameHeaderIndex = j;
} else if("VALUE".equalsIgnoreCase(cellValue)) {
valueHeaderIndex = j;
}
}
if(surnameHeaderIndex == -1 || valueHeaderIndex == -1) {
throw new Exception("Could not find header indexes\nSurname : " + surnameHeaderIndex + " | Value : " + valueHeaderIndex);
}
//createnew workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("data");
for (int i = 1; i < rowNum; i++) {
XSSFRow row = ws.getRow(i);
row = sheet.createRow(rowNum++);
String surname = cellToString(row.getCell(surnameHeaderIndex));
String value = cellToString(row.getCell(valueHeaderIndex));
int cellIndex = 0;
row.createCell(cellIndex++).setCellValue(surname);
row.createCell(cellIndex++).setCellValue(value);
}
FileOutputStream fos = new FileOutputStream(new File("test1.xlsx"));
workbook.write(fos);
fos.close();
}
public static String cellToString(XSSFCell cell) {
int type;
Object result = null;
type = cell.getCellType();
switch (type) {
case XSSFCell.CELL_TYPE_NUMERIC:
result = BigDecimal.valueOf(cell.getNumericCellValue())
.toPlainString();
break;
case XSSFCell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_BLANK:
result = "";
break;
case XSSFCell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
}
return result.toString();
}
}
【问题讨论】:
-
给出一个完整的例子和想要的输出。你提到排序,但我不完全明白你的意思。
-
嗨,所以基本上来自那个 excel 表。我想迭代并找到特定的值。例如,现在我得到了数组中的所有数据,我需要用它做一些事情例如:我正在寻找的输出是一旦我找到 Surname 列,找到值“foo”,当你找到 foo 时,转到值列并得到相应的数字。如果您查看演示表,“foo”的值为“7”。但是,如果列标题位置是动态的,我如何获取 foo 的位置并将其与 7 的值相关联?
标签: java excel apache apache-poi xssf