日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI和easyExcel使用(2)PoI对Excel的基本读写操作

發布時間:2024/1/1 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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):

  • 創建工作薄
//創建一個工作簿Workbook workbook = new HSSFWorkbook();
  • 創建工作表
//創建一個工作表 Sheet sheet = workbook.createSheet("學生統計表");
  • 創建行
//創建一個行Row row1 = sheet.createRow(0);
  • 創建單元格,并賦值
//創建一個單元格 Cell cell = row1.createCell(0); cell.setCellValue("1,1");
  • 生成表
//生成一張表FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xls");workbook.write(fileOutputStream);//關閉fileOutputStream.close();

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讀操作

步驟:

  • 獲取文件流
FileInputStream fileInputStream = new FileInputStream(path + "學生統計表.xlsx");
  • ?根據文件流創建工作薄
//創建工作薄Workbook workbook = new HSSFWorkbook(fileInputStream);
  • 得到工作表
//獲得工作表 Sheet sheet = workbook.getSheetAt(0);
  • 得到行
//獲得行Row row = sheet.getRow(0);
  • 得到單元格并輸出,讀取值要保證類型相同
//獲得單元格Cell cell = row.getCell(0);System.out.println(cell.getStringCellValue());
  • 關閉文件流
fileInputStream.close();

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的基本读写操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。