到岗4月有余,项目有条不紊的进行着,一切感觉还是非常顺利的。除了本职的工作,我有了新的任务,进行工时的统计,这个活说来非常简单就是从excel表中整理所有员工的工时情况记录到word中,但是每次发布的excel表数据忒长了。。。看着看着就晕头转向,也罢,开发个小工具解决这个问题。由于涉及到了excel、word的读写,所以我选择了apache的poi作为读写的工具包。
poi的maven倚赖如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.8</version>
</dependency>
excel的相关操作:
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA: {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
cellvalue = sdf.format(date);
}
else {
cellvalue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case HSSFCell.CELL_TYPE_STRING:
cellvalue = cell.getRichStringCellValue().getString();
break;
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
@SuppressWarnings("deprecation")
public Map<String, List<WorkTime>> readExcelContent(InputStream is) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
HSSFSheet sheet = null;
HSSFRow row = null;
Map<String, List<WorkTime>> content = new HashMap<String, List<WorkTime>>();
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int userNameIndex = 1;
int startTimeIndex =2;
int endTimeIndex=3;
int workHoursIndex=4;
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
String userName = getCellFormatValue(row.getCell((short) userNameIndex)).trim();
String startTimeStr = getCellFormatValue(row.getCell((short) startTimeIndex)).trim();
String endTimeStr = getCellFormatValue(row.getCell((short) endTimeIndex)).trim();
String workHoursStr = getCellFormatValue(row.getCell((short) workHoursIndex)).trim();
WorkTime wt= new WorkTime();
wt.setStartTime(startTimeStr);
wt.setEndTime(endTimeStr);
wt.setWorkHous(workHoursStr);
if (content.containsKey(userName)){
content.get(userName).add(wt);
}else{
List<WorkTime> workTimelist = new ArrayList<WorkTime>();
workTimelist.add(wt);
content.put(userName,workTimelist);
}
}
return content;
}
word的相关操作:
public static void readwriteWord(String sourceFilePath,
String destFilePath, Map<String, String> map) {
FileInputStream in = null;
try {
in = new FileInputStream(new File(sourceFilePath));
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
}
HWPFDocument hdt = null;
try {
hdt = new HWPFDocument(in);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
Range range = hdt.getRange();
for (Map.Entry<String, String> entry : map.entrySet()) {
range.replaceText("$" + entry.getKey() + "$", entry.getValue());
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
FileOutputStream out = null;
try {
out = new FileOutputStream(destFilePath, true);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
}
try {
hdt.write(ostream);
out.write(ostream.toByteArray());
out.close();
ostream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
愿脑哥的光辉指引我前进的方向