You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.5 KiB

package Tools.MaxKb;
import com.dsideal.QingLong.Util.ExcelCommonUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class GenerateQA_1 {
public static void main(String[] args) throws IOException {
//数据源
String source = "D:\\dsWork\\QingLong\\Doc\\MaxKB\\各学校人员和班级统计.xlsx";
//模板
String template = "D:\\dsWork\\QingLong\\Doc\\MaxKB\\excel模版.xlsx";
//结果
String target = source.replace(".xlsx", "【结果】.xlsx");
//模板读取
FileInputStream fTemplate = new FileInputStream(template);
Workbook wbTemplate = new XSSFWorkbook(fTemplate);
Sheet sheetTemplate = wbTemplate.getSheetAt(0);// 获取第一个工作表
CellStyle style = ExcelCommonUtil.getCellStyle(wbTemplate);//单元格样式
//源文件
FileInputStream fSource = new FileInputStream(source);
Workbook wbSource = new XSSFWorkbook(fSource);
Sheet sheetSource = wbSource.getSheetAt(0);// 获取第一个工作表
//1、一共有哪些区
Set<String> areaSet = new HashSet<>();
for (int i = 1; i < sheetSource.getLastRowNum(); i++) {//放过表头
Row row = sheetSource.getRow(i);
String jyj = ExcelCommonUtil.getStringValue(row.getCell(1));//教育局
areaSet.add(jyj);
}
//2、记录每个区下有多少个学校,多少个班级,多少个教师,多少个专任教师
Map<String, List<String>> areaMap = new HashMap<>();
for (int i = 1; i < sheetSource.getLastRowNum(); i++) {//放过表头
Row row = sheetSource.getRow(i);
String schoolName = ExcelCommonUtil.getStringValue(row.getCell(0));//学位名称
String jyjName = ExcelCommonUtil.getStringValue(row.getCell(1));//教育局
String classCount = ExcelCommonUtil.getStringValue(row.getCell(2));//班级数量
String studentCount = ExcelCommonUtil.getStringValue(row.getCell(3));//学生数量
String teacherCount = ExcelCommonUtil.getStringValue(row.getCell(4));//教师数量
String zhuanRenCount = ExcelCommonUtil.getStringValue(row.getCell(5));//专任教师数量
List<String> list = new ArrayList<>();
if (areaMap.containsKey(jyjName)) {
list = areaMap.get(jyjName);
}
list.add("学校名称:" + schoolName + ",所属教育局:" + jyjName + ",班级数量:" + classCount + ",学生数量:" + studentCount + ",教师数量:" + teacherCount + ",专任教师数量:" + zhuanRenCount + ";");
areaMap.put(jyjName, list);
}
//将数据写入到EXCEL
for (int i = 0; i < areaSet.size(); i++) {
String area = areaSet.toArray()[i].toString();
Row row = sheetTemplate.getRow(i + 1); // 放过表头
if (row == null) {
row = sheetTemplate.createRow(i + 1);//创建行
}
//(1)问题名称
Cell cell = row.getCell(0);
if (cell == null) {
cell = row.createCell(0);
}
cell.setCellValue(area + "下的学校列表");
cell.setCellStyle(style);
//(2)回答
cell = row.getCell(1);
if (cell == null) {
cell = row.createCell(1);
}
String content = "";
for (String s : areaMap.get(area)) {
content = content + s;
}
//学校数量
content = content + ",学校总数:" + areaMap.get(area).size() + "个。" + "\n";
cell.setCellValue(content);
cell.setCellStyle(style);
//(3)可选问题名称
cell = row.getCell(2);
if (cell == null) {
cell = row.createCell(2);
}
cell.setCellValue(area + "下的学校列表");
cell.setCellStyle(style);
}
// 保存文件
FileOutputStream fileOut = new FileOutputStream(target);
wbTemplate.write(fileOut);
//关闭文件
fSource.close();
wbSource.close();
System.out.println("恭喜,所有操作成功完成!");
}
}