|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import com.jfinal.kit.Kv;
|
|
|
|
|
import com.dsideal.QingLong.Util.ExcelCommonUtil;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
@ -11,120 +11,48 @@ import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class TestMaxKB {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定单元格的值(字符串)
|
|
|
|
|
* 作者:黄海
|
|
|
|
|
* 时间:2019-01-01
|
|
|
|
|
*
|
|
|
|
|
* @param cell
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getStringValue(Cell cell) {
|
|
|
|
|
String value = "";
|
|
|
|
|
if (cell == null || cell.equals(null) || cell.getCellType() == CellType.BLANK) {
|
|
|
|
|
value = "";
|
|
|
|
|
} else {
|
|
|
|
|
//判断数据类型
|
|
|
|
|
switch (cell.getCellType()) {
|
|
|
|
|
case FORMULA:
|
|
|
|
|
try {
|
|
|
|
|
/*
|
|
|
|
|
* 此处判断使用公式生成的字符串有问题,因为HSSFDateUtil.isCellDateFormatted(cell)判断过程中cell
|
|
|
|
|
* .getNumericCellValue();方法会抛出java.lang.NumberFormatException异常
|
|
|
|
|
*/
|
|
|
|
|
if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
|
Date businessDateTime = cell.getDateCellValue();
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
|
cal.setTime(businessDateTime);
|
|
|
|
|
int year = cal.get(Calendar.YEAR);
|
|
|
|
|
int month = cal.get(Calendar.MONTH) + 1;
|
|
|
|
|
int date = cal.get(Calendar.DATE);
|
|
|
|
|
value = year + "-" + month + "-" + date;
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
value = String.valueOf(cell.getNumericCellValue());
|
|
|
|
|
}
|
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
|
value = String.valueOf(cell.getRichStringCellValue());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case NUMERIC:
|
|
|
|
|
value = "" + cell.getNumericCellValue();
|
|
|
|
|
break;
|
|
|
|
|
case STRING:
|
|
|
|
|
value = cell.getStringCellValue();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//先设置为字符串格式
|
|
|
|
|
//cell.setCellType(CellType.STRING);
|
|
|
|
|
//然后读取之
|
|
|
|
|
//value=cell.getStringCellValue().trim();
|
|
|
|
|
if (value.endsWith(".0")) {
|
|
|
|
|
value = value.substring(0, value.length() - 2);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
//数据源
|
|
|
|
|
String excelPath = "D:\\dsWork\\QingLong\\Doc\\MaxKB\\各学校人员和班级统计.xlsx";
|
|
|
|
|
//写入模板
|
|
|
|
|
String templatePath = "D:\\dsWork\\QingLong\\Doc\\MaxKB\\excel模版.xlsx";
|
|
|
|
|
|
|
|
|
|
//输出结果
|
|
|
|
|
String outputPath = "D:\\dsWork\\QingLong\\Doc\\MaxKB\\结果.xlsx";
|
|
|
|
|
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(templatePath);
|
|
|
|
|
Workbook workbookTemplate = new XSSFWorkbook(fTemplate);
|
|
|
|
|
// 获取第一个工作表
|
|
|
|
|
Sheet sheetTemplate = workbookTemplate.getSheetAt(0);
|
|
|
|
|
// 创建单元格样式(可选)
|
|
|
|
|
CellStyle style = workbookTemplate.createCellStyle();
|
|
|
|
|
// 设置字体
|
|
|
|
|
Font font = workbookTemplate.createFont();
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
font.setFontHeightInPoints((short) 11);
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
// 设置对齐方式
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//使用POI读取EXCEL文件的内容
|
|
|
|
|
FileInputStream fSource = new FileInputStream(excelPath);
|
|
|
|
|
Workbook workbook = new XSSFWorkbook(fSource);
|
|
|
|
|
// 获取第一个工作表
|
|
|
|
|
Sheet sheetSource = workbook.getSheetAt(0);
|
|
|
|
|
FileInputStream finTemplate = new FileInputStream(template);
|
|
|
|
|
Workbook wbTemplate = new XSSFWorkbook(finTemplate);
|
|
|
|
|
Sheet sheetTemplate = wbTemplate.getSheetAt(0);// 获取第一个工作表
|
|
|
|
|
CellStyle cellStyle = 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 (Row row : sheetSource) {
|
|
|
|
|
String jyjName = getStringValue(row.getCell(1));//教育局
|
|
|
|
|
areaSet.add(jyjName);
|
|
|
|
|
String jyj = ExcelCommonUtil.getStringValue(row.getCell(1));//教育局
|
|
|
|
|
areaSet.add(jyj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//2、记录每个区下有多少个学校,多少个班级,多少个教师,多少个专任教师
|
|
|
|
|
Map<String, List<String>> schoolMap = new HashMap<>();
|
|
|
|
|
Map<String, List<String>> areaMap = new HashMap<>();
|
|
|
|
|
for (Row row : sheetSource) {
|
|
|
|
|
String schoolName = getStringValue(row.getCell(0));//学位名称
|
|
|
|
|
String jyjName = getStringValue(row.getCell(1));//教育局
|
|
|
|
|
String classCount = getStringValue(row.getCell(2));//班级数量
|
|
|
|
|
String studentCount = getStringValue(row.getCell(3));//学生数量
|
|
|
|
|
String teacherCount = getStringValue(row.getCell(4));//教师数量
|
|
|
|
|
String zhuanRenCount = getStringValue(row.getCell(5));//专任教师数量
|
|
|
|
|
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 (schoolMap.containsKey(jyjName)) {
|
|
|
|
|
list = schoolMap.get(jyjName);
|
|
|
|
|
if (areaMap.containsKey(jyjName)) {
|
|
|
|
|
list = areaMap.get(jyjName);
|
|
|
|
|
}
|
|
|
|
|
list.add("学校名称:" + schoolName + ",所属教育局:" + jyjName + ",班级数量:" + classCount + ",学生数量:" + studentCount + ",教师数量:" + teacherCount + ",专任教师数量:" + zhuanRenCount + ";");
|
|
|
|
|
schoolMap.put(jyjName, list);
|
|
|
|
|
areaMap.put(jyjName, list);
|
|
|
|
|
}
|
|
|
|
|
//将数据写入到EXCEL
|
|
|
|
|
for (int i = 0; i < areaSet.size(); i++) {
|
|
|
|
@ -139,7 +67,7 @@ public class TestMaxKB {
|
|
|
|
|
cell = row.createCell(0);
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(area + "下的学校列表");
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
|
|
|
|
//(2)回答
|
|
|
|
|
cell = row.getCell(1);
|
|
|
|
@ -148,11 +76,11 @@ public class TestMaxKB {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String content = "";
|
|
|
|
|
for (String s : schoolMap.get(area)) {
|
|
|
|
|
for (String s : areaMap.get(area)) {
|
|
|
|
|
content = content + s + "\n";
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(content);
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
|
|
|
|
//(3)可选问题名称
|
|
|
|
|
cell = row.getCell(2);
|
|
|
|
@ -160,14 +88,16 @@ public class TestMaxKB {
|
|
|
|
|
cell = row.createCell(2);
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(area + "下的学校列表");
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
}
|
|
|
|
|
// 保存文件
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream(outputPath);
|
|
|
|
|
workbookTemplate.write(fileOut);
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream(target);
|
|
|
|
|
wbTemplate.write(fileOut);
|
|
|
|
|
|
|
|
|
|
//关闭文件
|
|
|
|
|
fSource.close();
|
|
|
|
|
workbook.close();
|
|
|
|
|
wbSource.close();
|
|
|
|
|
|
|
|
|
|
System.out.println("写入成功");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|