此案例使用: org.apache.poi 的jar
請先到網路官網上下載: http://www.apache.org/dyn/closer.cgi#verify
OK後就將:poi-3.10-FINAL-20140208(隨著日期更新檔名不一樣)放入lib裡就可使用如下:
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;
下面開始key code:
public class test_exlImport {
public static void main(String[] args) throws Exception {
// C:\201304WorkJavaPlace\\Member_test\\WebContent\\updata\\testExl.xlsx
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
"C:\\201304WorkJavaPlace\\Member_test\\WebContent\\updata\\testExl.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short) 0);
String msg = cell.getStringCellValue();
System.out.println(msg);
// 取最後一行數
System.out.println("最後一行: " + sheet.getLastRowNum());
// 取指定行數的最後一列數
System.out.println("共列: " + row.getLastCellNum());
// 取指定行數的某一列的數
System.out.println("answer1: "+ row.getCell(0) );
System.out.println("answer2: "+ row.getCell(1) );
System.out.println("answer3: "+ row.getCell(2) );
System.out.println("answer4: "+ row.getCell(3) );
HSSFRow row2 = sheet.getRow(0);
Iterator iter = row2.iterator();
while(iter.hasNext())
{
System.out.println(iter.next());
}
//Iterator rowTopTotal = sheet.;
HSSFCell cell2 = row.getCell((short) 0);
String msg2= cell.getStringCellValue();
System.out.println(msg2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上 可以試試~~
參考網址:
1. http://fecbob.pixnet.net/blog/post/38190553-%5Bjsp%5D-%E8%B3%87%E6%96%99%E5%BA%AB%E8%BD%89excel%EF%BC%88poi%E5%AF%A6%E7%8F%BE%EF%BC%89
2. http://wenku.baidu.com/view/1e3073ec4afe04a1b071de78.html
用途: 常用的檔案上傳excel 匯入到資料庫內
留言列表