1. 程式人生 > >java讀取excel檔案

java讀取excel檔案

複製程式碼
package com.execl;

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;

/** * * @描述:測試excel讀取 * * 匯入的jar包 * * poi-3.8-beta3-20110606.jar * * poi-ooxml-3.8-beta3-20110606.jar * * poi-examples-3.8-beta3-20110606.jar * * poi-excelant-3.8-beta3-20110606.jar * * poi-ooxml-schemas-3.8-beta3-20110606.jar * * poi-scratchpad-3.8-beta3-20110606.jar * * xmlbeans-2.3.0.jar * * dom4j-1.6.1.jar * * jar包官網下載地址:
http://poi.apache.org/download.html * * 下載poi-bin-3.8-beta3-20110606.zipp * */ public class ImportExecl { /** 總行數 */ private int totalRows = 0; /** 總列數 */ private int totalCells = 0; /** 錯誤資訊 */ private String errorInfo; /** 構造方法 */ public ImportExecl() { }
public int getTotalRows() { return totalRows; } public int getTotalCells() { return totalCells; } public String getErrorInfo() { return errorInfo; } public boolean validateExcel(String filePath) { /** 檢查檔名是否為空或者是否是Excel格式的檔案 */ if (filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil .isExcel2007(filePath))) { errorInfo = "檔名不是excel格式"; return false; } /** 檢查檔案是否存在 */ File file = new File(filePath); if (file == null || !file.exists()) { errorInfo = "檔案不存在"; return false; } return true; } public List<List<String>> read(String filePath) { List<List<String>> dataLst = new ArrayList<List<String>>(); InputStream is = null; try { /** 驗證檔案是否合法 */ if (!validateExcel(filePath)) { System.out.println(errorInfo); return null; } /** 判斷檔案的型別,是2003還是2007 */ boolean isExcel2003 = true; if (WDWUtil.isExcel2007(filePath)) { isExcel2003 = false; } /** 呼叫本類提供的根據流讀取的方法 */ File file = new File(filePath); is = new FileInputStream(file); dataLst = read(is, isExcel2003); is.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { is = null; e.printStackTrace(); } } } /** 返回最後讀取的結果 */ return dataLst; } public List<List<String>> read(InputStream inputStream, boolean isExcel2003) { List<List<String>> dataLst = null; try { /** 根據版本選擇建立Workbook的方式 */ Workbook wb = null; if (isExcel2003) { wb = new HSSFWorkbook(inputStream); } else { wb = new XSSFWorkbook(inputStream); } dataLst = read(wb); } catch (IOException e) { e.printStackTrace(); } return dataLst; } private List<List<String>> read(Workbook wb) { List<List<String>> dataLst = new ArrayList<List<String>>(); /** 得到第一個shell */ Sheet sheet = wb.getSheetAt(0); /** 得到Excel的行數 */ this.totalRows = sheet.getPhysicalNumberOfRows(); /** 得到Excel的列數 */ if (this.totalRows >= 1 && sheet.getRow(0) != null) { this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); } /** 迴圈Excel的行 */ for (int r = 0; r < this.totalRows; r++) { Row row = sheet.getRow(r); if (row == null) { continue; } List<String> rowLst = new ArrayList<String>(); /** 迴圈Excel的列 */ for (int c = 0; c < this.getTotalCells(); 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: // Boolean cellValue = 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列 */ dataLst.add(rowLst); } return dataLst; } public static void main(String[] args) throws Exception { ImportExecl poi = new ImportExecl(); // List<List<String>> list = poi.read("d:/aaa.xls"); List<List<String>> list = poi.read("D:\\Users\\test.xlsx"); if (list != null) { for (int i = 0; i < list.size(); i++) { System.out.print("" + (i) + ""); List<String> cellList = list.get(i); for (int j = 0; j < cellList.size(); j++) { // System.out.print(" 第" + (j + 1) + "列值:"); System.out.print(" " + cellList.get(j)); } System.out.println(); } } } } class WDWUtil { public static boolean isExcel2003(String filePath) { return filePath.matches("^.+\\.(?i)(xls)$"); } public static boolean isExcel2007(String filePath) { return filePath.matches("^.+\\.(?i)(xlsx)$"); } }
複製程式碼

相關推薦

java讀取excel檔案內容,並將讀取到的內容寫入到另一檔案

需要匯入的jar包下載地址https://pan.baidu.com/s/16cTpUfx0KvKkbGYkXAUKMA 程式碼:ReadExcel.java //信1605-3 20163432 張運濤 package domain; import java.io.File; im

Java 讀取Excel 檔案內容

在一個專案中,有一個需求,是把excel檔案的內容轉換為xml格式展示。在學習如何操作的過程中,首先是如何獲取excel檔案,其中操作的程式碼如下: 1.首先是匯入需要的 jar, 下載地址: 2.程式碼實現: package com.apusic; import org.apache.po

java讀取excel檔案內容

import java.io.FileInputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List;

java讀取excel 檔案,並把讀取到的資料轉換成javabean物件

javaBean物件public class AcademicaChievements { private Integer aid; private String number; private String sname; private String award_

java讀取excel檔案

package com.execl; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.A

解決java讀取excel檔案有小數點的問題

package com.hmy.ssh.myMethod; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import

Java讀取Excel檔案,生成SQL語句

import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import java.io.*; public class Main { public static void

使用Java讀取Excel檔案內容

使用Java讀取Excel檔案的內容非常簡單,Apache POI這個專案已經實現了對此類文件操作的功能,我們只需學會如何使用它即可。 1.首先需要引入Apache POI,這裡推薦使用Maven的方式管理專案依賴。在pom.xml檔案中加入依賴項: &

java利用poi讀取Excel檔案

java讀取Excel檔案,筆者認為:從結構上來看,Excel檔案是由一個一個的單元格組成的,有點像細胞cell,逐行的排列。那麼我們讀的時候也應該逐行逐行的讀,從左到右的尋找每一個cell。一、例項程式碼: 只是實現了一種方式,因為依照讀取內容的不同,讀取的後想要的操作不同,因此不能苟同全部,只是方法是相

java使用POI讀取excel檔案,相容xls和xlsx

public List<Double> readExcels(InputStream is)throws Exception{List<Double> xlsxList = new ArrayList<Double>();    try {             if(i

Java模組 -- 讀取Excel檔案寫入資料庫 Mybatis , POI , JXL

廢話不多說,直接上程式碼結構圖 所用到的lib包 Students 實體類 package com.test.model; public class Students { private int id; private String username; pr

java 操作 poi 解析、讀取 Excel 檔案

讀取Excel 我這裡是用的POI 的jar包 dom4j-1.6.1.jar         poi-3.10.1-20140818.jar        poi-excelant-3.10.1-20140818.jar    xmlbeans-2.6.0.jar po

Java 讀取excel指定行列資料以及將資料儲存到txt檔案

在使用的軟體中經常要用到一些資料的匯入匯出,以及準確的定位資料,這些經常會涉及excle表格,因此把今天學習到的關於如何利用Java準確獲取到excle中的某一列資料,同時將此列資料輸出到txt檔案格式中。 使用的jar包:jxl.jar  相關的API:http://jx

java讀取excel獲取數據寫入到另外一個excel

string stat arr final user tostring enc ++ cef pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.a

java讀取Excel表格中的數據

bubuko str 需求 equal read sta alt 表格 nbsp 1、需求 用java代碼讀取hello.xls表格中的數據 2、hello.xls表格 3、java代碼 package com.test; import java.io.File;

java讀取excel文件數據導入mysql數據庫

l數據庫 ktr static null AD 第二周 pen 不支持 ace 這是我來公司的第二周的一個小學習任務,下面是實現過程: 1.建立maven工程(方便管理jar包) 在pom.xml導入 jxl,mysql-connector 依賴 可以在maven

Java讀取excel數據保存入庫

number date類型 put ase cep throws user workbook urn Java開發讀取excel表格數據入庫保存: List<Map<String, Object>> list = null; String fileP

java讀取json檔案並轉換為String

import java.io.*; public class Output { //測試 public static void main(String[] args){ String json = "null"; try { json = readJsonData("I

第四篇:java讀取Excel簡單模板

場景:對於經常需要匯入Excel模板或資料來解析後加以應用的,使用頻率非常之高,做了一個比較穩定的版本,體現在這些地方工具:org.apache.poi使用前必須瞭解這些:1、要解析,那肯定先判斷是不是Excel2、xls字尾的Excel,是03版及以前的用HSSFWorkbook類  &

【Python筆記】操作讀取Excel檔案、文字檔案

需求:讀取Excel檔案、替換文字檔案中得指定某個字串並生成新的檔案 原始碼: #encoding:utf-8 # -*- coding: utf-8 -*- #!/usr/bin/env python # -*- coding=utf-8 -*- #Using GPL v2 #Author: