简体   繁体   中英

Read only few columns from excel sheet

I'm writing a Java program to fetch data from Excel sheet.

From the below program, i'm able to retrieve the entire data.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.ss.formula.functions.Column;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadData {

    @SuppressWarnings({ "resource", "null" })
    public static void main(String[] args) throws IOException {

        // get file
        FileInputStream fin = new FileInputStream(
                new File("C:\\A2015.xlsx"));

        // create book holding object
        XSSFWorkbook wb = new XSSFWorkbook(fin);

        // get sheet

        XSSFSheet sheet = wb.getSheetAt(0);

        // iterate through rows

        Iterator<Row> rowIt = sheet.rowIterator();

        while (rowIt.hasNext()) {
            XSSFRow row = (XSSFRow) rowIt.next();

            // iterate through Columns

            Iterator<Cell> colIt = row.cellIterator();
            while (colIt.hasNext()) {
                Cell cell = colIt.next();
                System.out.println(cell.toString());
            }
            System.out.println();

        }

    }
}

But here my case is there are nearly 45-47 columns and out of there there is some data which is not required(for me, but needed for some other teams). Every column has a heading, and out of these 45-47 columns i want to pull data only from 12 columns, and there are randomly placed between the rest of columns in Excel sheet.

My question is, is there a way to iterate through all the rows and get data from these 12 columns only by using the Heading, If so can you please let me know how to extract it.

I'm using Apache POI.

Thanks

    public static short getCellNum(String cellCode)throws InvalidNameException{
       char[] cellCodeU = cellCode.toUpperCase().toCharArray();
       int length = cellCodeU.length;
       int cellNumber = 0;
       for (int j=0;j<length;j++){
          if (cellCodeU[j]<'A' || cellCodeU[j]>'Z')
            throw new InvalidNameException("Wrong column index: " + cellCode);
          cellNumber = cellNumber*CELL_NUMBER_IN_SHEET + (cellCodeU[j]-64);
       }
       cellNumber-=1;
       if (cellNumber<0) 
          throw new InvalidNameException("Wrong column index: " + cellCode);
       return (short)cellNumber;
    }


 String columnsToRead = new String[]{"AA", "AB", "AU"};

 while (rowIt.hasNext()) {
        XSSFRow row = (XSSFRow) rowIt.next();
        for (int a = 0; a < columnsToRead.length; a++){
            Cell cell = getCell(getCellNum(columnsToRead[a]));
            System.out.println(cell.toString());   
        }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM