-
package com.unite.parentcompany;
-
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; -
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.poifs.filesystem.POIFSFileSystem; -
import com.bean.unite.parentcompany.UniteHhrsEmpWagReportForm;
-
/**
*
* 此类用于汇总“【淮海公司劳动人事表2份单位展开(淮海公司职工人数及工资报表)】” 在册总人数填列
*
* @author xiexw
*
*/
public class UniteHhrsEmpWagReport {
private FileInputStream fileFormat = null; // 报表格式文件
private FileInputStream fileIn = null; // 数据源文件
private FileOutputStream fileOut = null; // 所生成的报表文件
private HSSFWorkbook wb = null; // 创建一个workbook实例
private HSSFWorkbook wb_unite = null; // 格式文件的一个实例
private HSSFSheet sheet = null; // 创建一个sheet实例
private HSSFRow row = null; // 创建一个row的实例
private HSSFCell col = null; // 创建一个column的实例
private String column_name[] = { "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH",
"AI" }; // [B - AI]列数组,用于报表生成时列数组 -
private POIFSFileSystem fs = null; // 创建一个POIFSFileSystem实例
-
public void CreateUniteHhrsReport(List<UniteHhrsEmpWagReportForm> list_xc,
List<UniteHhrsEmpWagReportForm> list_hh, String modelFilePath,
String causeFilePath, int row_index) {
// 初始化带有格式的报表文件路径
this.initPOIFSFileSystem(modelFilePath);
// 进行星辰汇总
this.UniteXcReport(this.readCellFactory(list_xc, row_index));
// 进行淮海汇总
this.UniteHhReport(this.readCellFactory(list_hh, row_index));
// 总公司报表生成
try {
fileOut = new FileOutputStream(causeFilePath);
this.wb_unite.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} -
/**
* 初始化带有格式的报表文件
*/
public void initPOIFSFileSystem(String filepath) {
try {
fileFormat = new FileInputStream(filepath);
fs = new POIFSFileSystem(fileFormat);
wb_unite = new HSSFWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} -
/*
*
* 星辰汇总在册汇总,写入
*
*/
public void UniteXcReport(List<List<String>> list) {
HSSFSheet sheet_xc = wb_unite.getSheet("星辰公司");
/*
* 按公司名称对应[源]-【公司名称】:【目标】-公司名称写入【星辰公司工作薄中】
* row:【在册总人数】-【物业公司】第9行-第35行,index:8-34 column:【A-AI】第1列-第35列,index
* 0-34
*/
HSSFRow row = null;
HSSFCell col = null;
/**
* 设置第9行的[A - AI]列数据 row-index : 8
*/
row = sheet_xc.getRow(8);
for (int c = 0; c <= 34; c++) {
col = row.getCell(c); -
if (null == col) {
col = row.getCell(c);
} -
if (c == 0) {
col.setCellType(HSSFCell.CELL_TYPE_STRING);
col.setCellValue("总人数");
} -
else {
col.setCellType(HSSFCell.CELL_TYPE_FORMULA);
col.setCellFormula("SUM(" + column_name[c - 1] + "10:"
+ column_name[c - 1] + "35)");// 第10行 - 第35行之和 -
}
} -
/**
* 设置第[10 - 35]行的[A - AI]列数据 row-index: 9 - 34
*
*/
for (int r = 9, index = 0; r <= 34 && index < list.size(); r++, index++) {
row = sheet_xc.getRow(r);
if (null == row) {
row = sheet_xc.createRow(r);
}
List<String> temp = list.get(index); -
//System.out.println("temp:" + temp);
for (int c = 0; c <= 34; c++) {// c为每个单元格,即每列
// 如果是[合计]行,那么要设置公式
col = row.getCell(c);
if (null == col) {
col = row.createCell(c);
} else {
// 如果是[第1列],那么他的值为[基层单位名称]
if (c == 0) {
col.setCellType(HSSFCell.CELL_TYPE_STRING);
col.setCellValue(temp.get(c)); // 基层单位名称 -
} else if (c == 1) {
-
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(c))); // 单元格数据(特殊列第二列)
// System.out.print("特殊列:"+col + " ");
}
/**
* 当c==2是女职工单元格(第三列),则是取基层报表的全民女职工与集体女职工之和
*/
else if (c == 2) {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(k))
+ Double.valueOf(temp.get(k + 1)));
// System.out.print("女职工:"+col + " "); -
}
else if (3 <= c && c <= 8) {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(k + 1))); // 单元格数据
// System.out.print("一般列"+col+ " "); -
}
else if ((9 <= c && c <= 15) || (27 <= c && c <= 34)) {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue((Double.valueOf(temp.get(k + 1))) / 26); // 平均值
// System.out.print("平均值:"+col + " ");
} else {//
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(k + 1))); // 单元格数据
// System.out.print("星辰一般列:"+col + " ");
} -
}
}
} -
}
-
/**
* 淮海汇总,写入
*/
public void UniteHhReport(List<List<String>> list) { -
HSSFSheet sheet_hh = wb_unite.getSheet("淮海公司");
/**
* 按公司名称对号入座[源]-公司名称:[目标]-公司名称; 把数据写入[淮海公司]工作薄中 row:[在册总人数-
* 星辰公司(星辰公司汇总[合计]行)]第9行-第21行,index = 8 - 20; [在册总人数]行,index =
* 8,设置计算公式SUM(); column:[A - AA]第1列-第35列,index = 0 - 34;
*/
HSSFRow row = null;
HSSFCell col = null;
/**
* 设置第9行的[A - AA]列数据 row-index : 8
*/
row = sheet_hh.getRow(8);
for (int c = 0; c <= 34; c++) {
col = row.getCell(c);
if (null == col) {
col = row.getCell(c);
}
if (c == 0) {
col.setCellType(HSSFCell.CELL_TYPE_STRING);
col.setCellValue("在册总人数");
} else {
col.setCellType(HSSFCell.CELL_TYPE_FORMULA);
col.setCellFormula("SUM(" + column_name[c - 1] + "10:"
+ column_name[c - 1] + "21)"); -
}
}
/**
* 设置第[10 - 21]行的[A - AA]列数据 row-index: 9 - 20
*/
for (int r = 9, index = 0; r <= 20 && index < list.size(); r++, index++) {
row = sheet_hh.getRow(r);
if (null == row) {
row = sheet_hh.createRow(r);
}
List<String> temp = list.get(index);
for (int c = 0; c <= 34; c++) {
// 如果是[合计]行,那么要设置公式
col = row.getCell(c);
if (null == col) {
col = row.createCell(c);
}
// 如果是[第1列],那么他的值为[基层单位名称]
if (c == 0) {
col.setCellType(HSSFCell.CELL_TYPE_STRING);
col.setCellValue(temp.get(c)); // 基层单位名称 -
} else {
-
if (c == 1) {
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(c))); // 单元格数据(特殊列第二列“合计列”)
// System.out.print("淮海特殊列:"+col + " ");
} else if (c == 2) {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(k))
+ Double.valueOf(temp.get(k + 1)));
// System.out.print("淮海女职工:"+col + " ");
}
else if (3 <= c && c <= 8) {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(k + 1))); // 单元格数据
// System.out.print("一般列:"+col + " ");
}
else if ((9 <= c && c <= 15) || (27 <= c && c <= 34)) {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col
.setCellValue((Double.valueOf(temp.get(k + 1))) / 37); // 平均值
// System.out.print("淮海平均值:"+col + " ");
} else {
int k = c;
col.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
col.setCellValue(Double.valueOf(temp.get(k + 1))); // 单元格数据
// System.out.print("淮海一般列:"+col + " ");
}
}
}
}
/**
* 设置第21行[A - AI]列的数据 row-index: 20
*/
row = sheet_hh.getRow(20);
for (short c = 0; c <= 34; c++) {
col = row.getCell(c);
if (null == col) {
col = row.createCell(c);
}
if (c == 0) {
col.setCellType(HSSFCell.CELL_TYPE_STRING);
col.setCellValue("星辰公司汇总");
} else {
col.setCellType(HSSFCell.CELL_TYPE_FORMULA);
col.setCellFormula("SUM('星辰公司'!" + column_name[c - 1] + "9)"); -
}
} -
}
-
/*
* 读取【淮海公司劳动人事表2(即淮海公司职工数及工资表)】 [在册人数]行及相关数据,并封装
*
*/
public List<List<String>> readCellFactory(
List<UniteHhrsEmpWagReportForm> bean, int row_index) {
List<List<String>> list = new ArrayList<List<String>>();
try {
Iterator<UniteHhrsEmpWagReportForm> it = bean.iterator();// 迭代器
while (it.hasNext()) {
UniteHhrsEmpWagReportForm temp = (UniteHhrsEmpWagReportForm) it
.next();
fileIn = new FileInputStream(temp.getHupload_path());// 得到数据源文件
wb = new HSSFWorkbook(fileIn);
/*
* 读取基层单位【淮海劳动人事表2】 SheetName:[淮海劳表2]
*
*
*/
sheet = wb.getSheet("淮海劳表2");
row = sheet.getRow(row_index);// 第九行“在册总人数” -
List<String> col_value = new ArrayList<String>();
-
col_value.add(temp.getReport_name()); // 填加[基层单位名称]
for (int c = 1; c <= 35; c++) {
col = row.getCell(c);
col_value.add(String.valueOf(col.getNumericCellValue())); // 填加[单元格数据]
} -
list.add(col_value); // [基层单位名称和单元格数据]做一个整体存放在list中
} -
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
通过对POI组件,实现对Excel表格合并的读写操作
最新推荐文章于 2023-08-01 10:05:23 发布