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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java - Poi 操作 Excel

發布時間:2025/3/21 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java - Poi 操作 Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java - Poi 操作 Excel

關注 “弋凡”(YiFan)微信公眾號吧 記錄簡單筆記 做你的最愛

  • 注意

    • XSSFWorkbook 對象是操作 .xlsx 格式的表格
    • HSSFWorkbook 對象是操作 .xls 格式的表格
    • xls格式 <= 65536 行
    • xlsx格式 > 65536 行
  • 導入pom文件

<!-- .xlsx格式 7版本--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency> <!-- 日期格式化工具!--><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.10.1</version></dependency>
  • 寫入數據

public static void main(String[] args) throws Exception {String PATH = "E:/IDEA_Project/hzmv/";// 創建一個 ExcelWorkbook workbook = new XSSFWorkbook();// 創建一個工作表Sheet sheet = workbook.createSheet("HZMV~數據源");// 創建一行Row row1 = sheet.createRow(0);// 創建一個單元格 (1,1)Cell cell1 = row1.createCell(0);cell1.setCellValue("鏈接");//(1,2)Cell cell2 = row1.createCell(1);cell2.setCellValue("格式");// 創建一行Row row2 = sheet.createRow(1);Cell cell3 = row2.createCell(0);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell3.setCellValue(time);// 生成一張表(io流)FileOutputStream stream = new FileOutputStream(PATH + "hzmv數據源.xlsx");workbook.write(stream);stream.close();System.err.println("hzmv數據源生產完畢~");}
  • 讀取數據

public static void main(String[] args) throws Exception {String PATH = "E:/IDEA_Project/hzmv/";// 獲取文件流FileInputStream inputStream = new FileInputStream(PATH + "hzmv數據源.xlsx");// 創建一個工作簿 execl能使用的操作 這里都能使用Workbook workbook = new XSSFWorkbook(inputStream);// 拿到第一個表Sheet sheetAt = workbook.getSheetAt(0);System.err.println(sheetAt);// 拿到第行Row row1 = sheetAt.getRow(0);//拿到第一個單元格Cell cell1 = row1.getCell(1);// 讀取的時候一定要注意數據類型/** getStringCellValue 字符串類型* getNumericCellValue 數字類型* */System.err.println(cell1.getStringCellValue()); }
  • 讀取數據及判斷數據類型 *

public static void main(String[] args) throws Exception {String PATH = "E:/IDEA_Project/hzmv/";// 獲取文件流FileInputStream inputStream = new FileInputStream(PATH + "hzmv數據源.xlsx");// 創建一個工作簿 execl能使用的操作 這里都能使用Workbook workbook = new XSSFWorkbook(inputStream);// 拿到第一個表Sheet sheetAt = workbook.getSheetAt(0);// 獲取標題內容Row rowTit = sheetAt.getRow(0);if(rowTit != null ){// 得到所有的列~int cellCount = rowTit.getPhysicalNumberOfCells();// 遍歷for (int cellnum = 0; cellnum < cellCount ; cellnum++) {// 得到單元格Cell cell = rowTit.getCell(cellnum);if(cell != null ){CellType cellType = cell.getCellType();String cellValue = "";switch (cellType){case _NONE:break;case NUMERIC:cellValue = String.valueOf(cell.getNumericCellValue());break;case STRING :cellValue = cell.getStringCellValue();break;case FORMULA:break;case BLANK:break;case BOOLEAN:cellValue = String.valueOf(cell.getBooleanCellValue());break;case ERROR:break;}System.err.print(cellValue+"->"+cellType+" | ");}}}System.err.println();// 獲取表中的內容int rowCount = sheetAt.getPhysicalNumberOfRows();for (int rownum = 1; rownum < rowCount ; rownum++) {Row rowData = sheetAt.getRow(rownum);if(rowData != null){// 得到所有的列int cellCount = rowData.getPhysicalNumberOfCells();for (int cellnum = 0; cellnum < cellCount ; cellnum++) {// 得到每個單元格Cell cell = rowData.getCell(cellnum);if(cell != null ){// 得到每個單元格的數據的類型~CellType cellType = cell.getCellType();String cellValue = "";switch (cellType){case _NONE:cellValue= "null";break;case NUMERIC:// 日期if(DateUtil.isCellDateFormatted(cell)){Date value = cell.getDateCellValue();cellValue = new DateTime(value).toString("yyyy-MM-dd HH:mm:ss");break;}else {cellValue = String.valueOf(cell.getNumericCellValue());break;}case STRING :cellValue = cell.getStringCellValue();break;case FORMULA:break;case BLANK:cellValue= "null";break;case BOOLEAN:cellValue = String.valueOf(cell.getBooleanCellValue());break;case ERROR:cellValue= "error";break;}System.err.print(cellValue+"->"+cellType+" | ");}}System.err.println();}}inputStream.close(); }
  • 運行效果圖

  • 數據內容

快來關注“弋凡”微信公眾號吧

總結

以上是生活随笔為你收集整理的Java - Poi 操作 Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

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