轉(zhuǎn)自 https://blog.csdn.net/qq_21454973/article/details/80581459 并稍微改動(dòng)60行 讀取cell內(nèi)容。
文件:
輸出:
廠家1_1000000_自然人11_196493_
廠家1_1000000_自然人12_164194_
廠家1_1000000_自然人13_17269_
廠家1_1000000_自然人14_56635_
廠家1_1000000_自然人15_565406_
廠家2_2000000_自然人21_483501_
廠家2_2000000_自然人22_621527_
廠家2_2000000_自然人23_415647_
廠家2_2000000_自然人24_54321_
廠家2_2000000_自然人25_425002_
pom.xml
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
代碼:
package com.navitek.utils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
* @Date: 2019/7/3 0003 16:39
public class ExcelUtils {
public static void main(String[] args) {
getAllByExcel("E:\\all_temp\\temp.xls");
public static void getAllByExcel(String filepath) {
// 同時(shí)支持Excel 2003、2007
File excelFile = new File(filepath); // 創(chuàng)建文件對(duì)象
FileInputStream is = new FileInputStream(excelFile); // 文件流
Workbook workbook = WorkbookFactory.create(is); // 這種方式 Excel
String[] res = readExcel(workbook, 0, 1, 0);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
private static String[] readExcel(Workbook wb, int sheetIndex, int startReadLine, int tailLine) {
Sheet sheet = wb.getSheetAt(sheetIndex);
String[] res = new String[sheet.getLastRowNum() - tailLine + 1];
for (int i = startReadLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {
boolean isMerge = isMergedRegion(sheet, i, c.getColumnIndex());
String rs = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex());
//System.out.print(rs + "_"+ row.getRowNum()+"_"+c.getColumnIndex() +"_");
//System.out.print(c.getRichStringCellValue() + "");
res[i] += getCellValue(c)+ "_";
String[] result = new String[res.length - startReadLine];
for (int i = 0; i < startReadLine; i++) {
for (int j = 0; j < res.length; j++) {
for (int i = 0; i < result.length; i++) {
private static boolean isMergedRegion(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
int firstColumn = range.getFirstColumn();
int lastColumn = range.getLastColumn();
int firstRow = range.getFirstRow();
int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
public static String getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);
return getCellValue(fCell);
private static String getCellValue(Cell cell) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
int cellType = cell.getCellType();
case Cell.CELL_TYPE_STRING: // 文本
cellValue = cell.getStringCellValue();
case Cell.CELL_TYPE_NUMERIC: // 數(shù)字、日期
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); // 日期型
cellValue = String.valueOf((int) cell.getNumericCellValue()); // 數(shù)字
case Cell.CELL_TYPE_BOOLEAN: // 布爾型
cellValue = String.valueOf(cell.getBooleanCellValue());
case Cell.CELL_TYPE_BLANK: // 空白
cellValue = cell.getStringCellValue();
case Cell.CELL_TYPE_ERROR: // 錯(cuò)誤
case Cell.CELL_TYPE_FORMULA: // 公式
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。