標籤:excel poi java
java中使用poi開放的api來操作excel中的資料
官網:http://poi.apache.org/
寫了一個java excel的demo,代碼如下;
package com.lc_kykz.test;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.StringTokenizer;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;/* * @Author : Gavin * @Function: 讀取excel並操作資料 * @Date : 2015-6-18 */public class ExcelReader {// 建立檔案輸入資料流private BufferedReader reader = null;// 檔案類型private String filetype;// 檔案二進位輸入資料流private InputStream is = null;// 當前的Sheetprivate int currSheet;// 當前位置private int currPosition;// Sheet數量private int numOfSheets;// HSSFWorkbookHSSFWorkbook workbook = null;// 設定Cell之間以空格分割private static String EXCEL_LINE_DELIMITER = ",";// 建構函式建立一個ExcelReaderpublic ExcelReader(String inputfile) throws IOException, Exception {// 判斷參數是否為空白或沒有意義if (inputfile == null || inputfile.trim().equals("")) {throw new IOException("NO input file!!!");}// 取得檔案名稱的尾碼名賦值給filetypefiletype = inputfile.substring(inputfile.lastIndexOf(".") + 1);// 設定開始行為0currPosition = 0;// 設定當前位置為0currSheet = 0;// 建立檔案輸入資料流is = new FileInputStream(inputfile);// 判斷檔案格式if (filetype.equalsIgnoreCase("xls")) {// 如果是Excel檔案則建立HSSFWorkbook讀取workbook = new HSSFWorkbook(is);// 設定Sheet數numOfSheets = workbook.getNumberOfSheets();} else {throw new Exception("File Type incorrrect!");}}// 用於讀取檔案的一行public String readLine() throws IOException {// excel檔案通過poi讀取檔案if (filetype.equalsIgnoreCase("xls")) {// 根據currSheet值獲得當前的sheetHSSFSheet sheet = workbook.getSheetAt(currSheet);// 判斷當前行是否到但前Sheet的結尾if (currPosition > sheet.getLastRowNum()) {// 當前行位置清零currPosition = 0;// 判斷是否還有Sheetwhile (currSheet < numOfSheets - 1) {// 得到下一張SheetcurrSheet += 1;sheet = workbook.getSheetAt(currSheet);// 當前行數是否已經到達檔案末尾if (currPosition > sheet.getLastRowNum()) {// 當前Sheet指向下一張SheetcurrSheet++;continue;} else {// 擷取當前行數int row = currPosition;currPosition++;// 讀取當前行資料return getLine(sheet, row);}}return null;}// 擷取當前行數int row = currPosition;currPosition++;// 讀取當前行資料return getLine(sheet, row);}return null;}// 返回Sheet的一行資料private String getLine(HSSFSheet sheet, int row) {// 根據行數取得Sheet的一行HSSFRow rowline = sheet.getRow(row);// 建立字元創緩衝區StringBuffer buffer = new StringBuffer();// 擷取當前行的列數int filledColumns = rowline.getLastCellNum();HSSFCell cell = null;// 迴圈遍曆所有列for (int i = 0; i < filledColumns; i++) {// 取得當前Cellcell = rowline.getCell((short) i);String cellvalue = null;if (cell != null) {// 判斷當前Cell的Typeswitch (cell.getCellType()) {// Cell的Type為NUMERICcase HSSFCell.CELL_TYPE_NUMERIC: {// 判斷當前的cell是否為Dateif (HSSFDateUtil.isCellDateFormatted(cell)) {// 如果是Date類型則,取得該Cell的Date值Date date = cell.getDateCellValue();// 把Date轉換成本地格式的字串cellvalue = cell.getDateCellValue().toLocaleString();} else { // 如果是純數字// 取得當前Cell的數值,強制轉換為intInteger num = new Integer((int) cell.getNumericCellValue());cellvalue = String.valueOf(num);}break;}// Cell的Type為STRINGcase HSSFCell.CELL_TYPE_STRING:// 取得當前的Cell字串cellvalue = cell.getStringCellValue().replaceAll("'", "''");break;// 預設的Cell值default:cellvalue = " ";}} else {cellvalue = "";}// 在每個欄位之間插入分割符buffer.append(cellvalue).append(EXCEL_LINE_DELIMITER);// 去除最後一個分隔字元buffer.substring(0, buffer.length() - 1);}return buffer.toString();}// 關閉資源public void closeResource() {try {if (is != null) is.close();} catch (IOException e) {e.printStackTrace();}finally{try {if (reader != null) reader.close();} catch (IOException e) {e.printStackTrace();}}}// 測試casepublic static void main(String[] args) {try {ExcelReader er = new ExcelReader("d://Gavin/xxx.xls");String line = er.readLine();//得到excel資料後,寫處理資料的代碼就可以了,如:插入資料庫等等} catch (Exception e) {e.printStackTrace();}}}
java讀取excel並操作資料