图为要导入的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 读取合并行数据(上)的更多相关文章

  1. 利用kettle组件导入excel文件到数据库

    利用kettle组件导入excel文件到数据库 1.     实现目标 把excel文件内容导入到目标表中:然后用java调用kettle的转换.excel文件的内容仅仅有两列,示比例如以下: wat ...

  2. POI异步导入Excel兼容xsl和xlsx

    项目架构:spring+struts2+hibernate4+oracle 需求:用户导入excel文件,导入到相应的数据表中,要求提供导入模板,支持xls和xlsx文件 思路分析: 1.提供一个下载 ...

  3. Java利用POI实现导入导出Excel表格示例代码

    转自:https://www.jb51.net/article/95526.htm 介绍 Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其 ...

  4. SSH框架使用poi插件实现Excel的导入导出功能

    采用POI生成excel结构 直接贴出代码  excel表格导出功能 action代码: struts.xml配置: 前台jsp代码:

  5. poi批量导入excel文件

    package com.practice.util; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...

  6. java 使用POI批量导入excel数据

    一.定义 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二.所需jar包: 三.简单的一个读取e ...

  7. java利用poi包 为excel生成超链接

    转载自:http://www.blogjava.net/leekiang/archive/2008/10/21/235794.html   1,一个需求, 要求报表生成的Excel表格支持超链接.例如 ...

  8. 将eChart图片利用POI导出到Excel

    在使用POI进行将数据导出到Excel时, 若要将eChart在前端生成的统计图(如柱状图.折线图.饼图等)一并导出,使用POI在后台构建数据图比较复杂,因此我选择将eChart在前端的统计图的bas ...

  9. C#导入Excel|读取Excel方法

    OleDbConnection读取 /// <summary>       /// 返回Excel数据源       /// </summary>       /// < ...

  10. SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel

    一.前言 今天小编带大家一起整合一下easyExcel,之所以用这个,是因为easyExcel性能比较好,不会报OOM! 市面上常见的导入导出Excel分为三种: hutool easyExcel p ...

随机推荐

  1. 流式计算(四)-Flink Stream API 篇二

    个人原创文章,禁止任何形式转载,否则追究法律责任! 本文只发表在"公众号"和"博客园",其他均属复制粘贴!如果觉得排版不清晰,请查看公众号文章. 话说看图看核心 ...

  2. C# 计算代码的运行时间

    用法 主要通过Stopwatch类来实现... 在开发.调试.测试.分析中非常实用. Stopwatch sw = new Stopwatch(); sw.Start(); // 某些耗时的计算或任务 ...

  3. .net WorkFlow 流程定义

    WikeFlow官网:www.wikesoft.com WikeFlow学习版演示地址:workflow.wikesoft.com WikeFlow学习版源代码下载:https://gitee.com ...

  4. 康谋分享 | 3DGS:革新自动驾驶仿真场景重建的关键技术

    登录后复制 随着自动驾驶技术的迅猛发展,构建高保真.动态的仿真场景成为了行业的迫切需求.传统的三维重建方法在处理复杂场景时常常面临效率和精度的挑战.在此背景下,3D高斯点阵渲染(3DGS)技术应运而生 ...

  5. 工良出品 | 长文讲解 MCP 和案例实战

    作者:痴者工良 博客地址:https://www.whuanle.cn/ 示例项目地址:https://github.com/whuanle/mcpdemo 近期 MCP 协议越来越爆火,很多开发者都 ...

  6. 多线程——ThreadPool

    参考:第三节:ThreadPool的线程开启.线程等待.线程池的设置.定时功能 - Yaopengfei - 博客园 (cnblogs.com) C# AppDomain 详解_勇于尝试,却要三思后行 ...

  7. 贪心算法——Demo1

    题干: 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干. 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j,都有 ...

  8. BURP APP HTTPS抓包xposed+justtrustme工具篇

    APP HTTPS抓包 当APP是HTTPS时,则单纯的使用Burpsuite无法抓取数据包,原因是APP启用了SSL Pinning(又叫做"SSL证书绑定"). 1.下载夜神模 ...

  9. 基于SaaS纯BS架构的全院级PACS系统

           2014年曾经做过一版简单的Dicom Web Viewer,之前的Web版本由于技术和功能的极限性,仅能简单的运用于临床阅片和患者的电子胶片使用,无法普及到放射和超声等影像科室.影像科 ...

  10. P6375 「StOI-1」小Z的旅行 题解

    题意:P6375 「StOI-1」小 Z 的旅行 给定一座山,每座山有一个高度,只能向更低的山走或者向高度相同的山走,要求不能向高度相同的山连续走两次,不能原地不动. 每次走的权值都是两座山之间的坐标 ...