Excel学习4_ java poi 读取有合并单元格的EXCEL文件
2017-09-06 21:55
639 查看
数据的样子是这样的
![](http://img.blog.csdn.net/20170401173139231?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTkxOTQyMzY1NA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
[java] view
plain copy
public String addReportByExcel(Long userId,InputStream inputStream,String fileName)
throws BusinessException{
String message = "Import success";
boolean isE2007 = false; //判断是否是excel2007格式
if(fileName.endsWith("xlsx")){
isE2007 = true;
}
int rowIndex = 0;
int columnIndex = 0;
try {
InputStream input = inputStream; //建立输入流
Workbook wb = null;
//根据文件格式(2003或者2007)来初始化
if(isE2007){
wb = new XSSFWorkbook(input);
}else{
wb = new HSSFWorkbook(input);
}
Sheet sheet = wb.getSheetAt(0); //获得第一个表单
//System.out.println("总行数:"+sheet.getLastRowNum());
List<CellRangeAddress> cras = getCombineCell(sheet);
//isMergedRegion(Sheet sheet,int row ,int column);判断是不是合并单元格\
int count = sheet.getLastRowNum()+1;//总行数
List<InspectionReport> irs = new ArrayList<>();
for(int i = 1; i < count;i++){
rowIndex = i;
Row row = sheet.getRow(i);
InspectionReport ir = new InspectionReport();
ir.setReportName(getCellValue(row.getCell(0)));
ir.setShift(Double.valueOf(getCellValue(row.getCell(1))).intValue());
ir.setLine(getCellValue(row.getCell(2)));
ir.setStationCode(getCellValue(row.getCell(3)));
ir.setArea(Double.valueOf(getCellValue(row.getCell(4))).intValue());
ir.setReportStatus(Double.valueOf(getCellValue(row.getCell(5))).intValue());
List<InspectionItem> items = new ArrayList<>();
if(isMergedRegion(sheet,i,0)){
int lastRow = getRowNum(cras,sheet.getRow(i).getCell(0),sheet);
for(;i<=lastRow;i++){
row = sheet.getRow(i);
InspectionItem item = new InspectionItem();
item.setItem(getCellValue(row.getCell(6)));
item.setMethod(getCellValue(row.getCell(7)));
item.setMode(getCellValue(row.getCell(8)));
item.setStandardValue(getCellValue(row.getCell(9)));
item.setDeviationValue(getCellValue(row.getCell(10)));
String pinci = getCellValue(row.getCell(11));
Double d = Double.valueOf(pinci);
item.setFrequency(d.intValue());
items.add(item);
}
i--;
}else{
row = sheet.getRow(i);
InspectionItem item = new InspectionItem();
item.setItem(getCellValue(row.getCell(6)));
item.setMethod(getCellValue(row.getCell(7)));
item.setMode(getCellValue(row.getCell(8)));
item.setStandardValue(getCellValue(row.getCell(9)));
item.setDeviationValue(getCellValue(row.getCell(10)));
String pinci = getCellValue(row.getCell(11));
Double d = Double.valueOf(pinci);
item.setFrequency(d.intValue());
items.add(item);
}
ir.setItems(items);
irs.add(ir);
}
XaResult<List<InspectionReport>> saved = inspectionReportService.createByXls(1l, irs);
message = saved.getMessage();
/*JSONArray js= new JSONArray();
js.addAll(irs);
System.out.println(js.toJSONString());*/
} catch (Exception ex) {
//xr.setMessage("Import failed, please check the data in "+rowIndex+" rows "+columnIndex+" columns ");
message = "Import failed, please check the data in "+rowIndex+" rows ";
}
return message;
}
使用到的 工具类:
[java] view
plain copy
/**
* 获取单元格的值
* @param cell
* @return
*/
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 "";
}<pre code_snippet_id="2306825" snippet_file_name="blog_20170401_2_6166356" name="code" class="java">/**
* 合并单元格处理,获取合并行
* @param sheet
* @return List<CellRangeAddress>
*/
public List<CellRangeAddress> getCombineCell(Sheet sheet)
{
List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
//获得一个 sheet 中合并单元格的数量
int sheetmergerCount = sheet.getNumMergedRegions();
//遍历所有的合并单元格
for(int i = 0; i<sheetmergerCount;i++)
{
//获得合并单元格保存进list中
CellRangeAddress ca = sheet.getMergedRegion(i);
list.add(ca);
}
return list;
}
private int getRowNum(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet){
int xr = 0;
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
for(CellRangeAddress ca:listCombineCell)
{
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR)
{
if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC)
{
xr = lastR;
}
}
}
return xr;
}
/**
* 判断单元格是否为合并单元格,是的话则将单元格的值返回
* @param listCombineCell 存放合并单元格的list
* @param cell 需要判断的单元格
* @param sheet sheet
* @return
*/
public String isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet)
throws Exception{
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
String cellValue = null;
for(CellRangeAddress ca:listCombineCell)
{
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR)
{
if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC)
{
Row fRow = sheet.getRow(firstR);
Cell fCell = fRow.getCell(firstC);
cellValue = getCellValue(fCell);
break;
}
}
else
{
cellValue = "";
}
}
return cellValue;
}
/**
* 获取合并单元格的值
* @param sheet
* @param row
* @param column
* @return
*/
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 ;
}
/**
* 判断指定的单元格是否是合并单元格
* @param sheet
* @param row 行下标
* @param column 列下标
* @return
*/
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;
}</pre><br>
<pre></pre>
<p></p>
<pre></pre>
<br>
<br>
<br>
<p></p>
原文来自:http://blog.csdn.net/a919423654/article/details/68946507
[java] view
plain copy
public String addReportByExcel(Long userId,InputStream inputStream,String fileName)
throws BusinessException{
String message = "Import success";
boolean isE2007 = false; //判断是否是excel2007格式
if(fileName.endsWith("xlsx")){
isE2007 = true;
}
int rowIndex = 0;
int columnIndex = 0;
try {
InputStream input = inputStream; //建立输入流
Workbook wb = null;
//根据文件格式(2003或者2007)来初始化
if(isE2007){
wb = new XSSFWorkbook(input);
}else{
wb = new HSSFWorkbook(input);
}
Sheet sheet = wb.getSheetAt(0); //获得第一个表单
//System.out.println("总行数:"+sheet.getLastRowNum());
List<CellRangeAddress> cras = getCombineCell(sheet);
//isMergedRegion(Sheet sheet,int row ,int column);判断是不是合并单元格\
int count = sheet.getLastRowNum()+1;//总行数
List<InspectionReport> irs = new ArrayList<>();
for(int i = 1; i < count;i++){
rowIndex = i;
Row row = sheet.getRow(i);
InspectionReport ir = new InspectionReport();
ir.setReportName(getCellValue(row.getCell(0)));
ir.setShift(Double.valueOf(getCellValue(row.getCell(1))).intValue());
ir.setLine(getCellValue(row.getCell(2)));
ir.setStationCode(getCellValue(row.getCell(3)));
ir.setArea(Double.valueOf(getCellValue(row.getCell(4))).intValue());
ir.setReportStatus(Double.valueOf(getCellValue(row.getCell(5))).intValue());
List<InspectionItem> items = new ArrayList<>();
if(isMergedRegion(sheet,i,0)){
int lastRow = getRowNum(cras,sheet.getRow(i).getCell(0),sheet);
for(;i<=lastRow;i++){
row = sheet.getRow(i);
InspectionItem item = new InspectionItem();
item.setItem(getCellValue(row.getCell(6)));
item.setMethod(getCellValue(row.getCell(7)));
item.setMode(getCellValue(row.getCell(8)));
item.setStandardValue(getCellValue(row.getCell(9)));
item.setDeviationValue(getCellValue(row.getCell(10)));
String pinci = getCellValue(row.getCell(11));
Double d = Double.valueOf(pinci);
item.setFrequency(d.intValue());
items.add(item);
}
i--;
}else{
row = sheet.getRow(i);
InspectionItem item = new InspectionItem();
item.setItem(getCellValue(row.getCell(6)));
item.setMethod(getCellValue(row.getCell(7)));
item.setMode(getCellValue(row.getCell(8)));
item.setStandardValue(getCellValue(row.getCell(9)));
item.setDeviationValue(getCellValue(row.getCell(10)));
String pinci = getCellValue(row.getCell(11));
Double d = Double.valueOf(pinci);
item.setFrequency(d.intValue());
items.add(item);
}
ir.setItems(items);
irs.add(ir);
}
XaResult<List<InspectionReport>> saved = inspectionReportService.createByXls(1l, irs);
message = saved.getMessage();
/*JSONArray js= new JSONArray();
js.addAll(irs);
System.out.println(js.toJSONString());*/
} catch (Exception ex) {
//xr.setMessage("Import failed, please check the data in "+rowIndex+" rows "+columnIndex+" columns ");
message = "Import failed, please check the data in "+rowIndex+" rows ";
}
return message;
}
使用到的 工具类:
[java] view
plain copy
/**
* 获取单元格的值
* @param cell
* @return
*/
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 "";
}<pre code_snippet_id="2306825" snippet_file_name="blog_20170401_2_6166356" name="code" class="java">/**
* 合并单元格处理,获取合并行
* @param sheet
* @return List<CellRangeAddress>
*/
public List<CellRangeAddress> getCombineCell(Sheet sheet)
{
List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
//获得一个 sheet 中合并单元格的数量
int sheetmergerCount = sheet.getNumMergedRegions();
//遍历所有的合并单元格
for(int i = 0; i<sheetmergerCount;i++)
{
//获得合并单元格保存进list中
CellRangeAddress ca = sheet.getMergedRegion(i);
list.add(ca);
}
return list;
}
private int getRowNum(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet){
int xr = 0;
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
for(CellRangeAddress ca:listCombineCell)
{
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR)
{
if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC)
{
xr = lastR;
}
}
}
return xr;
}
/**
* 判断单元格是否为合并单元格,是的话则将单元格的值返回
* @param listCombineCell 存放合并单元格的list
* @param cell 需要判断的单元格
* @param sheet sheet
* @return
*/
public String isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet)
throws Exception{
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
String cellValue = null;
for(CellRangeAddress ca:listCombineCell)
{
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR)
{
if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC)
{
Row fRow = sheet.getRow(firstR);
Cell fCell = fRow.getCell(firstC);
cellValue = getCellValue(fCell);
break;
}
}
else
{
cellValue = "";
}
}
return cellValue;
}
/**
* 获取合并单元格的值
* @param sheet
* @param row
* @param column
* @return
*/
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 ;
}
/**
* 判断指定的单元格是否是合并单元格
* @param sheet
* @param row 行下标
* @param column 列下标
* @return
*/
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;
}</pre><br>
<pre></pre>
<p></p>
<pre></pre>
<br>
<br>
<br>
<p></p>
原文来自:http://blog.csdn.net/a919423654/article/details/68946507
相关文章推荐
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- java通过poi解析Excel文件,直接读取客户端文件
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
- java使用POI读取properties文件并写到Excel的方法
- Java学习-017-EXCEL 文件读取实例源代码
- Java模块 -- 读取Excel文件写入数据库 Mybatis , POI , JXL
- java使用poi读取ppt文件和poi读取excel、word示例
- JAVA:使用POI读取excel文件中的日期
- poi读取Excel时日期为数字 的解决方法 转载 2017年12月14日 10:31:14 标签:poi /excel /java 19 在使用poi解析Excel文件时,会发现,表格中的日期解析出
- Java使用poi读取Excel文件例子
- Java SpringMVC POI上传excel并读取文件内容
- Java 读取Excel文件(POI)
- iMOOC学习笔记:Java的Excel读写操作入门实践-利用POI解析Excel文件(二)
- java 读取Excel文件(POI)
- java使用poi读取ppt文件和poi读取excel、word示例
- 【JAVA】apache poi excel 文件读取,各种数据类型,不规则excel格式都可以读取
- Java+Flex+POI 解析Excel文件(读取文件中想要的内容)
- java poi 读取Excel文件,进行导入
- 【java】使用POI读取excel文件内容
- POI读取EXCEL文件的错误 java.io.IOException: Invalid header signature; read 23