package cn.cnnic.ops;

import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.Set;
import java.util.Map.Entry; import javax.swing.JFrame; 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; /**
*
* @author zhzhang
* 实现根据排班表,计算每个人监控室值班的个数(分白班和夜班)
*/
public class DutyRosterVerificationFrame { public static void main(String[] args) {
/**
* 初始化JFrame
*/
final JFrame jframe = new JFrame("值班表数据汇总");
/**
* 设置布局方式
*/
Panel flowLayoutPanel = new Panel();
flowLayoutPanel.setLayout(new FlowLayout());
/**
* 需要计算的文件位置和名称
*/
final TextField tfFileName = new TextField();
tfFileName.setColumns(50);
tfFileName.setText("此处显示选定的排班表");
tfFileName.setEditable(true);
/**
* 计算结果:每个人监控室值班的个数(分白班和夜班)
*/
final TextArea taResult = new TextArea();
taResult.setLocation(50, 50);
taResult.setSize(500, 500);
taResult.setText("此处显示白班和夜班的个数");
Button btnOpen = new Button("Open");
/**
* 按钮打开事件
*/
btnOpen.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(jframe,"打开文件",FileDialog.LOAD);
fd.setVisible(true);
String fileName = fd.getDirectory()+fd.getFile();
tfFileName.setText(fileName);
}
});
/**
* 打开按钮的大小
*/
btnOpen.setSize(100, 50);
/**
* 打开按钮的位置
*/
btnOpen.setLocation(100, 100); /**
* 初始化计算按钮
*/
Button btnCal = new Button("Calculate");
btnCal.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
try {
String strResult = getDataFromExcel(tfFileName.getText().toString());
taResult.setText(strResult);
} catch (InvalidFormatException | IOException e1) {
e1.printStackTrace();
}
}
}); flowLayoutPanel.add(btnOpen);
flowLayoutPanel.add(tfFileName);
flowLayoutPanel.add(btnCal);
jframe.add(taResult); jframe.add(flowLayoutPanel);
jframe.setSize(600, 600);
jframe.setLocation(100, 100);
jframe.setVisible(true);
} /**
*
* @param file
* @return 计算结果
* @throws FileNotFoundException
* @throws IOException
* @throws InvalidFormatException
*/
public static String 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);
}
}
}
}
}
return outputSortReturn("白班", dayShift, dutyMap)+outputSortReturn("夜班", nightShift, dutyMap);//
} /**
*
* @param str
* 说明白班还是夜班
* @param map
* 员工及值班个数HashMap
* @param mapDim
* 值班维表
* @return
* 计算结果
*/
public static String outputSortReturn(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());
}
});
String strResult = "===================" + str + "======================"+"\n";
strResult += "代号\t姓名\t数量\n";
for (int index = 0; index < list.size(); index++) {
strResult += list.get(index).getKey() + "\t" + mapDim.get(list.get(index).getKey()) + "\t"
+ list.get(index).getValue()+"\n";
}
return strResult;
} /**
*
* @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数据,并统计输出Frame版本的更多相关文章

  1. JAVA反射机制示例,读取excel数据映射到JAVA对象中

    import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...

  2. jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL

    这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...

  3. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

  4. Java读取Excel数据

    Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 下图是excel文件的路径和文件名 下图是exce ...

  5. java的poi技术读取Excel数据

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  6. 性能测试工具LoadRunner27-LR之读取Excel数据

    为何要读取Excel数据? 很多用户喜欢用Excel来统计数据,比如学生成绩表.个人信息等.有时需要把Excel中的数据来进行参数化,数据量比较多时,一个个在LR里输入是不现实的,因此需要用LR来导入 ...

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

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

  8. 猜想-未做 利用office组件读取excel数据

    ---未实际使用过 用SQL-Server访问Office的Access和Excel http://blog.sina.com.cn/s/blog_964237ea0101532x.html 2007 ...

  9. poi——读取excel数据

    单元格类型 读取Excel数据 package com.java.test.poi; import java.io.File; import java.io.FileInputStream; impo ...

随机推荐

  1. int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    1:int.Parse(一个参数)        此参数必须满足: 1 只能是字符串: 2 只能是 “整型” 字符串,即各种整型ToString()之后的形式,也不能为浮点型. 2:int.TryPa ...

  2. 压力测试报出503错误---ASP.NET支持大并发的相关配置

    项目反馈报出503错误,需要收集性能数据如下: 1.Windows性能监视器,该应用程序池进程的线程和处理队列 2.问题重现时的进程dump 这是请求到达IIS后遇到的第一个队列,HTTP.sys收到 ...

  3. js封装包

    (function () { //check the class name , it will be replaced when existed if (window.IQCBase) { //ret ...

  4. C 最熟悉的陌生人 (纪念当年就读的梅州市江南高级中学)

    最熟悉的陌生人 作者:张慧桥 “枪与玫瑰” 我送走了“蝶恋花”,犹有一种身在梦中的感觉,昨晚的宿醉让我只觉得头晕乎乎的很不舒服,想想自己连澡都还没洗呢,便去洗了个冷水澡. 煮了杯浓浓的咖啡喝了下去,我 ...

  5. 【转】JSch - Java实现的SFTP(文件上传详解篇)

    JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到 ...

  6. android学习笔记57——电话管理器TelephoneyManager

    电话管理器TelephoneyManager

  7. [转]Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only

    Okay for centos 6.4 also On apu.0xdata.loc, after this install was done $ which python /usr/local/bi ...

  8. ORA-01747: user.table.column, table.column 或列说明无效

    Oracle.DataAccess.Client.OracleException ORA-01747: user.table.column, table.column 或列说明无效 原因1: 查了一下 ...

  9. 超链接实现post方式提交

    思路:如果想要超链接实现post方式提交,必须借助表单.下面得两种方式,一种是借助显示的form表单,一种是借助隐式的form表单方式一:将超链接放到一个form表单中,或者超链接本身就在一个form ...

  10. linux pxe+dhcp+nfs+tftp

    yum -y install vsftpd dhcp xinetd tftp-server syslinux(安装"syslinux"才有pxelinux.0) tftp 服务(v ...