如何从 excel sheet 中检索某些特定的行和列?
How to retrieve some specific rows and columns from an excel sheet?
- 我正在使用 java (Apache POI) 读取 xlsx 文件。
- 我创建了一个文档 class(将所有 excel 列标题作为变量)
- 我必须读取 excel 中的每一行并通过创建文档 class.
的 collection 映射到文档 class
我面临的问题是我必须从第 2 行和第 7 列开始读取到第 35 列并将相应的值映射到文档 class.
无法弄清楚代码应该是怎样的?
- 我写了下面几行代码。
List sheetData = new ArrayList();
InputStream excelFile = new BufferedInputStream(new FileInputStream("D:\Excel file\data.xlsx"));
Workbook workBook = new XSSFWorkbook(excelFile); // Creates Workbook
XSSFSheet sheet = (XSSFSheet) workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();
for (int i = 7; i <= 35; i++) {
XSSFRow row = sheet.getRow(i);
Cell cell = row.getCell(i);
String val = formatter.formatCellValue(cell);
sheetData.add(val);
}
您可以在文档中使用 Iterator 进行迭代,但还有一个函数 "getRow() and getCell()"
Workbook workbook = new XSSFWorkbook(excelFile);
// defines the standard pointer in document in the first Sheet
XSSFSheet data = this.workbook.getSheetAt(0);
// you could iterate the document with an iterator
Iterator<Cell> iterator = this.data.iterator();
// x/y pointer at the document
Row row = data.getRow(y);
Cell pointingCell = row.getCell(x);
String pointingString = pointingCell.getStringCellValue();
假设我正确理解了你的问题,我相信你想要处理从第 2 行开始到文件末尾存在的每一行,并且对于这些行中的每一行,请考虑第 7 列到第 35 列中的单元格。我相信您可能还需要处理这些值,但您没有说明如何处理,因此对于这个示例,我将把它们填充到一个字符串列表中并希望最好...
这是基于Apache POI documentation for iterating over rows and cells
File excelFile = new File("D:\Excel file\data.xlsx");
Workbook workBook = WorkbookFactory.create(excelFile);
Sheet sheet = workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();
// Start from the 2nd row, processing all to the end
// Note - Rows and Columns in Apache POI are 0-based not 1-based
for (int rn=1; rn<=sheet.getLastRowNum(); rn++) {
Row row = sheet.getRow(rn);
if (row == null) {
// Whole row is empty. Handle as required here
continue;
}
List<String> values = new ArrayList<String>();
for (int cn=6; cn<35; cn++) {
Cell cell = row.getCell(cn);
String val = null;
if (cell != null) { val = formatter.formatCellValue(cell); }
if (val == null || val.isEmpty()) {
// Cell is empty. Handle as required here
}
// Save the value to list. Save to an object instead if required
values.append(val);
}
}
workBook.close();
根据您的业务需求,加入处理空白行和单元格的逻辑。然后,再次根据您的业务需求,对您找到的值做任何您需要做的事情!
- 我正在使用 java (Apache POI) 读取 xlsx 文件。
- 我创建了一个文档 class(将所有 excel 列标题作为变量)
- 我必须读取 excel 中的每一行并通过创建文档 class. 的 collection 映射到文档 class
我面临的问题是我必须从第 2 行和第 7 列开始读取到第 35 列并将相应的值映射到文档 class.
无法弄清楚代码应该是怎样的?
- 我写了下面几行代码。
List sheetData = new ArrayList();
InputStream excelFile = new BufferedInputStream(new FileInputStream("D:\Excel file\data.xlsx"));
Workbook workBook = new XSSFWorkbook(excelFile); // Creates Workbook
XSSFSheet sheet = (XSSFSheet) workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();
for (int i = 7; i <= 35; i++) {
XSSFRow row = sheet.getRow(i);
Cell cell = row.getCell(i);
String val = formatter.formatCellValue(cell);
sheetData.add(val);
}
您可以在文档中使用 Iterator 进行迭代,但还有一个函数 "getRow() and getCell()"
Workbook workbook = new XSSFWorkbook(excelFile);
// defines the standard pointer in document in the first Sheet
XSSFSheet data = this.workbook.getSheetAt(0);
// you could iterate the document with an iterator
Iterator<Cell> iterator = this.data.iterator();
// x/y pointer at the document
Row row = data.getRow(y);
Cell pointingCell = row.getCell(x);
String pointingString = pointingCell.getStringCellValue();
假设我正确理解了你的问题,我相信你想要处理从第 2 行开始到文件末尾存在的每一行,并且对于这些行中的每一行,请考虑第 7 列到第 35 列中的单元格。我相信您可能还需要处理这些值,但您没有说明如何处理,因此对于这个示例,我将把它们填充到一个字符串列表中并希望最好...
这是基于Apache POI documentation for iterating over rows and cells
File excelFile = new File("D:\Excel file\data.xlsx");
Workbook workBook = WorkbookFactory.create(excelFile);
Sheet sheet = workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();
// Start from the 2nd row, processing all to the end
// Note - Rows and Columns in Apache POI are 0-based not 1-based
for (int rn=1; rn<=sheet.getLastRowNum(); rn++) {
Row row = sheet.getRow(rn);
if (row == null) {
// Whole row is empty. Handle as required here
continue;
}
List<String> values = new ArrayList<String>();
for (int cn=6; cn<35; cn++) {
Cell cell = row.getCell(cn);
String val = null;
if (cell != null) { val = formatter.formatCellValue(cell); }
if (val == null || val.isEmpty()) {
// Cell is empty. Handle as required here
}
// Save the value to list. Save to an object instead if required
values.append(val);
}
}
workBook.close();
根据您的业务需求,加入处理空白行和单元格的逻辑。然后,再次根据您的业务需求,对您找到的值做任何您需要做的事情!