利用POI插件导入excel 读取合并行数据(上)
图为要导入的excel格式

分析一下:

前一部分数据是读取 合并行 存入一张 “会见” 表 ,后面蓝色的 非合并行 存入 “会见人信息” 表。
先说后台方法,(读取本地文件例子)
public void importJsInfo() throws IOException {
Boolean bool = false;
Record record =new Record();
String name ="1.xlsx";
FileInputStream f = new FileInputStream(new File("C:\\Users\\foresee\\Desktop\\1.xlsx"));
record = readXls(f,name); //读取excel方法
if(record.get("list") != null ) {
List<Hjrxx> hjrxxList = record.get("hjrxxList");
List<Hj> hjList = record.get("list");
bool = HjService.service.saveJsxx(hjrxxList,hjList); //把读取结果封装在record对象里,并保存两张表的业务逻辑
}
renderText(String.valueOf(bool));
}
下面是 :上面调用的读取excel方法 readXls (注意下身份证长数字和日期格式处理)
/**
* 读取excel
* @throws IOException
*/
private Record readXls(InputStream inputStream,String fileName) throws IOException {
Record record = new Record();
boolean isE2007 = false; //判断是否是excel2007格式
if(fileName.endsWith("xlsx")){
isE2007 = true;
}
int rowIndex = 0;
int columnIndex = 0; InputStream input = inputStream; //建立输入流
Workbook wb = null;
//根据文件格式(2003或者2007)来初始化
if(isE2007){
wb = new XSSFWorkbook(input);
}else{
wb = new HSSFWorkbook(input);
}
Sheet sheet = wb.getSheetAt(0); //获得第一个表单 List<CellRangeAddress> cras = importHj.getCombineCell(sheet);
//isMergedRegion(Sheet sheet,int row ,int column);判断是不是合并单元格
int count = sheet.getLastRowNum()+1;//总行数 List<Hj> hjs = new ArrayList<>();
List<Hjrxx> hjrxxs = new ArrayList<>(); //这两个LIst最后要放入record这个对象里,作为方法的返回值
for(int i = 2; i < count;i++){
rowIndex = i;
Row row = sheet.getRow(i);
Hj hj = new Hj();
if(importHj.getCellValue(row.getCell(0)) != " ") {
String sfzh = NumberToTextConverter.toText(row.getCell(0).getNumericCellValue()); //根据表格获得的身份证查出另一个字段值,存入数据库
Record jbxx = JbxxService.service.findByBrSfzh(sfzh);
hj.set(Hj.column_br_id, jbxx.get("br_id"));
}
//如果该行是日期,同样是需要先处理,否则会报错
if (HSSFDateUtil.isCellDateFormatted(row.getCell(3))) {// 处理日期格式、时间格式
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = row.getCell(3).getDateCellValue();
Date jssj = row.getCell(4).getDateCellValue();
String a =sdf.format(date);//根据需要取时间,date类型和String类型
String b =sdf.format(jssj);
System.out.println(a);
hj.set(Hj.column_kssj, ToolDateTime.parse(a, ToolDateTime.pattern_ymd_hm));
hj.set(Hj.column_jssj, ToolDateTime.parse(b, ToolDateTime.pattern_ymd_hm)); }
hj.set(Hj.column_hjlx,"01"); //会见实体类
hj.set(Hj.column_pzbm,importHj.getCellValue(row.getCell(1)));
hj.set(Hj.column_pzr,importHj.getCellValue(row.getCell(2)));
hj.set(Hj.column_jbmj,importHj.getCellValue(row.getCell(5)));
hj.set(Hj.column_bz,importHj.getCellValue(row.getCell(6)));
hj.set(Hj.column_createtm, ToolDateTime.getSqlTimestamp(new Date()));
hj.set(Hj.column_updatetm, ToolDateTime.getSqlTimestamp(new Date()));
hj.set(Hj.column_validmk, Hj.VALIDMK_1);
Hjrxx hjrxx = null;
if(importHj.isMergedRegion(sheet,i,0)){
int lastRow = importHj.getRowNum(cras,sheet.getRow(i).getCell(0),sheet); for(;i<=lastRow;i++){
row = sheet.getRow(i);
hjrxx = new Hjrxx(); //会见人信息实体类
hjrxx.set("hjrxm",importHj.getCellValue(row.getCell(7)));
hjrxx.set("sfzh",importHj.getCellValue(row.getCell(8)));
hjrxx.set("lxfs",importHj.getCellValue(row.getCell(9)));
hjrxx.set("szdw",importHj.getCellValue(row.getCell(10)));
hjrxx.set("hjdz",importHj.getCellValue(row.getCell(11)));
hjrxx.set(Hjrxx.column_createtm, ToolDateTime.getSqlTimestamp(new Date()));
hjrxx.set(Hjrxx.column_updatetm, ToolDateTime.getSqlTimestamp(new Date()));
hjrxx.set(Hjrxx.column_validmk, Hjrxx.VALIDMK_1);
hjrxxs.add(hjrxx);
}
i--;
}else{
row = sheet.getRow(i);
hjrxx = new Hjrxx();
hjrxx.set("hjrxm",importHj.getCellValue(row.getCell(7)));
hjrxx.set("sfzh",importHj.getCellValue(row.getCell(8)));
hjrxx.set("lxfs",importHj.getCellValue(row.getCell(9)));
hjrxx.set("szdw",importHj.getCellValue(row.getCell(10)));
hjrxx.set("hjdz",importHj.getCellValue(row.getCell(11)));
hjrxx.set(Hjrxx.column_createtm, ToolDateTime.getSqlTimestamp(new Date()));
hjrxx.set(Hjrxx.column_updatetm, ToolDateTime.getSqlTimestamp(new Date()));
hjrxx.set(Hjrxx.column_validmk, Hjrxx.VALIDMK_1);
hjrxxs.add(hjrxx);
}
hjs.add(hj); }
record.set("list", hjs);
record.set("hjrxxList",hjrxxs);
return record;
}
上面用到的工具类,单独放一个文件,直接调用(工具类从网上抄的,没改动)
注:该文件为本地一个文件。下篇写上传一个文件,并导入数据库。
package com.platform.mvc.aq.hj; import java.util.ArrayList;
import java.util.List; 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.util.CellRangeAddress; public class importHj {
/**
* 获取单元格的值
* @param cell getCombineCell
* @return
*/
public static 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() ;
}
return "";
}
/**
* 合并单元格处理,获取合并行
* @param sheet
* @return List<CellRangeAddress>
*/
public static 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;
} static 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
*/
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){
return true;
}
}
}
return false;
} }
利用POI插件导入excel 读取合并行数据(上)的更多相关文章
- 利用kettle组件导入excel文件到数据库
利用kettle组件导入excel文件到数据库 1. 实现目标 把excel文件内容导入到目标表中:然后用java调用kettle的转换.excel文件的内容仅仅有两列,示比例如以下: wat ...
- POI异步导入Excel兼容xsl和xlsx
项目架构:spring+struts2+hibernate4+oracle 需求:用户导入excel文件,导入到相应的数据表中,要求提供导入模板,支持xls和xlsx文件 思路分析: 1.提供一个下载 ...
- Java利用POI实现导入导出Excel表格示例代码
转自:https://www.jb51.net/article/95526.htm 介绍 Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其 ...
- SSH框架使用poi插件实现Excel的导入导出功能
采用POI生成excel结构 直接贴出代码 excel表格导出功能 action代码: struts.xml配置: 前台jsp代码:
- poi批量导入excel文件
package com.practice.util; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- java 使用POI批量导入excel数据
一.定义 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二.所需jar包: 三.简单的一个读取e ...
- java利用poi包 为excel生成超链接
转载自:http://www.blogjava.net/leekiang/archive/2008/10/21/235794.html 1,一个需求, 要求报表生成的Excel表格支持超链接.例如 ...
- 将eChart图片利用POI导出到Excel
在使用POI进行将数据导出到Excel时, 若要将eChart在前端生成的统计图(如柱状图.折线图.饼图等)一并导出,使用POI在后台构建数据图比较复杂,因此我选择将eChart在前端的统计图的bas ...
- C#导入Excel|读取Excel方法
OleDbConnection读取 /// <summary> /// 返回Excel数据源 /// </summary> /// < ...
- SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel
一.前言 今天小编带大家一起整合一下easyExcel,之所以用这个,是因为easyExcel性能比较好,不会报OOM! 市面上常见的导入导出Excel分为三种: hutool easyExcel p ...
随机推荐
- 学习unigui【20】unistringGrid
做成下面效果图: 采用unistringGrid控件. 问题: 1.不同的日期区间如何得到.如: 项目 开始时间时间 -- 终止使用时间 呼吸机 yyyy-mm-dd yyyy-mm-dd ...
- 大模型 Token 究竟是啥:图解大模型Token
前几天,一个朋友问我:"大模型中的 Token 究竟是什么?" 这确实是一个很有代表性的问题.许多人听说过 Token 这个概念,但未必真正理解它的作用和意义.思考之后,我决定写篇 ...
- [T.4] 团队项目:团队代码管理准备
团队的代码仓库地址 [GitHub - Meng-XuanYu/JayJay-TeamVersionControl: A public repo for BUAASE2025 course homew ...
- Java容器集合经典面试题集
目录 概述类面试题 1. 请说一下Java容器集合的分类,各自的继承结构 2. 请谈一谈Java集合中的fail-fast和fail-safe机制 3. 如何一边遍历一边删除Collection中的元 ...
- ipconfig出现媒体状态为媒体已断开连接问题
1.可能是因为路由器或者是交换机没有DHCP功能,需要手动的给电脑配置IP和掩码
- 康谋技术 | 揭秘汽车功能的核心——深度解读ADTF中的过滤器图
在汽车领域,ADTF(Automotive Data and Time-Triggered Framework)是一个强大的工具,用于开发切实可行的汽车功能和复杂的应用程序,实现数据的转换.记录和可视 ...
- 学习nodejs的一点笔记
>>1.模块:一个文件即为一个模块 1)global可以声明全局变量 (跨模块) 例如:global a = 100; console.log(global.a); //输出100 2 ...
- CSP-S 17天冲刺计划
var code = "91461527-5e0b-458f-ae4b-db46cf2a11c8" D1~D3(树专题复习)(OK\color{green}OKOK) 树基础(OK ...
- Python3_数据类型和变量
Python3_数据类型和变量 一.数据类型 Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) 在 ...
- linux期末考试题(1)
linux期末考试题 一.选择题(共20分,每小题2分) 1.以下哪个环境变量表示shell搜索外部命令或程序路径(C) A.ENV B.PWD C.PATH D.ROOT 解答: ENV用于显示当前 ...