2013-04-05 7 views
0

我正在使用servlet並試圖讀取用戶上傳的excel文件並插入到數據庫中。 我的Excel是這種格式: ID IP1 IP2用戶TKTNO(這是在Excel &數據庫表以及標題)如何讀取excel文件並使用java和poi或任何其他庫管理程序將這些數據插入到數據庫中?

下的標題,我有在Excel文件中的數據我已經閱讀並插入到數據庫中。 請迫切需要幫助....謝謝

+0

有從Excel中讀取數據的庫,但如果你能得到你的用戶將excel文件另存爲CSV或製表符分隔值tex文件更容易。不需要任何庫,並且有很多Java解析CSV數據的例子。將數據存入數據庫實際上是一個單獨的問題,答案取決於您正在使用哪種數據庫。 – Thorn 2013-04-05 13:28:07

回答

0

你這是怎麼使用Apache POI庫讀取Excel文件,我猜這是不夠好,對於初學者來說,現在你可以根據需要採取存儲在某些集合對象的單元格值和對象存儲到數據庫

package com.Excel; 

import java.io.*; 
import java.util.Iterator; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class ReadExcelFile { 
    public static void main(String[] args) 
    { 
     try { 

      FileInputStream file = new FileInputStream(new File("C:/Users/hussain.a/Desktop/mar_25/Tradestation_Q4 Dashboard_Week 5_1029-1104.xlsx")); 
      XSSFWorkbook workbook = new XSSFWorkbook(file); 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      Iterator<Row> rowIterator = sheet.iterator(); 
      rowIterator.next(); 
      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(); 
        switch(cell.getCellType()) 
        { 
         case Cell.CELL_TYPE_BOOLEAN: 
          System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t"); 
          break; 
         case Cell.CELL_TYPE_NUMERIC: 
          System.out.println("numeric===>>>"+cell.getNumericCellValue() + "\t"); 
          break; 
         case Cell.CELL_TYPE_STRING: 
          System.out.println("String===>>>"+cell.getStringCellValue() + "\t"); 
          break; 
        } 
       } 
       System.out.println(""); 
      } 
      file.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
相關問題