From 6f0dc85929c291a5a1a24c56deac17b201dab5b8 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Mon, 28 Mar 2022 17:59:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=A3=80=E9=AA=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/QcSourceServiceImpl.java | 53 ++++++++++++++----- .../xkrs/service/impl/QcSpecServiceImpl.java | 26 ++++----- .../java/com/xkrs/util/LocalDoubleUtils.java | 6 ++- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java index 2868739..13777f6 100644 --- a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java @@ -5,6 +5,7 @@ import com.xkrs.dao.QcItemDao; import com.xkrs.dao.QcSourceDao; import com.xkrs.dao.QcSpecDao; import com.xkrs.encapsulation.PromptMessageEnum; +import com.xkrs.model.bean.XSSFCellDataBean; import com.xkrs.model.entity.CraftItemEntity; import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcSourceEntity; @@ -13,13 +14,11 @@ import com.xkrs.model.qo.ExportSourceExcelQo; import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.service.QcSourceService; -import com.xkrs.util.Constant; -import com.xkrs.util.ExcelUploadUtil; -import com.xkrs.util.LocalDateUtils; -import com.xkrs.util.LocalStringUtils; +import com.xkrs.util.*; import org.apache.http.util.TextUtils; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; import org.springframework.context.i18n.LocaleContextHolder; @@ -28,9 +27,11 @@ 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.io.IOException; +import java.util.List; import java.util.*; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; @@ -218,10 +219,10 @@ public class QcSourceServiceImpl implements QcSourceService { double valueMax = stringToDouble(existsQcSpec.getMax()); double valueMin = stringToDouble(existsQcSpec.getMin()); double value = stringToDouble(qcValue); - if (valueMax >= 0 && value >= 0 && value >= valueMax) { + if (valueMax > -99_000D && value > -99_000D && value >= valueMax) { return qcValue + "##H"; } - if (valueMin >= 0 && value >= 0 && value <= valueMin) { + if (valueMin > -99_000D && value > -99_000D && value <= valueMin) { return qcValue + "##L"; } return qcValue; @@ -235,7 +236,7 @@ public class QcSourceServiceImpl implements QcSourceService { return Double.parseDouble(stringValue); } catch (Exception e) { e.printStackTrace(); - return -1D; + return -100_000D; } } @@ -340,6 +341,21 @@ public class QcSourceServiceImpl implements QcSourceService { XSSFWorkbook workbook = new XSSFWorkbook(); //建立新的sheet对象(excel的表单) XSSFSheet sheet = workbook.createSheet("检验数据表"); + for (int i = 0; i < dataListList.get(0).size(); i++) { + if (i <= 5) { + sheet.setColumnWidth(i, 20 * 256); + } else { + sheet.setColumnWidth(i, 14 * 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 highCellStyle = workbook.createCellStyle(); highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); @@ -378,15 +394,26 @@ public class QcSourceServiceImpl implements QcSourceService { XSSFRow row = sheet.createRow(i + startRowNum); for (int j = 0; j < dataList.size(); j++) { XSSFCell cell = row.createCell(j); - String data = dataList.get(j); - if (data.contains("##H")) { - cell.setCellValue(data.split("##")[0]); + String sourceValue = dataList.get(j); + if (i == 0) { + cell.setCellValue(sourceValue); + cell.setCellStyle(headCellStyle); + } else if (j <= 5) { + cell.setCellValue(sourceValue); + cell.setCellStyle(normalCellStyle); + } else if (sourceValue.contains("##H")) { + cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(sourceValue.split("##")[0])); cell.setCellStyle(highCellStyle); - } else if (data.contains("##L")) { - cell.setCellValue(data.split("##")[0]); + } else if (sourceValue.contains("##L")) { + cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(sourceValue.split("##")[0])); cell.setCellStyle(lowCellStyle); } else { - cell.setCellValue(data); + 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.keepThreeDecimal(cellDataBean.getDoubleValue())); + } cell.setCellStyle(normalCellStyle); } } diff --git a/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java index 7c8be9a..bd733ea 100644 --- a/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java @@ -399,21 +399,21 @@ public class QcSpecServiceImpl implements QcSpecService { XSSFRow row = sheet.createRow(i + startRowNum); for (int j = 0; j < dataList.size(); j++) { XSSFCell cell = row.createCell(j); - if (i == 0) { - cell.setCellStyle(headCellStyle); - } else { - cell.setCellStyle(normalCellStyle); - } String sourceValue = dataList.get(j); - if (j <= 2) { + if (i == 0) { 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())); + cell.setCellStyle(headCellStyle); + } else if (j <= 2) { + cell.setCellValue(sourceValue); + cell.setCellStyle(normalCellStyle); + } else { + 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.keepThreeDecimal(cellDataBean.getDoubleValue())); + } + cell.setCellStyle(normalCellStyle); } } } diff --git a/src/main/java/com/xkrs/util/LocalDoubleUtils.java b/src/main/java/com/xkrs/util/LocalDoubleUtils.java index 940f435..aabbdde 100644 --- a/src/main/java/com/xkrs/util/LocalDoubleUtils.java +++ b/src/main/java/com/xkrs/util/LocalDoubleUtils.java @@ -9,7 +9,11 @@ public class LocalDoubleUtils { private LocalDoubleUtils() { } - public static String formatEmptyValue(double value) { + public static String keepThreeDecimal(String value) { + return keepThreeDecimal(Double.parseDouble(value)); + } + + public static String keepThreeDecimal(double value) { return decimalFormat.format(value); }