生活随笔
收集整理的這篇文章主要介紹了
java读取Excel2003和Excel2007内容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java讀取Excel2003和Excel2007內容并可以控制是否讀取全部sheet頁內容
在http://download.csdn.net/detail/u010792467/8072015下載所需要的包
如果需要excel2003和excel2007文件可以去
http://download.csdn.net/detail/u010792467/8072009下載
<span style="font-size:18px;">package Excel;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
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.XSSFWorkbook;public class ImportExecl {// 錯誤信息private String errorInfo;// 錯誤信息private static int readSheet = 0;private static boolean readSheetNum = false;public static void main(String[] args) throws Exception {// readSheetNum = true 開啟自定義讀取sheet頁 默認false(讀取全部sheet頁)readSheetNum = true;// readSheet默認為0(讀取第一頁) 值為2時讀取第二頁readSheet = 1;ImportExecl poi = new ImportExecl();// 2003// List<List<String>> list = poi.readExcel("D:\\EXCEL2003測試.xls");// 2007List<List<String>> list = poi.readExcel("D:\\EXCEL2007測試.xlsx");if (list != null) {for (int i = 0; i < list.size(); i++) {System.out.print("第" + (i) + "行");List<String> listCell = list.get(i);for (String s : listCell) {System.out.print(" " + s);}System.out.println();}}}// 驗證excel文件public boolean validateExcel(String filePath) {// 檢查文件名是否為空或者是否是Excel格式的文件if (filePath == null|| !(is2003Excel(filePath) || is2007Excel(filePath))) {errorInfo = "文件名不是excel格式";return false;}// 檢查文件是否存在File file = new File(filePath);if (file == null || !file.exists()) {errorInfo = "excel文件不存在";return false;}return true;}// 根據文件名讀取excel文件public List<List<String>> readExcel(String filePath) {List<List<String>> dataList = new ArrayList<List<String>>();InputStream is = null;try {// 驗證文件是否合法if (!validateExcel(filePath)) {System.out.println(errorInfo);return null;}// 判斷文件的類型,是2003還是2007boolean is2003Excel = true;if (is2007Excel(filePath)) {is2003Excel = false;}// 調用本類提供的根據流讀取的方法File file = new File(filePath);is = new FileInputStream(file);dataList = readFile(is, is2003Excel);is.close();} catch (Exception ex) {ex.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {is = null;e.printStackTrace();}}}// 返回最后讀取的結果return dataList;}// 根據流讀取Excel文件public List<List<String>> readFile(InputStream inputStream,boolean is2003Excel) {List<List<String>> dataLists = null;try {// 根據版本選擇創建Workbook的方式Workbook wb = null;if (is2003Excel) {wb = new HSSFWorkbook(inputStream);} else {wb = new XSSFWorkbook(inputStream);}// sheet循環int sheetNum = sheetCirculation(wb);List<List<String>> dataList = new ArrayList<List<String>>();if (readSheetNum) {dataLists = read(dataList, wb, readSheet);} else {for (int i = 0; i < sheetNum; i++) {// Sheet sheet = wb.getSheetAt(i);// 顯示sheet名稱// System.out.println(sheet.getSheetName());dataLists = read(dataList, wb, i);}}} catch (IOException e) {e.printStackTrace();}return dataLists;}// 讀取數據private List<List<String>> read(List<List<String>> dataList, Workbook wb,int sheets) {// 總行數int totalRows = 0;// 總列數int totalCells = 0;// 第一個shell頁Sheet sheet = wb.getSheetAt(sheets);// Excel的行數totalRows = sheet.getPhysicalNumberOfRows();// Excel的列數if (totalRows >= 1 && sheet.getRow(0) != null) {totalCells = sheet.getRow(0).getPhysicalNumberOfCells();}// 遍歷Excel的行for (int r = 0; r < totalRows; r++) {Row row = sheet.getRow(r);if (row == null) {continue;}List<String> rowLst = new ArrayList<String>();// 遍歷Excel的列for (int c = 0; c < totalCells; c++) {Cell cell = row.getCell(c);String cellValue = "";if (null != cell) {// 以下是判斷數據的類型switch (cell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC: // 數字cellValue = cell.getNumericCellValue() + "";break;case HSSFCell.CELL_TYPE_STRING: // 字符串cellValue = cell.getStringCellValue();break;case HSSFCell.CELL_TYPE_BOOLEAN: // BooleancellValue = cell.getBooleanCellValue() + "";break;case HSSFCell.CELL_TYPE_FORMULA: // 公式cellValue = cell.getCellFormula() + "";break;case HSSFCell.CELL_TYPE_BLANK: // 空值cellValue = "";break;case HSSFCell.CELL_TYPE_ERROR: // 故障cellValue = "非法字符";break;default:cellValue = "未知類型";break;}}rowLst.add(cellValue);}// 保存第r行的第c列dataList.add(rowLst);}return dataList;}private int sheetCirculation(Workbook wb) {int sheetCount = -1;sheetCount = wb.getNumberOfSheets();return sheetCount;}// 是否是2003的excel,返回true是2003public static boolean is2003Excel(String filePath) {return filePath.matches("^.+\\.(?i)(xls)$");}// 是否是2007的excel,返回true是2007public static boolean is2007Excel(String filePath) {return filePath.matches("^.+\\.(?i)(xlsx)$");}// 構造方法public ImportExecl() {}// 得到錯誤信息public String getErrorInfo() {return errorInfo;}
public List<List<String>> readExcel(String filePath,int num){this.readSheet=num;this.readSheetNum=true;List<List<String>> list = readExcel(filePath);return list;
}
}
</span>
把readSheetNum = false;時結果如下
第0行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第1行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第2行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第3行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第4行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第5行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第6行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第7行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第8行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第9行 a1 a2 a3 a4 a5 a6 a7 a8 a9 第10行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第11行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第12行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第13行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第14行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第15行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第16行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第17行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第18行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第19行 b1 b2 b3 b4 b5 b6 b7 b8 b9 第20行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第21行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第22行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第23行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第24行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第25行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第26行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第27行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第28行 c1 c2 c3 c4 c5 c6 c7 c8 c9 第29行 c1 c2 c3 c4 c5 c6 c7 c8 c9 |
測試EXCEL2003測試.xls
把List<List<String>> list = poi.readExcel("D:\\EXCEL2003測試.xls");注釋去掉即可
作者:儱劍阿攵
轉載請注明鏈接:http://blog.csdn.net/awenluck/article/details/40394451
總結
以上是生活随笔為你收集整理的java读取Excel2003和Excel2007内容的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。