基于Apache POI 从xlsx读出数据
生活随笔
收集整理的這篇文章主要介紹了
基于Apache POI 从xlsx读出数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】寫在前面
- 0.1) these codes are from 基于Apache POI 的從xlsx讀出數據
- 0.2) this idea is from http://cwind.iteye.com/blog/2187670 , adding some comments for easy understanding proves to be my work.
package com.cwind.poi; import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class SimpleDatasheetReader {public static void main(String[] args){try {File excel = new File("E:/bench-cluster/temp-resource/RunningMan.xlsx");FileInputStream fis = new FileInputStream(excel);
//創建工作簿
XSSFWorkbook book = new XSSFWorkbook(fis);//創建工作簿下的第一頁紙張
XSSFSheet sheet = book.getSheetAt(0);// 基于行的迭代器
Iterator<Row> itr = sheet.iterator(); System.out.println(itr.hasNext());// Iterating over Excel file in Java
while (itr.hasNext()) {//得到行
Row row = itr.next(); System.out.println(row.getLastCellNum());// Iterating over each column of Excel file
// 基于行創建單元格 迭代器
//依次 獲取某行的單元格
Cell cell = cellIterator.next();switch (cell.getCellType()) {//下面是依據不同數據類型 打印出單元格的 數據
case Cell.CELL_TYPE_STRING:System.out.print(cell.getStringCellValue() + "\t");break;case Cell.CELL_TYPE_NUMERIC:if(DateUtil.isCellDateFormatted(cell)){System.out.print(cell.getDateCellValue() + "\t");}else{System.out.print(cell.getNumericCellValue() + "\t");}break;case Cell.CELL_TYPE_BOOLEAN:System.out.print(cell.getBooleanCellValue() + "\t");break;default:}}System.out.println("");}}catch(Exception ex){ex.printStackTrace();}} }總結
以上是生活随笔為你收集整理的基于Apache POI 从xlsx读出数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么申请邮箱域名(怎么申请邮箱域名注册)
- 下一篇: 域名到期怎么抢注(域名到期怎么抢注)