导出Excel表格
This commit is contained in:
@ -24,6 +24,6 @@ public interface QcSourceService {
|
||||
/**
|
||||
* 导出Excel表格
|
||||
*/
|
||||
String exportExcel(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception;
|
||||
String exportExcel(String host, String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception;
|
||||
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.transaction.Transactional;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
@ -244,12 +245,11 @@ public class QcSourceServiceImpl implements QcSourceService {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出Excel表格
|
||||
*/
|
||||
@Override
|
||||
public String exportExcel(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception {
|
||||
public String exportExcel(String host, String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception {
|
||||
List<CraftItemEntity> craftItemList = craftItemDao.findAll();
|
||||
if (craftItemList.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale);
|
||||
@ -260,18 +260,19 @@ public class QcSourceServiceImpl implements QcSourceService {
|
||||
}
|
||||
List<Map<String, Object>> sourceList = qcSourceDao.queryQcSource(LocalStringUtils.formatEmptyValue(batchNo), LocalStringUtils.formatEmptyValue(machineNo), LocalStringUtils.formatEmptyValue(materialNo), LocalStringUtils.formatEmptyValue(mouldNo), LocalStringUtils.formatEmptyValue(varietyNo), LocalStringUtils.formatEmptyValue(craftItemNo));
|
||||
List<Map<String, Object>> resultList = generateResultList(sourceList);
|
||||
List<List<String>> formattedResultList = formatResultList(resultList, qcItemList);
|
||||
String finalHost = host.endsWith("/") ? host : host + "/";
|
||||
List<List<String>> formattedResultList = formatResultList(finalHost, resultList, qcItemList);
|
||||
if (formattedResultList.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale);
|
||||
}
|
||||
doExportExcel(formattedResultList);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "导出成功!", locale);
|
||||
String excelPath = doExportExcel(formattedResultList);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + excelPath, locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化结果集数据
|
||||
*/
|
||||
private List<List<String>> formatResultList(List<Map<String, Object>> resultList, List<QcItemEntity> qcItemList) {
|
||||
private List<List<String>> formatResultList(String host, List<Map<String, Object>> resultList, List<QcItemEntity> qcItemList) {
|
||||
List<List<String>> formatResultList = new ArrayList<>();
|
||||
if (resultList == null || resultList.isEmpty()) {
|
||||
return formatResultList;
|
||||
@ -295,7 +296,7 @@ public class QcSourceServiceImpl implements QcSourceService {
|
||||
dataList.add(value);
|
||||
} else if (Constant.QC_ITEM_TYPE_PICTURE.equals(entity.getQcItemType())) {
|
||||
if (value.startsWith("picture")) {
|
||||
dataList.add("http://118.24.27.47:2088/" + value);
|
||||
dataList.add(host + value);
|
||||
} else {
|
||||
dataList.add(value);
|
||||
}
|
||||
@ -311,7 +312,7 @@ public class QcSourceServiceImpl implements QcSourceService {
|
||||
/**
|
||||
* 将格式化后的结果集数据导出为Excel文件
|
||||
*/
|
||||
private void doExportExcel(List<List<String>> dataListList) throws Exception {
|
||||
private String doExportExcel(List<List<String>> dataListList) throws Exception {
|
||||
//创建XSSFWorkbook对象(excel的文档对象)
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
//建立新的sheet对象(excel的表单)
|
||||
@ -369,10 +370,15 @@ public class QcSourceServiceImpl implements QcSourceService {
|
||||
}
|
||||
}
|
||||
//输出Excel文件
|
||||
File dirFile = new File("/home/sxy/server/industrial_measurement/excel/");
|
||||
if (!dirFile.exists()) {
|
||||
dirFile.mkdirs();
|
||||
}
|
||||
String excelFileName = System.currentTimeMillis() + ".xlsx";
|
||||
FileOutputStream output = new FileOutputStream("/Users/liuchengqian/Desktop/DaJiang/excel/" + excelFileName);
|
||||
FileOutputStream output = new FileOutputStream(new File(dirFile, excelFileName));
|
||||
workbook.write(output);
|
||||
output.flush();
|
||||
return "excel/" + excelFileName;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user