2013-04-01 40 views

回答

0
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Iterator; 

import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
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.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class Test { 

    public static void main(String[] args) throws IOException { 

     try { 

      FileInputStream file = new FileInputStream(new File("C:\\test.xls")); 

      HSSFWorkbook workbook = new HSSFWorkbook(file); 
      HSSFSheet sheet = workbook.getSheetAt(0); 
      //Iterate through each rows from first sheet 
      Iterator<Row> rowIterator = sheet.iterator(); 
      while(rowIterator.hasNext()) { 
       Row row = rowIterator.next(); 

       //For each row, iterate through each columns 
       Iterator<Cell> cellIterator = row.cellIterator(); 
       while(cellIterator.hasNext()) { 

        Cell cell = cellIterator.next(); 
        System.out.print(cell.getStringCellValue() + "\t\t"); 
        FileOutputStream outFile =new FileOutputStream(new File("C:\\"+cell.getStringCellValue())); 
        workbook.write(outFile); 
       } 
       System.out.println(""); 
      } 
      file.close(); 

      /* FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls")); 
      workbook.write(outFile);*/ 
      //outFile.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

嘿,那真的很有幫助。非常感謝... :) – GanStack

0

有一些庫可以用來讀取excel文件。在我的情況下,我選擇了Apache POI,您可以免費下載,然後添加到您的項目中。一旦你添加它,創建文檔:

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("filename.xls")); 

,或者如果它是從2007年或更高版本的Excel文件:

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("filename.xlsx")); 

現在,你可以通過每一行迭代,並讀出第一列:

for(Row row : workbook.getSheetAt(0)) // Go through each row in sheet 0 
    System.out.println(row.getCell(0).getStringCellValue()); 
+0

感謝您的寶貴意見。我會利用它.. :) – GanStack

0

公共類的測試{

public static void main(String[] args) throws IOException { 

    try { 

     FileInputStream file = new FileInputStream(new File("D:\\sampleFileNames.xls")); 

     HSSFWorkbook workbook = new HSSFWorkbook(file); 
     FileOutputStream outFile= null; 
     HSSFSheet sheet = workbook.getSheetAt(0); 
     //Iterate through each rows from first sheet 
     Iterator<Row> rowIterator = sheet.iterator(); 
     while(rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 
      //For each row, iterate through each columns 
      Iterator<Cell> cellIterator = row.cellIterator(); 
        Cell cell = cellIterator.next(); 
        File file1 = new File("D:\\Ganesh\\"+cell.getStringCellValue().toString()); 
        if(!file1.createNewFile()) { 
         System.out.println("File already exists"); 


       /* Cell cell = cellIterator.next(); 
       System.out.print(cell.getStringCellValue() + "\t\t"); 
       outFile =new FileOutputStream(new File("D:\\Ganesh\\"+cell.getStringCellValue().toString())); 
       //workbook.write(outFile); 
      System.out.println(""); 
     } 
     file.close(); 
     outFile.close(); 
     /* FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls")); 
     workbook.write(outFile);*/ 
     //outFile.close(); 


} 
     } 
     }catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

} }