如何使用POI从Excel中基于Java的一些唯一值的Excel中读取特定行? [英] how to read a specific row from the excel based some unique value in java using POI?
问题描述
我正在尝试根据某些唯一值获取记录.
i am trying to get the record based on some unique value.
示例;它有一个带有emp名称,emp id,emp地址的值表,我想根据emp id来获取记录.
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.
这是我尝试过的代码,也是这段代码不是从头开始读取文件中的数据.
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
我的Excel工作表如下:
my excel sheet looks like this :
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
谢谢 安东尼
推荐答案
假设您要获取eid = 1
的员工,则可以在将其打印在console.
上时进行支票
由于您不知道哪一行包含eid = 1,因此您必须解析每一行.
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
的索引是2
.
The index of eid
is 2
as it seems from the output.
因此可以执行以下更改.
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();
}
}
这篇关于如何使用POI从Excel中基于Java的一些唯一值的Excel中读取特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!