2012-10-06 45 views
2

我有一張Excel工作表,其中有近150列。我寫一個程序,其中在將取回一列,其中列名是說X.在另一個線程我看到了如何讀取XL工作簿和片材..採寫以下代碼..使用Java程序從Excel文件中獲取列X

 HSSFSheet sheet = workbook.getSheetAt(0); 
     Iterator rows = sheet.rowIterator(); 


     while (rows.hasNext()) 
     { 
      HSSFRow row = (HSSFRow) rows.next(); 
      Iterator cells = row.cellIterator(); 

      List data = new ArrayList(); 
      while (cells.hasNext()) 
      { 
       HSSFCell cell = (HSSFCell) cells.next(); 
       data.add(cell); 
      } 

      sheetData.add(data); 
     } 
    } 

所以如何從列表中提取確切的列,列名爲..?

+0

那麼問題是什麼? – arshajii

+0

我如何只在列X下獲取數據。我不想讀通過XL表中的所有列..是否有一個API來這樣做? – Prat

+0

負面價值的任何理由?這裏的規則是什麼? – Prat

回答

8

的Apache POI API HSSFSheet是基於行的,你需要通過itteration提取列數據,下面的鏈接可能會回答你的問題:

Extracting data in spreadsheet columns in Apache POI API

代碼修改,第一個工作表

的第1行至搜索字符串
package projectTest.test; 

import java.io.FileInputStream; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.poifs.filesystem.POIFSFileSystem; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 

public class Poi { 


    public static void main(String[] args) throws Exception { 
    //test file is located in your project path   
    FileInputStream fileIn = new FileInputStream("test.xls"); 
    //read file 
    POIFSFileSystem fs = new POIFSFileSystem(fileIn); 
    HSSFWorkbook filename = new HSSFWorkbook(fs); 
    //open sheet 0 which is first sheet of your worksheet 
    HSSFSheet sheet = filename.getSheetAt(0); 

    //we will search for column index containing string "Your Column Name" in the row 0 (which is first row of a worksheet 
    String columnWanted = "Your Column Name"; 
    Integer columnNo = null; 
    //output all not null values to the list 
    List<Cell> cells = new ArrayList<Cell>(); 

    Row firstRow = sheet.getRow(0); 

    for(Cell cell:firstRow){ 
     if (cell.getStringCellValue().equals(columnWanted)){ 
      columnNo = cell.getColumnIndex(); 
     } 
    } 


    if (columnNo != null){ 
    for (Row row : sheet) { 
     Cell c = row.getCell(columnNo); 
     if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) { 
      // Nothing in the cell in this row, skip it 
     } else { 
      cells.add(c); 
     } 
    } 
    }else{ 
     System.out.println("could not find column " + columnWanted + " in first row of " + fileIn.toString()); 
    } 

    } 
} 
+0

是的,已經檢查過。但是給出了列號的結果。但如何根據列名來做到這一點? – Prat

+0

@ user1122891我修改了代碼來搜索列的字符串名稱(在工作簿中的1工作表的一行中) –

+0

謝謝。它爲我工作。 – Prat

0

這是非常簡單和高效的代碼並且按照預期工作

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 

public class TestExcelFile { 

    public static void main(String[] args) { 
     String envFilePath = System.getenv("AZURE_FILE_PATH"); 

     // upload list of files/directory to blob storage 
     File folder = new File(envFilePath); 
     File[] listOfFiles = folder.listFiles(); 

     for (int i = 0; i < listOfFiles.length; i++) { 
      if (listOfFiles[i].isFile()) { 
       System.out.println("File " + listOfFiles[i].getName()); 

       Workbook workbook; 
       //int masterSheetColumnIndex = 0; 
       try { 
         workbook = WorkbookFactory.create(new FileInputStream(envFilePath + "\\"+ listOfFiles[i].getName())); 
         // Get the first sheet. 
         Sheet sheet = workbook.getSheetAt(0); 
         // Get the first cell. 
         Row row = sheet.getRow(0); 
         //Cell cell = row.getCell(0); 
         for (Cell cell1 : row) { 
          // Show what is being read. 
          System.out.println(cell1.toString()); 
          //masterSheetColumnIndex++; 
         } 
         //we will search for column index containing string "Your Column Name" in the row 0 (which is first row of a worksheet 
         String columnWanted = "Modality Name"; 
         Integer columnNo = null; 
         //output all not null values to the list 
         List<Cell> cells = new ArrayList<Cell>(); 

         Row firstRow = sheet.getRow(0); 

         for(Cell cell1:firstRow){ 
          if (cell1.getStringCellValue().equals(columnWanted)){ 
           columnNo = cell1.getColumnIndex(); 
          } 
         } 

         if (columnNo != null){ 
          for (Row row1 : sheet) { 
           Cell c = row1.getCell(columnNo); 
           if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) { 
            // Nothing in the cell in this row, skip it 
           } else { 
            cells.add(c); 
           } 
          } 
          System.out.println(""); 
         }else{ 
          System.out.println("could not find column " + columnWanted + " in first row of " + listOfFiles[i].getName()); 
         } 

       } catch (InvalidFormatException | IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
相關問題