读取excel数据,并进行统计输出
package cn.cnnic.ops; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; public class DutyRosterVerification { public static void main(String[] args) {
try {
// getDataFromExcel("D:\\soft\\eclipse\\workspace\\DutyRoster\\src\\20160726_2016年8月份值班表.xlsx");
getDataFromExcel("D:\\Data\\xxx.xlsx");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
} public static void getDataFromExcel(String file) throws FileNotFoundException, IOException, InvalidFormatException {
InputStream ins = null;
Workbook wb = null;
ins = new FileInputStream(new File(file));
wb = WorkbookFactory.create(ins);
ins.close();
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum(); Map<String, String> dutyMap = new HashMap<String, String>();
dutyMap.put("1", "***");
dutyMap.put("2", "***");
dutyMap.put("3", "***");
dutyMap.put("4", "***");
dutyMap.put("5", "***");
dutyMap.put("6", "***");
dutyMap.put("7", "***"); Map<String, Integer> dayShift = new HashMap<String, Integer>();
Map<String, Integer> nightShift = new HashMap<String, Integer>(); // System.out.println(sheet.getRow(3).getCell(2).toString().split("\n")[0]);
for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
Row rowCurrent = sheet.getRow(rowIndex);
if (rowIndex >= 3 && (rowIndex - 3) % 4 == 0) {
for (int colIndex = 2; colIndex <= 8; colIndex++) {
Cell cellCurrent = rowCurrent.getCell(colIndex);
if (cellCurrent != null) {
cellCurrent.setCellType(Cell.CELL_TYPE_STRING);
}
String team = cellCurrent.toString().trim().split("\n")[0];
String[] teamPerson = team.split(",");
for (int teamIndex = 0; teamIndex < teamPerson.length; teamIndex++) {
if (dayShift.get(teamPerson[teamIndex]) == null) {
dayShift.put(teamPerson[teamIndex], 1);
} else {
dayShift.put(teamPerson[teamIndex], dayShift.get(teamPerson[teamIndex]) + 1);
}
}
}
} else if (rowIndex >= 4 && (rowIndex - 4) % 4 == 0) {
for (int colIndex = 2; colIndex <= 8; colIndex++) {
Cell cellCurrent = rowCurrent.getCell(colIndex);
if (cellCurrent != null) {
cellCurrent.setCellType(Cell.CELL_TYPE_STRING);
}
String team = cellCurrent.toString().trim().split("\n")[0];
String[] teamPerson = team.split(",");
for (int teamIndex = 0; teamIndex < teamPerson.length; teamIndex++) {
if (nightShift.get(teamPerson[teamIndex]) == null) {
nightShift.put(teamPerson[teamIndex], 1);
} else {
nightShift.put(teamPerson[teamIndex], nightShift.get(teamPerson[teamIndex]) + 1);
}
}
}
}
} outputSort("白班", dayShift, dutyMap);
outputSort("夜班", nightShift, dutyMap);
} /**
*
* @param str
* 说明白班还是夜班
* @param map
* 员工及值班个数HashMap
* @param mapDim
* 值班维表
*/
public static void outputSort(String str, Map<String, Integer> map, Map<String, String> mapDim) { List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return -(o1.getValue() - o2.getValue());
}
}); System.out.println("===================" + str + "======================");
for (int index = 0; index < list.size(); index++) {
System.out.println(list.get(index).getKey() + "==" + mapDim.get(list.get(index).getKey()) + "=="
+ list.get(index).getValue());
}
} /**
*
* @param str
* 说明白班还是夜班
* @param map
* 员工及值班个数HashMap
* @param mapDim
* 值班维表
*/
public static void output(String str, Map<String, Integer> map, Map<String, String> mapDim) {
System.out.println("===================" + str + "======================");
Set<Entry<String, Integer>> dayEntities = map.entrySet();
for (Entry<String, Integer> en : dayEntities) {
System.out.println(en.getKey() + "---" + mapDim.get(en.getKey()) + "---" + en.getValue());
}
}
}
读取excel数据,并进行统计输出的更多相关文章
- 读取excel数据,并统计输出Frame版本
package cn.cnnic.ops; import java.awt.Button; import java.awt.FileDialog; import java.awt.FlowLayout ...
- JAVA反射机制示例,读取excel数据映射到JAVA对象中
import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...
- jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL
这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...
- Java读取Excel数据
Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 下图是excel文件的路径和文件名 下图是exce ...
- 性能测试工具LoadRunner27-LR之读取Excel数据
为何要读取Excel数据? 很多用户喜欢用Excel来统计数据,比如学生成绩表.个人信息等.有时需要把Excel中的数据来进行参数化,数据量比较多时,一个个在LR里输入是不现实的,因此需要用LR来导入 ...
- java的poi技术读取Excel数据到MySQL
这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...
- .NET读取Excel数据,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
解决.NET读取Excel数据时,提示错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序的操作: 1. 检查本机是否安装Office Access,如果未安装去去h ...
- oledbdataadapter 读取excel数据时,有的单元格内容不能读出
表现:excel中某列中,有的单元格左上角有绿色箭头标志,有的没有,c#编写读取程序,但是只能读取出带绿色箭头的单元格中的内容,其余不带的读取不到内容 原因:excel中单元格因为是文本格式而存储了数 ...
- Openxml入门---Openxm读取Excel数据
Openxml读取Excel数据: 有些问题,如果当Cell 里面是 日期和浮点型的话,对应的Cell.DataType==Null,对应的时间会转换为一个浮点型,对于这块可以通过DateTime.F ...
- C# 读取EXCEL数据
/// <summary> /// 读取EXCEL数据 /// </summary> /// <param name="Path">< ...
随机推荐
- VPS/服务器优化网络、加速方法总结与参考
在国外的服务器上因为受各种因素影响,即使国外的服务器都是百兆共享或者G口到国内下载速度都不是很让人满意,大部分人购买国外服务器是用作存储下载或者扶墙,速度慢的所以话影响我们的使用体验.所以就搞出了很多 ...
- ios 利用airprint实现无线打印(配合普通打印机)
很多时候,我们不可能为了一个打印功能而去替换打印机,来支持无线打印. 找了很久,终于找到了一种解决方案,来适应大多数没有带wifi功能的打印机支持无线打印. 前提: 有wifi路由器:因为手机要打印, ...
- [转]编译安装libevent,memcache,以及php的memcached扩展
一 安装libevent 1.去官网http://libevent.org/ 下载最新源码,我用的是libevent-2.0.20-stable.tar.gz 2.解压到/usr/src目录 ,执行命 ...
- VR外包团队:长年承接VR虚拟现实外包(应用、游戏、视频、漫游等)
北京动点飞扬软件,从事外包业务五年,长年承接全景VR视频,全景普通视频外包. 以下是全景VR视频案例(可操作,人不动景物不动,人移动,景物跟随) 欢迎联系我们QQ:372900288 TEL:1391 ...
- 【solr】之solr界面查询返回距离并排序
使用solr界面查询 {!geofilt}距离函数 star:[4 TO 5]星级排序 geodist() desc 距离排序 pt :31.221717,121.580891 sfield:loca ...
- C# HmacMD5 加密
string HmacMD5(string source, string key) { HMACMD5 hmacmd = new HMACMD5(Encoding.Default.GetBytes(k ...
- ruby中tes-unitt数据初始化方法整理
在用ruby做测试时,很多时候需要一些数据初始化以及事后的数据恢复还原之类的操作,下面整理了这些方法.require "test/unit" class TestAnion < ...
- c# MessageBox 用法大全
我们在程序中经常会用到MessageBox. 1.MessageBox.Show("Hello~~~~"); 最简单的,只显示提示信息. 2.MessageBox.Show(&qu ...
- Mybatis获取插入记录的自增长ID(转)
1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <ins ...
- yum命令——安装、卸载、查询等
--常用命令 1.安装软件 yum install 软件名称 2.卸载软件 yum remove 软件名称 3.更新软件 yum update 软件名称 4.列出所有可安装的软件包 yum list ...