2

我试图根据一些独特的价值来获得记录。

例子; 它有一个带有emp名称,emp id,emp地址的值表,我想根据emp id获取记录。

这是我尝试过的代码,也是从文件中读取数据的代码,而不是从头开始。

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);
    }
}

这段代码像这样打印出来。

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 表如下所示:

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                    

谢谢安东尼

4

1 回答 1

3

假设您要获取其 的员工eid = 1,那么您可以在打印时进行检查,console. 因为您不知道哪一行包含 eid=1 。所以您必须解析每一行。

的索引eid就像2从输出中看到的那样。

因此可以执行以下更改。

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();
        }
    }
于 2012-05-17T06:34:43.990 回答