Commit 1470bc27 by yangyang

时空车轨导出提交

parent fe059a03
package com.founder.commonutils.util;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 导出Excel工具类 admin 2018/10/11
* title 表名
* headersName 使用数组封装excel列名
* headersId 使用数组封装对应列名需要展示的列的属性名 (二者需要一一对应,顺序不能打乱)
* dtoList 对象结果集
* */
public class ExportExcelUtil<T> {
@SuppressWarnings("deprecation")
public void exportExcel(String title, String[] headersName,
String[] headersId, List<T> dtoList, HttpServletResponse response, String name)
throws Exception {
Map<Integer, String> headersNameMap = new HashMap<>();
int key = 0;
for (int i = 0; i < headersName.length; i++) {
if (!headersName[i].equals(null)) {
headersNameMap.put(key, headersName[i]);
key++;
}
}
Map<Integer, String> titleFieldMap = new HashMap<>();
int value = 0;
for (int i = 0; i < headersId.length; i++) {
if (!headersId[i].equals(null)) {
titleFieldMap.put(value, headersId[i]);
value++;
}
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(title);
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = wb.createCellStyle();
HSSFRow row = sheet.createRow(0);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell;
Collection<String> c = headersNameMap.values();// 拿到表格所有标题的value的集合
Iterator<String> it = c.iterator();// 表格标题的迭代器
// 根据选择的字段生成表头
short size = 0;
while (it.hasNext()) {
cell = row.createCell(size);
cell.setCellValue(it.next().toString());
cell.setCellStyle(style);
size++;
}
Collection<String> zdC = titleFieldMap.values();
Iterator<T> labIt = new ArrayList<T>().iterator();
if (dtoList != null) {
labIt = dtoList.iterator();
}
int zdRow = 0;
while (labIt.hasNext()) {// 记录的迭代器,遍历总记录
int zdCell = 0;
zdRow++;
row = sheet.createRow(zdRow);
T l = (T) labIt.next();
List<Field> fields = new ArrayList<>();
Class<?> tempClass = l.getClass();
while (tempClass != null) {
fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
tempClass = tempClass.getSuperclass();
}
Iterator<String> zdIt = zdC.iterator();
while (zdIt.hasNext()) {// 遍历要导出的字段集合
String next = zdIt.next();
for (short i = 0; i < fields.size(); i++) {// 遍历属性,比对
Field field = fields.get(i);
String fieldName = field.getName();// 属性名
if (next.equals(fieldName)) {
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);// 拿到属性的get方法
try {
Class<?> cls = l.getClass();
Method getMethod = cls.getMethod(getMethodName,
new Class[] {});
Object val = getMethod.invoke(l, new Object[] {});
String textVal = null;
if (val != null) {
// 如果是double类型则只保留两位小说
if (field.getGenericType().toString()
.equals("double")
|| field.getGenericType().toString()
.equals("Double")) {
DecimalFormat df = new DecimalFormat(
"#0.00");
String format = df.format(val);
textVal = String.valueOf(format);
} else {
textVal = String.valueOf(val);// 转化成String
}
} else {
textVal = null;
}
row.createCell((short) zdCell)
.setCellValue(textVal);
zdCell++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
OutputStream out = response.getOutputStream();
try {
response.setContentType("application/x-download");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;fileName="
+ new String((name).getBytes("gbk"), "iso8859-1")
+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())
+ ".xls");
wb.write(out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
...@@ -9,15 +9,12 @@ import com.founder.commonutils.model.newPublicEntity.MapRestResult; ...@@ -9,15 +9,12 @@ import com.founder.commonutils.model.newPublicEntity.MapRestResult;
import com.founder.commonutils.model.vo.param.SkTrailParam; import com.founder.commonutils.model.vo.param.SkTrailParam;
import com.founder.commonutils.model.vo.param.SkTrailSaveParam; import com.founder.commonutils.model.vo.param.SkTrailSaveParam;
import com.founder.commonutils.model.vo.response.SkTrailVO; import com.founder.commonutils.model.vo.response.SkTrailVO;
import com.founder.commonutils.util.Base64Util; import com.founder.commonutils.util.*;
import com.founder.commonutils.util.DateUtil;
import com.founder.commonutils.util.KeyUtil;
import com.founder.commonutils.model.newPublicEntity.SkTrail; import com.founder.commonutils.model.newPublicEntity.SkTrail;
import com.founder.servicebase.controller.ExcelControllerInterface; import com.founder.servicebase.controller.ExcelControllerInterface;
import com.founder.servicebase.logs.OperLog; import com.founder.servicebase.logs.OperLog;
import com.founder.servicebase.logs.OperationType; import com.founder.servicebase.logs.OperationType;
import com.founder.publicapi.service.SkTrailService; import com.founder.publicapi.service.SkTrailService;
import com.founder.commonutils.util.SpringUtil;
import com.founder.servicebase.service.SkDictionaryService; import com.founder.servicebase.service.SkDictionaryService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
...@@ -25,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -25,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -234,6 +232,39 @@ public class SkTrailController extends ApiController implements ExcelControllerI ...@@ -234,6 +232,39 @@ public class SkTrailController extends ApiController implements ExcelControllerI
} }
@PostMapping("/export")
@ApiOperation("Excel接口")
//@OperLog(message = "Excel接口(轨迹导出)", operation = OperationType.QUERY)
public void export(HttpServletResponse response,@RequestBody SkTrailParam sktrailParam)throws Exception{
//分页
Page page = new Page(sktrailParam.getPage(), sktrailParam.getPageSize());
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("isDeleted", "0");
if (sktrailParam.getLikeField() != null && !sktrailParam.getLikeField().equals("")) {
queryWrapper.eq("objectType", sktrailParam.getLikeField());
}
if (sktrailParam.getLikeValue() != null && !sktrailParam.getLikeValue().equals("")) {
queryWrapper.like("objectValue", sktrailParam.getLikeValue());
}
//时间
if (sktrailParam.getKssj() != null && !sktrailParam.getKssj().equals("")) {
queryWrapper.ge("str_to_date(date,'%Y-%m-%d %H:%i:%s')", sktrailParam.getKssj());
}
if (sktrailParam.getJssj() != null && !sktrailParam.getJssj().equals("")) {
queryWrapper.le("str_to_date(date,'%Y-%m-%d %H:%i:%s')", sktrailParam.getJssj());
}
queryWrapper.groupBy("objectValue");
IPage page1 = skTrailService.page(page, queryWrapper);
List<SkTrail> records = page1.getRecords();
ExportExcelUtil<SkTrail> exportExcelUtil = new ExportExcelUtil<>();
String[] headersName = {"号码","经度","纬度","时间","地址"};
String[] headersField = {"objectvalue","x","y","time","address"};
exportExcelUtil.exportExcel("DataStatistics", headersName, headersField, records, response,"轨迹信息");
}
/** /**
* 根据编号查找相关轨迹 * 根据编号查找相关轨迹
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment