政策征求意见导出
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package com.adc.da.workFlow.common;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import sun.awt.SunHints;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ExportExcel<T> {
|
||||
|
||||
private String header;
|
||||
|
||||
private static String[] property;
|
||||
Workbook workbook=new XSSFWorkbook();
|
||||
|
||||
public void setProperty(String property){
|
||||
this.property=property.split(",");
|
||||
}
|
||||
|
||||
public void setHeader(String header){
|
||||
this.header=header;
|
||||
}
|
||||
|
||||
public Workbook getWorkBook(T dataJson) throws Exception {
|
||||
JSONArray dataArray = JSONObject.parseArray(dataJson.toString());
|
||||
|
||||
CellStyle cellStyle = workbook.createCellStyle();//初始化单元格格式对象
|
||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
|
||||
|
||||
Sheet sheet = workbook.createSheet();
|
||||
sheet.setDefaultColumnWidth(25);
|
||||
|
||||
createHeader(sheet,header);
|
||||
|
||||
createData(sheet,dataArray);
|
||||
|
||||
return workbook;
|
||||
}
|
||||
|
||||
|
||||
public static void createHeader(Sheet sheet, String header){
|
||||
|
||||
Row rowHeader = sheet.createRow(0);//开始创建标题行
|
||||
if (StringUtils.isNotBlank(header)) {
|
||||
String[] headerArr = header.split(",");
|
||||
for (int i=0;i < headerArr.length; i++) {
|
||||
|
||||
rowHeader.createCell(i).setCellValue(headerArr[i]);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void createData(Sheet sheet, List<?> data) throws Exception{
|
||||
|
||||
if (data != null && !data.isEmpty()) {
|
||||
for (int i=0;i < data.size(); i++) {
|
||||
|
||||
Row row = sheet.createRow(i+1);
|
||||
|
||||
int sheetNum = 0;
|
||||
|
||||
JSONObject datumJson =(JSONObject)data.get(i);
|
||||
/**
|
||||
* 遍历jsonObject值填入一行的单元格中
|
||||
*/
|
||||
for (String s : property) {
|
||||
String value = datumJson.getString(s);
|
||||
row.createCell(sheetNum).setCellValue(value);
|
||||
sheetNum++;
|
||||
}
|
||||
// Class cls = importDto.getClass();
|
||||
// Field[] fields = cls.getDeclaredFields();
|
||||
// for (Field field : fields) {
|
||||
// field.setAccessible(true);
|
||||
// if (field.get(importDto)!=null){
|
||||
// String value = field.get(importDto).toString();
|
||||
// row.createCell(sheetNum).setCellValue(value);
|
||||
// }else {
|
||||
// row.createCell(sheetNum).setCellValue("");
|
||||
// }
|
||||
// sheetNum++;
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.adc.da.workFlow.controller;
|
||||
|
||||
import com.adc.da.common.ReadExcel;
|
||||
import com.adc.da.workFlow.common.ExportExcel;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/${restPath}/policy/activiti")
|
||||
@Api(tags = "政策征求意见和重点法规或政策认证流程")
|
||||
public class PolicyWorkFlowController {
|
||||
|
||||
|
||||
/**
|
||||
* 政策征求意见导出
|
||||
*/
|
||||
@PostMapping("/exportExcel")
|
||||
@ApiOperation("导出政策征求意见为excel")
|
||||
public void exportExcel(@RequestBody String dataJson, HttpServletResponse response, HttpServletRequest request) throws Exception {
|
||||
JSONObject data = JSONObject.parseObject(dataJson);
|
||||
JSONArray info = data.getJSONArray("dataList");
|
||||
|
||||
|
||||
ExportExcel exportExcel = new ExportExcel();
|
||||
exportExcel.setHeader("章节编号,章节名称,修改前,修改后,修改理由,原因,提出部门,提出人");
|
||||
exportExcel.setProperty("chapterNumber,chapterName,oldText,newText,changeReason,reason,department,responUserName");
|
||||
Workbook workbook=exportExcel.getWorkBook(info);
|
||||
|
||||
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setHeader("Content-Disposition",
|
||||
"attachment; filename=" + ReadExcel.encodeFileName("政策征求意见"+".xlsx",request));
|
||||
// response.setContentType("application/force-download");
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
workbook.write(outputStream);
|
||||
outputStream.flush();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user