...

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. Visaul Studio Code中提示 vue:无法加载vue.ps1,未对vue.ps1进行数字签名

    Visaul Studio Code错误提示 错误如图: 解决办法 首先以管理员身份打开windows PowShell终端. 输入下面命令,如提示选择Y即可. get-help set-execut ...

  2. Windows10通过WSL编译jdk12

    Windows使用WSL编译OpenJDK 安装Ubuntu以及配置国内镜像 首选确保windows10已经安装了ubuntu 更换ubuntu20.04国内镜像,这里我选择的是阿里云镜像 sudo ...

  3. MyBatis 面试复习整理

    MyBatis MyBatis 是一款优秀的ORM(对象关系映射)框架,可以通过对象和数据库之间的映射,将程序中的对象自动存储到数据库中.它内部封装了 JDBC ,使开发者只需要关注 SQL语句本身, ...

  4. javascriptRemke之原型的重要性

    前言:JavaScript的原型对象一直是新人学习js的一大重大阻碍,但是原型的知识往往又是面试中常常会被深挖的一个点,为什么会这样呢?本文带你揭秘JavaScript原型的重要性,了解重要性之后再进 ...

  5. win10安装git fatal: open /dev/null or dup failed: No such file or directory错误解决方法

    原因看大家意思应该是 非即插即用驱动文件null.sys问题. 网上有很多方案.最后试了一个可行的. 替换  windows/system32/drivers/null.sys为网盘中的文件即可. 链 ...

  6. “介绍一下自己吧”——记2020BUAA软工团队介绍和采访

    写在前面 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任建) 这个作业的要求在哪里 团队作业-团队介绍和采访 团队介绍 团队名称 我们是 BUAA软软软件工程小队 ,简称 ...

  7. [no code][scrum meeting] Alpha 2

    项目 内容 会议时间 2020-04-07 会议主题 功能规格说明书review 会议时长 30min 参会人员 OCR组(肖思炀,赵涛)和产品经理 $( "#cnblogs_post_bo ...

  8. 『学了就忘』Linux基础 — 4、VMware安装

    目录 1.VMware介绍 2.VMware主要特点 3.VMware建议配置 4.VMware安装 1.VMware介绍 VMware是一个虚拟PC的软件,可以在现有的操作系统上虚拟出一个新的硬件环 ...

  9. 单片机零基础学习之从“点灯”入门STM32

    本篇文章通过一个简单的例子来熟悉模块化编程以及利用库函数的方法进行开发使用STM32外设的基本流程. 首先,我们打开本讲的例程,在工程目录我们可以看到驱动分组下有 led.delay 两个.c源文件, ...

  10. PriorityQueue(优先队列)

    PriorityQueue 翻译过来就是优先队列,本质是一个堆, 默认情况下堆顶每次都保留最小值,每插入一个元素,仍动态维护堆顶为最小值. PriorityQueue 一个基于优先级的无界优先级队列. ...