...

package POIXLS;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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.ss.usermodel.WorkbookFactory; import java.sql.*; //import com.e6soft.base.service.JService;
//import com.e6soft.base.annotations.JCall;
//import com.e6soft.base.util.WebUtil; public class CS { public static List<Map<String, Object>> E() throws Exception{
/*用户先上传文件至服务器的临时位置,然后写一个读取excel的方法,程序从服务器的临时位置读取excel文件;
* 然后循环工作表和行,将单元格的内容存入集合;再上传至临时表(每次先清空),再根据sku(主键)更新正式数据库即可完成导入文件更新字段信息。*/ String widz = "C:\\Users\\killer\\Desktop\\参考\\工作簿1.xls"; // 文件在服务器的地址
// 获得文件所在地
File file = new File(widz); List<Map<String, Object>> qbsj = new ArrayList<Map<String,Object>>();
// 读取文件
try {
//同时支持Excel 2003、2007
//File excelFile = new File("F:\\MyEclipse 2017 CI\\SY\\src\\word\\aa.xls"); //创建文件对象
FileInputStream is = new FileInputStream(file); //文件流
Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量 //遍历每个Sheet
//for (int s = 0; s < sheetCount; s++) {
Sheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
//遍历每一行 // 前3行不读
int jl = 0; // 记录上一行多少
for (int r = 4; r < rowCount; r++) {
Row row = sheet.getRow(r);
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数 //遍历每一个单元格
for (int c = 0; c < cellCount; c++) {
Cell cell = row.getCell(c); // 序号
CellType cellType = cell.getCellType(); // STRING
String cellValue = null; //在读取单元格内容前,设置所有单元格中内容都是字符串类型
// cell.setCellType(CellType.STRING); // //设置单元格类型
cell.setCellType(CellType.STRING); // //设置单元格类型 //按照字符串类型读取单元格内数据
cellValue = cell.getStringCellValue(); //在这里可以对每个单元格中的值进行二次操作转化 System.out.println(">>>"+cellValue);
}
//
CellType BHcellType = row.getCell(0).getCellType(); // STRING
row.getCell(0).setCellType(BHcellType.STRING); // //设置单元格类型
String bh =row.getCell(0).getStringCellValue(); CellType XMMCcellType = row.getCell(1).getCellType(); // STRING
row.getCell(1).setCellType(XMMCcellType.STRING); // //设置单元格类型
String xmmc =row.getCell(1).getStringCellValue(); CellType HTMCcellType = row.getCell(2).getCellType(); // STRING
row.getCell(2).setCellType(HTMCcellType.STRING); // //设置单元格类型
String htmc =row.getCell(2).getStringCellValue(); CellType HTDSRcellType = row.getCell(3).getCellType(); // STRING
row.getCell(3).setCellType(HTDSRcellType.STRING); // //设置单元格类型
String htdsr =row.getCell(3).getStringCellValue(); CellType HTNRcellType = row.getCell(4).getCellType(); // STRING
row.getCell(4).setCellType(HTNRcellType.STRING); // //设置单元格类型
String htnr =row.getCell(4).getStringCellValue(); CellType WTFScellType = row.getCell(5).getCellType(); // STRING
row.getCell(5).setCellType(WTFScellType.STRING); // //设置单元格类型
String wtfs =row.getCell(5).getStringCellValue(); CellType HTJKcellType = row.getCell(6).getCellType(); // STRING
row.getCell(6).setCellType(HTJKcellType.STRING); // //设置单元格类型
String htjk =row.getCell(6).getStringCellValue(); CellType HTGQcellType = row.getCell(7).getCellType(); // STRING
row.getCell(7).setCellType(HTGQcellType.STRING); // //设置单元格类型
String htgq =row.getCell(7).getStringCellValue(); CellType QDSJcellType = row.getCell(8).getCellType(); // STRING
row.getCell(8).setCellType(QDSJcellType.STRING); // //设置单元格类型
String qdsj =row.getCell(8).getStringCellValue(); CellType XMZBBMcellType = row.getCell(9).getCellType(); // STRING
row.getCell(9).setCellType(XMZBBMcellType.STRING); // //设置单元格类型
String xmzbbm =row.getCell(9).getStringCellValue(); CellType XBRcellType = row.getCell(10).getCellType(); // STRING
row.getCell(10).setCellType(XBRcellType.STRING); // //设置单元格类型
String xbr =row.getCell(10).getStringCellValue(); CellType YJGDSJcellType = row.getCell(11).getCellType(); // STRING
row.getCell(11).setCellType(YJGDSJcellType.STRING); // //设置单元格类型
String yjgdsj =row.getCell(11).getStringCellValue(); CellType HTJXQKcellType = row.getCell(12).getCellType(); // STRING
row.getCell(12).setCellType(HTJXQKcellType.STRING); // //设置单元格类型
String htjxqk =row.getCell(12).getStringCellValue(); CellType YSRQcellType = row.getCell(13).getCellType(); // STRING
row.getCell(13).setCellType(YSRQcellType.STRING); // //设置单元格类型
String ysrq =row.getCell(13).getStringCellValue();
/*
CellType NFcellType = row.getCell(14).getCellType(); // STRING
row.getCell(14).setCellType(NFcellType.STRING); // //设置单元格类型
String nf =row.getCell(14).getStringCellValue(); */ row.getCell(14).setCellType(row.getCell(14).getCellType().STRING); // //设置单元格类型
String nf =row.getCell(14).getStringCellValue(); Map<String,Object> dataMap = new HashMap<String,Object>(); if(bh.equals("") && xmmc.equals("") && htmc.equals("")&& !htdsr.equals("")){ // 编号&项目&名称为空, 人不为空的情况
if(qbsj.get(jl-1).get("htdsr_y").equals("")){
qbsj.get(jl-1).put("htdsr_y",qbsj.get(jl-1).get("htdsr_y") + row.getCell(3).getStringCellValue());
}else{
qbsj.get(jl-1).put("htdsr_y",qbsj.get(jl-1).get("htdsr_y") +","+ row.getCell(3).getStringCellValue()); // 两个甲方,两个乙方 ,丙方,丁方,的情况
}
}else if(!bh.equals("") && xmmc.equals("") && htmc.equals("")){ // 合同编号不为空,项目名称 与合同名称为空的情况
// dataMap.put("bh", bh); // gs
dataMap.put("xmmc", qbsj.get(jl-1).get("xmmc"));
dataMap.put("htmc", qbsj.get(jl-1).get("htmc"));
if(!htdsr.equals("")){
if(htdsr.substring(0,1).equals("乙")){// 编号不为空,项目与名称为空的情况 其甲方没有的情况 也是甲方在上层的情况
dataMap.put("htdsr_j", qbsj.get(jl-1).get("htdsr_j"));
dataMap.put("htdsr_y", htdsr); }else{
dataMap.put("htdsr_j", htdsr);
dataMap.put("htdsr_y", "");
}
}
// 是否为空的情况
if(htnr.equals("")){
dataMap.put("htnr", qbsj.get(jl-1).get("htnr"));
}else{
dataMap.put("htnr", htnr);
}
if(wtfs.equals("")){
dataMap.put("wtfs", qbsj.get(jl-1).get("wtfs"));
}else{
dataMap.put("wtfs", wtfs);
}
if(htjk.equals("")){
dataMap.put("htjk", qbsj.get(jl-1).get("htjk"));
}else{
dataMap.put("htjk", htjk);
}
if(htgq.equals("")){
dataMap.put("htgq", qbsj.get(jl-1).get("htgq"));
}else{
dataMap.put("htgq", htgq);
}
if(qdsj.equals("")){
dataMap.put("qdsj", qbsj.get(jl-1).get("qdsj"));
}else{
dataMap.put("qdsj", qdsj);
}
if(xmzbbm.equals("")){
dataMap.put("xmzbbm", qbsj.get(jl-1).get("xmzbbm"));//
}else{
dataMap.put("xmzbbm", xmzbbm);//
}
if(xbr.equals("")){
dataMap.put("xbr", qbsj.get(jl-1).get("xbr"));
}else{
dataMap.put("xbr", xbr);
}
if(yjgdsj.equals("")){
dataMap.put("yjgdsj", qbsj.get(jl-1).get("yjgdsj"));
}else{
dataMap.put("yjgdsj", yjgdsj);
}
if(htjxqk.equals("")){
dataMap.put("htjxqk", qbsj.get(jl-1).get("htjxqk"));
}else{
dataMap.put("htjxqk", htjxqk);
}
if(ysrq.equals("")){
dataMap.put("ysrq", qbsj.get(jl-1).get("ysrq"));
}else{
dataMap.put("ysrq", ysrq);
}
dataMap.put("nf", nf);
qbsj.add(dataMap);
jl++;
// }else if(){ // }else if(!bh.equals("") && xmmc.equals("") && !htmc.equals("")){ // 编号不为空, 项目为空, 名称不为空的情况 dataMap.put("bh", bh); // gs
dataMap.put("xmmc", qbsj.get(jl-1).get("xmmc"));
dataMap.put("htmc", htmc);
dataMap.put("htdsr_j", htdsr);
dataMap.put("htdsr_y", "");
dataMap.put("htnr", htnr);
dataMap.put("wtfs", wtfs);
dataMap.put("htjk", htjk);
dataMap.put("htgq", htgq);
dataMap.put("qdsj", qdsj);
dataMap.put("xmzbbm", xmzbbm);//
dataMap.put("xbr", xbr);
dataMap.put("yjgdsj", yjgdsj);
dataMap.put("htjxqk", htjxqk);
dataMap.put("ysrq", ysrq);
dataMap.put("nf", nf); qbsj.add(dataMap);jl++; }else if(bh.equals("") && !xmmc.equals("") && !htmc.equals("")){ //编号为空,项目与名称不为空的情况 dataMap.put("bh", qbsj.get(jl-1).get("bh")); // gs
dataMap.put("xmmc", xmmc);
dataMap.put("htmc", htmc);
dataMap.put("htdsr_j", htdsr);
dataMap.put("htdsr_y", "");
dataMap.put("htnr", htnr);
dataMap.put("wtfs", wtfs);
dataMap.put("htjk", htjk);
dataMap.put("htgq", htgq);
dataMap.put("qdsj", qdsj);
dataMap.put("xmzbbm", xmzbbm);//
dataMap.put("xbr", xbr);
dataMap.put("yjgdsj", yjgdsj);
dataMap.put("htjxqk", htjxqk);
dataMap.put("ysrq", ysrq);
dataMap.put("nf", nf); qbsj.add(dataMap);jl++; }else if(!bh.equals("") && !xmmc.equals("") && !htmc.equals("")){ // 正常 dataMap.put("bh", bh); // gs
dataMap.put("xmmc", xmmc);
dataMap.put("htmc", htmc);
dataMap.put("htdsr_j", htdsr);
dataMap.put("htdsr_y", "");
dataMap.put("htnr", htnr);
dataMap.put("wtfs", wtfs);
dataMap.put("htjk", htjk);
dataMap.put("htgq", htgq);
dataMap.put("qdsj", qdsj);
dataMap.put("xmzbbm", xmzbbm);//
dataMap.put("xbr", xbr);
dataMap.put("yjgdsj", yjgdsj);
dataMap.put("htjxqk", htjxqk);
dataMap.put("ysrq", ysrq);
dataMap.put("nf", nf); qbsj.add(dataMap);jl++; }else if(bh.equals("") && xmmc.equals("") && htmc.equals("")&& htdsr.equals("")){ // 都为空的情况 // 有年份的情况
System.out.println(">>>>>>>>>>>>>>>>>>>>>>> 都为空的情况 <<<<<<<<<<<<<<<<<<<<<<");
}else{
System.out.println(">>>>>>>>>>>>>>>>>>>>>>> 未发现的情况 <<<<<<<<<<<<<<<<<<<<<<");
}
/*
// 定义每一行的变量 //String ID = parseCell(row.getCell(0)); Map<String,Object> dataMap = new HashMap<String,Object>();
// 顺序一致!!
//dataMap.put("DLRID", I); //DLRID 登录人id dataMap.put("GS", ""); // gs // System.out.println(dataMap);
// 加入
//qbsj.add(dataMap);*/ }
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println(">>>"+qbsj);
return qbsj; }
catch (Exception e) {
e.printStackTrace();
return qbsj;
}
} public static void main(String[] args) throws Exception{ List<Map<String, Object>> sj = E(); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//System.out.println(">>>"+fh);
} }

1.合同编号&项目名称&合同名称为空, 合同当事人不为空的情况

2.合同编号不为空,项目名称 与合同名称为空的情况

2.1合同编号不为空,项目名称与合同名称为空的情况       其甲方没有的情况 也是甲方在上层的情况

3.合同编号不为空, 项目名称为空, 合同名称不为空的情况

4.合同编号为空,项目名称与合同名称不为空的情况

5.正常

6.// 都为空的情况 // 有年份的情况

POI 4.0 读取Excel的更多相关文章

  1. Microsoft.Jet.OLEDB.4.0读取EXCEL数据

    用Microsoft.Jet.OLEDB.4.0读取EXCEL数据的代码是这样的:     string ConnStr="Provider=Microsoft.Jet.OLEDB.4.0; ...

  2. 使用Apache下poi创建和读取excel文件

    一:使用apache下poi创建excel文档 @Test /* * 使用Apache poi创建excel文件 */ public void testCreateExcel() { // 1:创建一 ...

  3. POI原生导入读取EXCEL

    好久没用 最近项目有冲突 所以又用到了这个 谁知道以后还会不会用 先记下来吧 直接扔项目里 调方法就OK 了. 记录一下....不想再写类似这样的东西了 import org.apache.poi.h ...

  4. Java小知识----POI事件模式读取Excel 2007

    一.知识背景 1.读取excel的方法选择问题 java中读excel中的时间,我们通常用POI去解析,在使用new HSSFWorkbook(NEW FileInputStream(excelFil ...

  5. 结合Poi实现可读取Excel的文件选择对话框

    第一步:ApachePoi的jar包导全,不全会出现异常. 第二步:写就完事了:此例为读取特定模板的excel,仅供参考,根据实际需求改写. package 自建包; import java.awt. ...

  6. VB6.0 读取Excel文件并返还数据集RecordSet

    读取Excel文件并返还数据集RecordSet 该方法适用于.xls,.xlsx类型的文件 读取Excel文件的Function: '取得数据集 Function getRecordSetForEx ...

  7. VC6.0读取Excel文件数据

    啰嗦一下:本人所在公司从事碟式斯特林太阳能发电设备的研发与销售.单台设备图如下: 工作原理如下:整个设备大致可分为五个部分, 1.服务器,负责气象.发电等数据存取,电.网连接等处理: 2.气象站,通过 ...

  8. java使用poi.3.10读取excel 2003 (xls格式)

    最近在做一个Excel导入数据库的案例,整理文档出来供大家参考. 1.下载 最新的 poi http://poi.apache.org/download.html    2.解压 把相关jar包引进项 ...

  9. 用POI 3.17读取EXCEL数据

    导入jar 包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

随机推荐

  1. ❤️【Python从入门到精通】(二十六)用Python的PIL库(Pillow)处理图像真的得心应手❤️

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本篇重点介绍Python处理图像的标准库PIL库,处理图像真的的很方便. 干货满满,建议收藏,需要用到时常看看. 小伙伴们如有问题及需要,欢迎踊跃 ...

  2. Dubbo与Zookeeper简单理解

    理论 在<分布式系统原理与范型>一书中有如下定义:"分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像单个相关系统 "; 分布式系统是由一组通过网络进行通信. ...

  3. Python:安装opencv出现错误Could not find a version that satisfies the requirement numpy==1.13.3 (from versions: 1.14.5, 1.14.6, 1.15.0rc2, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0rc1, 1.16.0rc2,

    安装opencv的时候,出现numpy的版本不匹配,卸载了不匹配的版本,重新安装却是一点用都没有,后面尝试了一下这里的提示pip更新,居然安装成功了,看来pip的版本过低真是误事啊. 报错是: Cou ...

  4. Vue自定义标签页,并且在其中渲染Echarts图表

    目录 一.需求说明 二.标签页功能实现 一.需求说明 1.点击标签按钮切换不同的echarts图表,考虑用Ant Design Vue,但是其样式无法自定义 2.div的整体布局样式使用tailwin ...

  5. 第21篇-加载与存储指令之iload、_fast_iload等(3)

    iload会将int类型的本地变量推送至栈顶.模板定义如下: def(Bytecodes::_iload , ubcp|____|clvm|____, vtos, itos, iload , _ ); ...

  6. 洛谷4248 AHOI2013差异 (后缀数组SA+单调栈)

    补博客! 首先我们观察题目中给的那个求\(ans\)的方法,其实前两项没什么用处,直接\(for\)一遍就求得了 for (int i=1;i<=n;i++) ans=ans+i*(n-1); ...

  7. 洛谷2093 JZPFAR + KD-Tree学习笔记 (KD-Tree)

    KD-Tree这玩意还真的是有趣啊.... (基本完全不理解) 只能谈一点自己的对KD-Tree的了解了. 首先这个玩意就是个暴力... 他的结构有点类似二叉搜索树 每一层都是以一个维度作为划分标准. ...

  8. 云原生的弹性 AI 训练系列之三:借助弹性伸缩的 Jupyter Notebook,大幅提高 GPU 利用率

    Jupyter Notebooks 在 Kubernetes 上部署往往需要绑定一张 GPU,而大多数时候 GPU 并没有被使用,因此利用率低下.为了解决这一问题,我们开源了 elastic-jupy ...

  9. pg_basebackup报错: pg_basebackup: incompatible server version 12.4

    pg_basebackup报错 今日从库复制主库data时,发现pg_basebackup无法使用,详情如下: 错误为:incompatible server version 12.4 [postgr ...

  10. 【死磕 NIO】— Reactor 模式就一定意味着高性能吗?

    大家好,我是大明哥,我又来了. 为什么是 Reactor 一般所有的网络服务,一般分为如下几个步骤: 读请求(read request) 读解析(read decode) 处理程序(process s ...