<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.13.graal</version>
</dependency>
private com.alibaba.fastjson.JSONObject readerMethod(File file) throws IOException {
FileReader fileReader = new FileReader(file);
Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
String jsonStr = sb.toString();
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(jsonStr);
return jsonObject;
}
/**
* 并行异步执行HTTP请求
*/
@RequestMapping(value = "/ps", method = RequestMethod.POST)
public void parallAsyn() throws IOException {
File file = new File("D:\\0geshi\\geshi1.json");
com.alibaba.fastjson.JSONObject jsonStr = readerMethod(file);
List<表> list = JSON.parseArray(jsonStr.get("gzx").toString(), 表.class);
//1.创建excel文档对象
HSSFWorkbook workbook = new HSSFWorkbook();
//2.获取excel表单对象
HSSFSheet sheet = workbook.createSheet("表数据demo");
//3.获取标题行(第1行)对象
HSSFRow row = sheet.createRow(0);
//4.创建标题行中列(第1列)对象
HSSFCell cell = row.createCell(0);
//设置标题名称
cell.setCellValue("标题demo");
//5.合并单元格,将坐标(0,0)单元格到(5,5)单元格合并(默认行数和列数都是从0开始的)
/*
参数1:开始行号
参数2:结束行号
参数3:开始列号
参数4:终止列号
*/
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 1);
sheet.addMergedRegion(cellRangeAddress);
//6.填入列名
/*
1)获取第一行对象
2)获取各个列对象
3)设置各个列的内容
*/
boolean 单行 = true;
boolean 双行 = false;
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
for (int i = 0; i < list.size(); i++) {
HSSFRow sheetRow = sheet.createRow(i + 1);
表 b1 = list.get(i);
if (i > 0 && !b1.get表名().equals(list.get(i - 1).get表名())) {
if(单行){
单行 = false;
双行 = true;
}else if(双行){
单行 = true;
双行 = false;
}
}
HSSFCell c0 = sheetRow.createCell(0);
HSSFCell c1 = sheetRow.createCell(1);
HSSFCell c2 = sheetRow.createCell(2);
HSSFCell c3 = sheetRow.createCell(3);
HSSFCell c4 = sheetRow.createCell(4);
HSSFCell c5 = sheetRow.createCell(5);
if(双行){
c0.setCellStyle(cellStyle);
c1.setCellStyle(cellStyle);
c2.setCellStyle(cellStyle);
c3.setCellStyle(cellStyle);
c4.setCellStyle(cellStyle);
c5.setCellStyle(cellStyle);
}
c0.setCellValue(b1.get表名());
c1.setCellValue(b1.get表说明());
c2.setCellValue(b1.get字段名());
c3.setCellValue(b1.get字段说明());
c4.setCellValue(b1.get字段类型());
c5.setCellValue(b1.get约束());
}
//8.导出数据到excel
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("D:\\0geshi\\demo.xls");
workbook.write(fileOutputStream);
fileOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}