如何使用 POI 从基于 java 中的某些唯一值的 excel 中读取特定行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10630749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read a specific row from the excel based some unique value in java using POI?
提问by CodeNotFound
i am trying to get the record based on some unique value.
我试图根据一些独特的价值来获得记录。
Example; It there is a table of values with emp name, emp id, emp address, i want to fetch the record based on the emp id.
例子; 它有一个包含 emp 名称、emp id、emp 地址的值表,我想根据 emp id 获取记录。
this the code i have tried also this code reads data from the file not from beginning.
这是我尝试过的代码,也是此代码从文件中读取数据而不是从头开始。
package org.xlsx.read;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 015 * @author giftsam 016
*/
public class ExcelSheetReader {
/**
* 021 * This method is used to read the data's from an excel file. 022 * @param
* fileName - Name of the excel file. 023
*/
private void readExcelFile(String fileName) {
/**
* Create a new instance for cellDataList
*/
List cellDataList = new ArrayList();
try {
/**
* 033 * Create a new instance for FileInputStream class 034
*/
FileInputStream fileInputStream = new FileInputStream(fileName);
/**
* Create a new instance for POIFSFileSystem class
*/
POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
/*
* 043 * Create a new instance for HSSFWorkBook Class 044
*/
HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
HSSFSheet hssfSheet = workBook.getSheetAt(0);
/**
* Iterate the rows and cells of the spreadsheet 050
* * to get
* all the datas
*/
Iterator rowIterator = hssfSheet.rowIterator();
while (rowIterator.hasNext()) {
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
Iterator iterator = hssfRow.cellIterator();
List cellTempList = new ArrayList();
while (iterator.hasNext()) {
HSSFCell hssfCell = (HSSFCell) iterator.next();
cellTempList.add(hssfCell);
}
cellDataList.add(cellTempList);
}
} catch (Exception e) {
e.printStackTrace();
}
/**
* Call the printToConsole method to print the cell data in the console.
*/
printToConsole(cellDataList);
}
/**
* This method is used to print the cell data to the console.
*
* @param cellDataList
* - List of the data's in the spreadsheet.
*/
private void printToConsole(List cellDataList) {
for (int i = 0; i < cellDataList.size(); i++) {
List cellTempList = (List) cellDataList.get(i);
for (int j = 0; j < cellTempList.size(); j++) {
HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
String stringCellValue = hssfCell.toString();
System.out.print(stringCellValue + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
String fileName = "D:/workspace/ReadExlsx/xlsx/Access_Control_Report_01-04-2012to30-04-2012.xls";
new ExcelSheetReader().readExcelFile(fileName);
}
}
this code prints out put like this.
这段代码打印出来是这样的。
292 Manikiruban Jaganathan 001 Esg 05/04/2012 09:11:28 19:06:06
292 Manikiruban Jaganathan 001 Esg 06/04/2012 09:01:07 19:02:37
292 Manikiruban Jaganathan 001 Esg 09/04/2012 09:02:08 19:39:29
292 Manikiruban Jaganathan 001 Esg 10/04/2012 09:13:20 18:54:05
292 Manikiruban Jaganathan 001 Esg 11/04/2012 09:31:55 18:33:03
my excel sheet looks like this :
我的 Excel 表格如下所示:
Sling Media Pvt Ltd
First and Last Record Details Report From 01/04/2012 to 30/04/2012
Report Prepared on: 10/05/2012 at 12:51
EmpID EmpName SiteCode Department Type Date First Last Location
1 Kr Veerappan 123 Hardware Team 02/04/2012 10:30:39 20:23:14
1 Kr Veerappan 123 Hardware Team 03/04/2012 09:46:01 19:58:16
1 Kr Veerappan 123 Hardware Team 04/04/2012 10:06:26 19:58:31
1 Kr Veerappan 123 Hardware Team 05/04/2012 11:38:51 19:32:17
1 Kr Veerappan 123 Hardware Team 06/04/2012 10:43:06 21:02:06
1 Kr Veerappan 123 Hardware Team 09/04/2012 11:02:32 22:05:14
1 Kr Veerappan 123 Hardware Team 10/04/2012 10:49:41 19:07:48
1 Kr Veerappan 123 Hardware Team 11/04/2012 10:16:42 20:30:13
1 Kr Veerappan 123 Hardware Team 12/04/2012 10:15:54 19:54:51
1 Kr Veerappan 123 Hardware Team 13/04/2012 10:18:30 19:05:55
1 Kr Veerappan 123 Hardware Team 18/04/2012 10:52:37 18:50:36
1 Kr Veerappan 123 Hardware Team 19/04/2012 10:24:59 20:29:59
1 Kr Veerappan 123 Hardware Team 20/04/2012 10:29:47 18:58:23
1 Kr Veerappan 123 Hardware Team 23/04/2012 10:24:31 20:11:29
1 Kr Veerappan 123 Hardware Team 24/04/2012 10:13:18 19:05:25
thanks Antony
谢谢安东尼
回答by vikiiii
Suppose you want to fetch the employee whose eid = 1
, then you can put a check while you print it on console.
Since you dont know which row contains the eid=1 .So you have to parse each row.
假设您要获取其 的员工eid = 1
,那么您可以在打印时进行检查,console.
因为您不知道哪一行包含 eid=1 。所以您必须解析每一行。
The index of eid
is 2
as it seems from the output.
的指标eid
是2
因为它从输出看起来。
So following changes can be performed.
因此可以执行以下更改。
private void printToConsole(List cellDataList) {
for (int i = 0; i < cellDataList.size(); i++) {
List cellTempList = (List) cellDataList.get(i);
if((HSSFCell) cellTempList.get(2).toString().equals("1"))
{
for (int j = 0; j < cellTempList.size(); j++) {
HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
String stringCellValue = hssfCell.toString();
System.out.print(stringCellValue + "\t");
}
}
else {
i++;
}
System.out.println();
}
}