POI和easyExcel使用(2)PoI对Excel的基本读写操作
生活随笔
收集整理的這篇文章主要介紹了
POI和easyExcel使用(2)PoI对Excel的基本读写操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
excel有四個主要對象:工作薄、工作表、行、單元格
1.POI的寫操作
創建一個maven項目,需要引入POI的一些包
<!--xls(03)--> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version> </dependency> <!--xls(07)--> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version> </dependency>寫操作基本包括如下步驟(針對03年Excel):
- 創建工作薄
- 創建工作表
- 創建行
- 創建單元格,并賦值
- 生成表
07版和03版操作一樣,就是工作簿對象不一樣,同時也是文件后綴不一樣
@Test//針對07版Excelpublic void test07() throws Exception {//創建一個工作簿Workbook workbook = new XSSFWorkbook();//創建一個工作表Sheet sheet = workbook.createSheet("學生統計表");//創建一個行Row row1 = sheet.createRow(0);//創建一個單元格Cell cell = row1.createCell(0);cell.setCellValue("1,1");//生成一張表FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xlsx");workbook.write(fileOutputStream);//關閉fileOutputStream.close();}2. POI大數據量寫入
?03版:只能導65536行數據
@Test//針對03版Excelpublic void test03BigData() throws Exception {long begin = System.currentTimeMillis();//創建一個工作簿Workbook workbook = new HSSFWorkbook();//創建一個工作表Sheet sheet = workbook.createSheet("學生統計表");//大數據導入for(int rowNum=0;rowNum<65536;rowNum++) {Row row = sheet.createRow(rowNum);for (int column = 0; column < 10; column++) {Cell cell = row.createCell(column);cell.setCellValue(column);}}//生成一張表FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xls");workbook.write(fileOutputStream);//關閉fileOutputStream.close();long end = System.currentTimeMillis();System.out.println("時間"+(end-begin)/1000+"s");}?07版:無行數限制速度慢
@Test//針對03版Excelpublic void test07BigData() throws Exception {long begin = System.currentTimeMillis();//創建一個工作簿Workbook workbook = new XSSFWorkbook();//創建一個工作表Sheet sheet = workbook.createSheet("學生統計表");//大數據導入for(int rowNum=0;rowNum<65536;rowNum++) {Row row = sheet.createRow(rowNum);for (int column = 0; column < 10; column++) {Cell cell = row.createCell(column);cell.setCellValue(column);}}//生成一張表FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xlsx");workbook.write(fileOutputStream);//關閉fileOutputStream.close();long end = System.currentTimeMillis();System.out.println("時間"+(end-begin)/1000+"s");}?加速版:
@Test//針對07版Excelpublic void test07BigDataS() throws Exception {long begin = System.currentTimeMillis();//創建一個工作簿Workbook workbook = new SXSSFWorkbook();//創建一個工作表Sheet sheet = workbook.createSheet("學生統計表");//大數據導入for(int rowNum=0;rowNum<100000;rowNum++) {Row row = sheet.createRow(rowNum);for (int column = 0; column < 10; column++) {Cell cell = row.createCell(column);cell.setCellValue(column);}}//生成一張表FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xlsx");workbook.write(fileOutputStream);//關閉fileOutputStream.close();//清除臨時文件((SXSSFWorkbook)workbook).dispose();long end = System.currentTimeMillis();System.out.println("時間"+(end-begin)/1000+"s");}3. poi讀操作
步驟:
- 獲取文件流
- ?根據文件流創建工作薄
- 得到工作表
- 得到行
- 得到單元格并輸出,讀取值要保證類型相同
- 關閉文件流
4.讀取不同類型的數據
先獲取標題,再獲取內容
@Testpublic void readCellType() throws Exception {FileInputStream fileInputStream = new FileInputStream(path + "test.xls");//創建工作薄Workbook workbook = new HSSFWorkbook(fileInputStream);//獲得工作表Sheet sheet = workbook.getSheetAt(0);//獲取標題內容(第一行)Row rowHead = sheet.getRow(0);if (rowHead != null) {//獲取總數目int cellCount = rowHead.getPhysicalNumberOfCells();for (int cellNum = 0; cellNum < cellCount; cellNum++) {Cell cell = rowHead.getCell(cellNum);if (cell != null) {String cellValue = cell.getStringCellValue();System.out.print(cellValue + " | ");}}System.out.println();//讀取表中內容int rowCount = sheet.getPhysicalNumberOfRows();for (int rowNum = 1; rowNum < rowCount; rowNum++) {Row rowData = sheet.getRow(rowNum);if (rowData != null) {for (int cellNum=0;cellNum<cellCount;cellNum++){Cell cell = rowData.getCell(cellNum);//匹配列的數據類型if(cell!=null){int cellType = cell.getCellType();String cellValue = "";switch (cellType){case CELL_TYPE_STRING:System.out.println("string類型");cellValue = cell.getStringCellValue();break;case CELL_TYPE_BOOLEAN:System.out.println("boolean類型");cellValue = String.valueOf(cell.getBooleanCellValue());break;case CELL_TYPE_NUMERIC:System.out.println("數字類型");if(HSSFDateUtil.isCellDateFormatted(cell)){System.out.println("日期");Date date = cell.getDateCellValue();cellValue =new DateTime(date).toString("yyyy-MM-dd");}else{cellValue = String.valueOf(cell.getNumericCellValue());}break;case CELL_TYPE_FORMULA:System.out.println("formula類型");break;case CELL_TYPE_BLANK:System.out.println("空");break;case CELL_TYPE_ERROR:System.out.println("類型錯誤");break;}System.out.print(cellValue+" ");}}System.out.println();}}}} }?
總結
以上是生活随笔為你收集整理的POI和easyExcel使用(2)PoI对Excel的基本读写操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android volley 超时,an
- 下一篇: WeMall应用商店插件更新: