package cn.shop.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
public class ReportExcel {
public static SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd");
public static SimpleDateFormat weekFrom = new SimpleDateFormat("EEE");
public static void ttt() {
Date startDate = new Date();
System.out.println("开始时间: " + Tools.fdf.format(startDate));
String filePath = "D:\\data\\ttceshi.xls";
try {
FileOutputStream fileOut = new FileOutputStream("D:\\ttceshi.xls");
HSSFWorkbook workbook = null;
InputStream fis = new FileInputStream(filePath);
workbook = new HSSFWorkbook(fis);
for (int i = 0; i < 450; i++) {
HSSFSheet newsheet = null;
HSSFSheet fromsheet = workbook.getSheet("sheet1");
newsheet = workbook
.createSheet("tt_" + (String.valueOf(i + 1)));
copyRows(workbook, fromsheet, newsheet, fromsheet
.getFirstRowNum(), fromsheet.getLastRowNum());
}
Date end1Date = new Date();
System.out.println("结束时间1: " + Tools.fdf.format(end1Date));
setNewSheetData(workbook);
workbook.removeSheetAt(0);
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
Date end2Date = new Date();
System.out.println("结束时间2: " + Tools.fdf.format(end2Date));
}
@SuppressWarnings("deprecation")
public static void setNewSheetData(HSSFWorkbook workbook) {
try {
for (int i = 0; i < 450; i++) {
HSSFSheet sheetNow = workbook.getSheet("tt_"
+ (String.valueOf(i + 1)));
HSSFRow row = sheetNow.getRow(1);
row.getCell(4).setCellValue("七月到八月中旬");// 统计月份
row.getCell(9).setCellValue("2012-07-01到2012-08-20");// 统计时间段
row.getCell(15).setCellValue("测试事业部" + String.valueOf(i + 1));// 统计事业部名称
row.getCell(20).setCellValue("测试Test" + String.valueOf(i + 1));// 统计部门名称
row.getCell(25).setCellValue("测试员工" + String.valueOf(i + 1));// 统计员工中文名称
row.getCell(30).setCellValue("ceshi-" + String.valueOf(i + 1));// 统计员工的英文名
// 第六列总计赋值
row = sheetNow.getRow(5);
// 得到统计范围内的总数
row.getCell(6).setCellValue("1:00:00");// 迟到累计时间
row.getCell(7).setCellValue("2:00:00");// 早退累计时间
row.getCell(8).setCellValue("3:00:00");// 自动调休累计时间
row.getCell(9).setCellValue("4:00:00");// 缺勤累计时间
row.getCell(14).setCellValue("5:00:00");// 请假累计时间
row.getCell(15).setCellValue("6:00:00");// 请假累计时间-调休
row.getCell(16).setCellValue("7:00:00");// 请假累计时间-年假
row.getCell(17).setCellValue("8:00:00");// 请假累计时间-事假
row.getCell(18).setCellValue("9:00:00");// 请假累计时间-病假
row.getCell(19).setCellValue("10:00:00");// 请假累计时间-婚/丧/产假
row.getCell(25).setCellValue("11:00:00");// A补贴累计23前
row.getCell(26).setCellValue("12:00:00");// C补贴累计23后
row.getCell(27).setCellValue("13:00:00");// B补贴累计23前
row.getCell(28).setCellValue("14:00:00");// D补贴累计23后
row = sheetNow.getRow(2);
int rownum = 6;
long startDay = from.parse("2012-07-01").getTime();
Long endDay = from.parse("2012-08-20").getTime();
for (long j = startDay; j < endDay + (1000 * 24 * 3600); j = j
+ (1000 * 24 * 3600)) {
Date date = new Date();
date.setTime(j);
String da = from.format(date);
Integer emonth = Integer.valueOf(date.getMonth()) + 1;
Integer eday = Integer.valueOf(date.getDate());
row = sheetNow.getRow(rownum);
row.getCell(0).setCellValue(emonth);// 统计--月份
row.getCell(1).setCellValue(eday);// 统计--日期
String dayType = judgeDayType(da);
row.getCell(3).setCellValue(dayType);
row.getCell(4).setCellValue("09:00");// 打卡--出勤时间
row.getCell(5).setCellValue("18:00");// 打卡--退勤时间
row.getCell(6).setCellValue("00:00");// 实际缺勤--迟到时间
row.getCell(7).setCellValue("00:00");// 实际缺勤--早退时间
row.getCell(8).setCellValue("1:00");// 实际缺勤--自动调休时间
row.getCell(9).setCellValue("2:00");// 实际缺勤--缺勤累计
row.getCell(10).setCellValue("9:00");// 上下班--开始时间
row.getCell(11).setCellValue("18:00");// 上下班--结束时间
row.getCell(12).setCellValue("");// 请假--开始时间
row.getCell(13).setCellValue("");// 请假--结束时间
row.getCell(14).setCellValue("");// 请假--累计时间
row.getCell(15).setCellValue("");// 请假--调休
row.getCell(16).setCellValue("");// 请假--年假
row.getCell(17).setCellValue("");// 请假--事假
row.getCell(18).setCellValue("");// 请假--病假
row.getCell(19).setCellValue("");// 请假--婚、丧、产假
row.getCell(20).setCellValue("");// 出差---开始时间
row.getCell(21).setCellValue("");// 出差---结束时间
row.getCell(22).setCellValue("");// 出勤补贴---开始时间
row.getCell(23).setCellValue("");// 出勤补贴---结束时间
row.getCell(24).setCellValue("");// 工作日---起算时间
row.getCell(25).setCellValue("100:00");// 工作日---A补贴23前
row.getCell(26).setCellValue("80:00:00");// 工作日---C补贴23后
row.getCell(27).setCellValue("300:00");// 节假日---B补贴23前
row.getCell(28).setCellValue("40:00:00");// 节假日---D补贴23后
row.getCell(29).setCellValue("35");
row.getCell(30).setCellValue("35");
row.getCell(31).setCellValue("10");
rownum++;
}
sheetNow.setForceFormulaRecalculation(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制行
*
* @param workbook
* @param fromsheet
* @param newsheet
* @param firstrow
* @param lastrow
*
* copySheet("template", "test2", "d:/template.xls");
*/
@SuppressWarnings("deprecation")
private static void copyRows(HSSFWorkbook workbook, HSSFSheet fromsheet,
HSSFSheet newsheet, int firstrow, int lastrow) {
if ((firstrow == -1) || (lastrow == -1) || lastrow < firstrow) {
return;
}
// 拷贝合并的单元格
Region region = null;
for (int i = 0; i < fromsheet.getNumMergedRegions(); i++) {
region = fromsheet.getMergedRegionAt(i);
if ((region.getRowFrom() >= firstrow)
&& (region.getRowTo() <= lastrow)) {
newsheet.addMergedRegion(region);
}
}
HSSFRow fromRow = null;
HSSFRow newRow = null;
HSSFCell newCell = null;
HSSFCell fromCell = null;
// 设置列宽
for (int i = firstrow; i <= lastrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow != null) {
for (int j = fromRow.getLastCellNum(); j >= fromRow
.getFirstCellNum(); j--) {
int colnum = fromsheet.getColumnWidth((short) j);
if (colnum > 100) {
newsheet.setColumnWidth((short) j, (short) colnum);
}
if (colnum == 0) {
newsheet.setColumnHidden((short) j, true);
} else {
newsheet.setColumnHidden((short) j, false);
}
}
break;
}
}
// 拷贝行并填充数据
for (int i = 0; i <= lastrow; i++) {
fromRow = fromsheet.getRow(i);
if (fromRow == null) {
continue;
}
newRow = newsheet.createRow(i - firstrow);
newRow.setHeight(fromRow.getHeight());
for (int j = fromRow.getFirstCellNum(); j < fromRow
.getPhysicalNumberOfCells(); j++) {
fromCell = fromRow.getCell((short) j);
if (fromCell == null) {
continue;
}
newCell = newRow.createCell((short) j);
newCell.setCellStyle(fromCell.getCellStyle());
int cType = fromCell.getCellType();
newCell.setCellType(cType);
switch (cType) {
case HSSFCell.CELL_TYPE_STRING:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
newCell.setCellValue(fromCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
newCell.setCellFormula(fromCell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(fromCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
newCell.setCellValue(fromCell.getErrorCellValue());
break;
default:
newCell.setCellValue(fromCell.getRichStringCellValue());
break;
}
}
}
}
/**
* 日期类型
*
* @param date
* @param hdsettings
* @return
*/
public static String judgeDayType(String date) {
String dayType = "";
try {
Date tt = from.parse(date);
String week = weekFrom.format(tt); // /from.parse(date)
if (week.equals("星期一") || week.equals("星期二") || week.equals("星期三")
|| week.equals("星期四") || week.equals("星期五")) {
dayType = "工作";
} else {
dayType = "周末";
}
} catch (Exception e) {
e.printStackTrace();
}
return dayType;
}
// 32列 55行
public static void main(String[] args) {
ttt();
//System.out.println(judgeDayType("2012-07-12"));
}
}