diff --git a/src/main/java/com/xkrs/controller/QcSpecController.java b/src/main/java/com/xkrs/controller/QcSpecController.java index 20a5952..003a463 100644 --- a/src/main/java/com/xkrs/controller/QcSpecController.java +++ b/src/main/java/com/xkrs/controller/QcSpecController.java @@ -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); + } + } diff --git a/src/main/java/com/xkrs/model/bean/XSSFCellDataBean.java b/src/main/java/com/xkrs/model/bean/XSSFCellDataBean.java new file mode 100644 index 0000000..c73b330 --- /dev/null +++ b/src/main/java/com/xkrs/model/bean/XSSFCellDataBean.java @@ -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 + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/QcSpecService.java b/src/main/java/com/xkrs/service/QcSpecService.java index 315e39b..8c3e1c2 100644 --- a/src/main/java/com/xkrs/service/QcSpecService.java +++ b/src/main/java/com/xkrs/service/QcSpecService.java @@ -38,4 +38,10 @@ public interface QcSpecService { */ String exportSpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception; + /** + * 导出空白检验规格 + */ + String exportEmptySpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception; + + } diff --git a/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java index 672dc07..7c8be9a 100644 --- a/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java @@ -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> 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 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); + } } diff --git a/src/main/java/com/xkrs/util/LocalDoubleUtils.java b/src/main/java/com/xkrs/util/LocalDoubleUtils.java new file mode 100644 index 0000000..940f435 --- /dev/null +++ b/src/main/java/com/xkrs/util/LocalDoubleUtils.java @@ -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); + } + +}