package com
.holmesen
;
import java
.io
.File
;
import java
.io
.FileInputStream
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.util
.*
;
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
;
import org
.apache
.poi
.openxml4j
.exceptions
.InvalidFormatException
;
import org
.apache
.poi
.ss
.usermodel
.WorkbookFactory
;
import org
.apache
.poi
.ss
.util
.CellRangeAddress
;
public class ExcelUtil {
public static void main(String
[] args
){
ExcelUtil excelUtil
= new ExcelUtil();
ArrayList
<Map
<String,String>> result
= excelUtil
.readExcelToObj("C:\\Users\\13156\\Downloads\\任务统计.xlsx");
for(Map
<String,String> map
:result
){
System
.out
.println(map
);
}
}
private ArrayList
<Map
<String,String>> readExcelToObj(String path
) {
Workbook wb
= null
;
ArrayList
<Map
<String,String>> result
= null
;
try {
wb
= WorkbookFactory
.create(new File(path
));
result
= readExcel(wb
, 0, 2, 0);
} catch (InvalidFormatException e
) {
e
.printStackTrace();
} catch (IOException e
) {
e
.printStackTrace();
}
return result
;
}
private ArrayList
<Map
<String,String>> readExcel(Workbook wb
,int sheetIndex
, int startReadLine
, int tailLine
) {
Sheet sheet
= wb
.getSheetAt(sheetIndex
);
Row row
= null
;
ArrayList
<Map
<String,String>> result
= new ArrayList<Map
<String,String>>();
for(int i
=startReadLine
; i
<sheet
.getLastRowNum()-tailLine
+1; i
++) {
row
= sheet
.getRow(i
);
Map
<String,String> map
= new HashMap<String,String>();
for(Cell c
: row
) {
String returnStr
= "";
boolean isMerge
= isMergedRegion(sheet
, i
, c
.getColumnIndex());
if(isMerge
) {
String rs
= getMergedRegionValue(sheet
, row
.getRowNum(), c
.getColumnIndex());
returnStr
= rs
;
}else {
returnStr
= c
.getRichStringCellValue().getString();
}
if(c
.getColumnIndex()==0){
map
.put("id",returnStr
);
}else if(c
.getColumnIndex()==1){
map
.put("base",returnStr
);
}else if(c
.getColumnIndex()==2){
map
.put("siteName",returnStr
);
}else if(c
.getColumnIndex()==3){
map
.put("articleName",returnStr
);
}else if(c
.getColumnIndex()==4){
map
.put("mediaName",returnStr
);
}else if(c
.getColumnIndex()==5){
map
.put("mediaUrl",returnStr
);
}else if(c
.getColumnIndex()==6){
map
.put("newsSource",returnStr
);
}else if(c
.getColumnIndex()==7){
map
.put("isRecord",returnStr
);
}else if(c
.getColumnIndex()==8){
map
.put("recordTime",returnStr
);
}else if(c
.getColumnIndex()==9){
map
.put("remark",returnStr
);
}
}
result
.add(map
);
}
return result
;
}
public 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
) ;
}
}
}
return null
;
}
private boolean isMergedRow(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
){
return true;
}
}
}
return false;
}
private 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
){
return true;
}
}
}
return false;
}
private boolean hasMerged(Sheet sheet
) {
return sheet
.getNumMergedRegions() > 0 ? true : false;
}
private void mergeRegion(Sheet sheet
, int firstRow
, int lastRow
, int firstCol
, int lastCol
) {
sheet
.addMergedRegion(new CellRangeAddress(firstRow
, lastRow
, firstCol
, lastCol
));
}
public String
getCellValue(Cell cell
){
if(cell
== null
) return "";
if(cell
.getCellType() == Cell
.CELL_TYPE_STRING
){
return cell
.getStringCellValue();
}else if(cell
.getCellType() == Cell
.CELL_TYPE_BOOLEAN
){
return String
.valueOf(cell
.getBooleanCellValue());
}else if(cell
.getCellType() == Cell
.CELL_TYPE_FORMULA
){
return cell
.getCellFormula() ;
}else if(cell
.getCellType() == Cell
.CELL_TYPE_NUMERIC
){
return String
.valueOf(cell
.getNumericCellValue());
}
return "";
}
public static void readContent(String fileName
) {
boolean isE2007
= false;
if(fileName
.endsWith("xlsx"))
isE2007
= true;
try {
InputStream input
= new FileInputStream(fileName
);
Workbook wb
= null
;
if(isE2007
)
wb
= new XSSFWorkbook(input
);
else
wb
= new HSSFWorkbook(input
);
Sheet sheet
= wb
.getSheetAt(0);
Iterator
<Row> rows
= sheet
.rowIterator();
while (rows
.hasNext()) {
Row row
= rows
.next();
System
.out
.println("Row #" + row
.getRowNum());
Iterator
<Cell> cells
= row
.cellIterator();
while (cells
.hasNext()) {
Cell cell
= cells
.next();
System
.out
.println("Cell #" + cell
.getColumnIndex());
switch (cell
.getCellType()) {
case HSSFCell
.CELL_TYPE_NUMERIC
:
System
.out
.println(cell
.getNumericCellValue());
break;
case HSSFCell
.CELL_TYPE_STRING
:
System
.out
.println(cell
.getStringCellValue());
break;
case HSSFCell
.CELL_TYPE_BOOLEAN
:
System
.out
.println(cell
.getBooleanCellValue());
break;
case HSSFCell
.CELL_TYPE_FORMULA
:
System
.out
.println(cell
.getCellFormula());
break;
default:
System
.out
.println("unsuported sell type======="+cell
.getCellType());
break;
}
}
}
} catch (IOException ex
) {
ex
.printStackTrace();
}
}
}
转载请注明原文地址: https://lol.8miu.com/read-12280.html