导出空白检验规格模板

This commit is contained in:
liuchengqian 2022-03-28 16:58:17 +08:00
parent b8f3bab4c1
commit bb0299c323
5 changed files with 179 additions and 3 deletions

View File

@ -64,4 +64,12 @@ public class QcSpecController {
return qcSpecService.exportSpecExcel(exportSpecExcelQo);
}
/**
* 导出空白检验规格
*/
@PostMapping("/exportEmptySpecExcel")
public String exportEmptySpecExcel(@RequestBody ExportSpecExcelQo exportSpecExcelQo) throws Exception {
return qcSpecService.exportEmptySpecExcel(exportSpecExcelQo);
}
}

View File

@ -0,0 +1,58 @@
package com.xkrs.model.bean;
public class XSSFCellDataBean {
public static final int VALUE_TYPE_STRING = 1;
public static final int VALUE_TYPE_DOUBLE = 2;
private int valueType;
private String stringValue;
private double doubleValue;
public XSSFCellDataBean(String source) {
double tempDoubleValue = -100_000D;
try {
tempDoubleValue = Double.parseDouble(source);
} catch (Exception e) {
}
if (tempDoubleValue < -99_000D) {
valueType = VALUE_TYPE_STRING;
stringValue = source;
} else {
valueType = VALUE_TYPE_DOUBLE;
doubleValue = tempDoubleValue;
}
}
public int getValueType() {
return valueType;
}
public XSSFCellDataBean setValueType(int valueType) {
this.valueType = valueType;
return this;
}
public String getStringValue() {
return stringValue;
}
public XSSFCellDataBean setStringValue(String stringValue) {
this.stringValue = stringValue;
return this;
}
public double getDoubleValue() {
return doubleValue;
}
public XSSFCellDataBean setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
return this;
}
@Override
public String toString() {
return "XSSFCellDataBean{" + "valueType=" + valueType + ", stringValue='" + stringValue + '\'' + ", doubleValue=" + doubleValue + '}';
}
}

View File

@ -38,4 +38,10 @@ public interface QcSpecService {
*/
String exportSpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception;
/**
* 导出空白检验规格
*/
String exportEmptySpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception;
}

View File

@ -5,6 +5,7 @@ import com.xkrs.dao.QcItemDao;
import com.xkrs.dao.QcSpecDao;
import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.bean.ReadSpecHeadBean;
import com.xkrs.model.bean.XSSFCellDataBean;
import com.xkrs.model.entity.CraftItemEntity;
import com.xkrs.model.entity.QcItemEntity;
import com.xkrs.model.entity.QcSpecEntity;
@ -14,12 +15,14 @@ import com.xkrs.model.qo.QcSpecQoInsert;
import com.xkrs.model.qo.QcSpecQoUpdate;
import com.xkrs.service.QcSpecService;
import com.xkrs.util.LocalDateUtils;
import com.xkrs.util.LocalDoubleUtils;
import com.xkrs.util.LocalStringUtils;
import org.apache.http.util.TextUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.context.i18n.LocaleContextHolder;
@ -29,8 +32,10 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.*;
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
@ -367,6 +372,18 @@ public class QcSpecServiceImpl implements QcSpecService {
XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单
XSSFSheet sheet = workbook.createSheet("检验规格表");
for (int i = 0; i < dataListList.get(0).size(); i++) {
sheet.setColumnWidth(i, 24 * 256);
}
XSSFCellStyle headCellStyle = workbook.createCellStyle();
headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
XSSFFont headFont = workbook.createFont();
headFont.setColor(HSSFColor.BLACK.index);
headCellStyle.setFont(headFont);
headCellStyle.setFillForegroundColor(new XSSFColor(new Color(156, 195, 230)));//设置背景色
headCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充模式
XSSFCellStyle normalCellStyle = workbook.createCellStyle();
normalCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
@ -382,9 +399,22 @@ public class QcSpecServiceImpl implements QcSpecService {
XSSFRow row = sheet.createRow(i + startRowNum);
for (int j = 0; j < dataList.size(); j++) {
XSSFCell cell = row.createCell(j);
String data = dataList.get(j);
cell.setCellValue(data);
cell.setCellStyle(normalCellStyle);
if (i == 0) {
cell.setCellStyle(headCellStyle);
} else {
cell.setCellStyle(normalCellStyle);
}
String sourceValue = dataList.get(j);
if (j <= 2) {
cell.setCellValue(sourceValue);
continue;
}
XSSFCellDataBean cellDataBean = new XSSFCellDataBean(sourceValue);
if (XSSFCellDataBean.VALUE_TYPE_STRING == cellDataBean.getValueType()) {
cell.setCellValue(cellDataBean.getStringValue());
} else if (XSSFCellDataBean.VALUE_TYPE_DOUBLE == cellDataBean.getValueType()) {
cell.setCellValue(LocalDoubleUtils.formatEmptyValue(cellDataBean.getDoubleValue()));
}
}
}
}
@ -403,4 +433,62 @@ public class QcSpecServiceImpl implements QcSpecService {
String finalHost = host.endsWith("/") ? host : host + "/";
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + "excel/" + excelFileName, locale);
}
private boolean formatData(String data) {
return false;
}
/**
* 导出空白检验规格
*/
@Override
public String exportEmptySpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception {
List<List<String>> dataListList = new ArrayList<>();
dataListList.add(QC_SPEC_HEAD);
XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单
XSSFSheet sheet = workbook.createSheet("检验规格表");
for (int i = 0; i < dataListList.get(0).size(); i++) {
sheet.setColumnWidth(i, 24 * 256);
}
XSSFCellStyle headCellStyle = workbook.createCellStyle();
headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
XSSFFont headFont = workbook.createFont();
headFont.setColor(HSSFColor.BLACK.index);
headCellStyle.setFont(headFont);
headCellStyle.setFillForegroundColor(new XSSFColor(new Color(156, 195, 230)));//设置背景色
headCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充模式
int startRowNum = 0;
List<String> dataList = dataListList.get(0);
if (dataList != null && dataList.size() > 0) {
XSSFRow row = sheet.createRow(startRowNum);
for (int j = 0; j < dataList.size(); j++) {
XSSFCell cell = row.createCell(j);
cell.setCellStyle(headCellStyle);
String data = dataList.get(j);
cell.setCellValue(data);
}
}
//输出Excel文件
File dirFile = new File("/home/sxy/server/industrial_measurement/excel/");
// File dirFile = new File("/Users/liuchengqian/Desktop/DaJiang/");
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String excelFileName = System.currentTimeMillis() + ".xlsx";
FileOutputStream output = new FileOutputStream(new File(dirFile, excelFileName));
workbook.write(output);
output.flush();
String host = exportSpecExcelQo.getHost();
String finalHost = host.endsWith("/") ? host : host + "/";
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + "excel/" + excelFileName, locale);
}
}

View File

@ -0,0 +1,16 @@
package com.xkrs.util;
import java.text.DecimalFormat;
public class LocalDoubleUtils {
private static final DecimalFormat decimalFormat = new DecimalFormat("0.000");
private LocalDoubleUtils() {
}
public static String formatEmptyValue(double value) {
return decimalFormat.format(value);
}
}