导出检验数据格式化

This commit is contained in:
liuchengqian 2022-03-28 17:59:39 +08:00
parent bb0299c323
commit 6f0dc85929
3 changed files with 58 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import com.xkrs.dao.QcItemDao;
import com.xkrs.dao.QcSourceDao; import com.xkrs.dao.QcSourceDao;
import com.xkrs.dao.QcSpecDao; import com.xkrs.dao.QcSpecDao;
import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.encapsulation.PromptMessageEnum;
import com.xkrs.model.bean.XSSFCellDataBean;
import com.xkrs.model.entity.CraftItemEntity; import com.xkrs.model.entity.CraftItemEntity;
import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcItemEntity;
import com.xkrs.model.entity.QcSourceEntity; 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.QcSourceQoDelete;
import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.model.qo.QcSourceQoInsert;
import com.xkrs.service.QcSourceService; import com.xkrs.service.QcSourceService;
import com.xkrs.util.Constant; import com.xkrs.util.*;
import com.xkrs.util.ExcelUploadUtil;
import com.xkrs.util.LocalDateUtils;
import com.xkrs.util.LocalStringUtils;
import org.apache.http.util.TextUtils; import org.apache.http.util.TextUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor; 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.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.xssf.usermodel.*;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
@ -28,9 +27,11 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.awt.*;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.*; import java.util.*;
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
@ -218,10 +219,10 @@ public class QcSourceServiceImpl implements QcSourceService {
double valueMax = stringToDouble(existsQcSpec.getMax()); double valueMax = stringToDouble(existsQcSpec.getMax());
double valueMin = stringToDouble(existsQcSpec.getMin()); double valueMin = stringToDouble(existsQcSpec.getMin());
double value = stringToDouble(qcValue); double value = stringToDouble(qcValue);
if (valueMax >= 0 && value >= 0 && value >= valueMax) { if (valueMax > -99_000D && value > -99_000D && value >= valueMax) {
return qcValue + "##H"; return qcValue + "##H";
} }
if (valueMin >= 0 && value >= 0 && value <= valueMin) { if (valueMin > -99_000D && value > -99_000D && value <= valueMin) {
return qcValue + "##L"; return qcValue + "##L";
} }
return qcValue; return qcValue;
@ -235,7 +236,7 @@ public class QcSourceServiceImpl implements QcSourceService {
return Double.parseDouble(stringValue); return Double.parseDouble(stringValue);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return -1D; return -100_000D;
} }
} }
@ -340,6 +341,21 @@ public class QcSourceServiceImpl implements QcSourceService {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单 //建立新的sheet对象excel的表单
XSSFSheet sheet = workbook.createSheet("检验数据表"); 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(); XSSFCellStyle highCellStyle = workbook.createCellStyle();
highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
@ -378,15 +394,26 @@ public class QcSourceServiceImpl implements QcSourceService {
XSSFRow row = sheet.createRow(i + startRowNum); XSSFRow row = sheet.createRow(i + startRowNum);
for (int j = 0; j < dataList.size(); j++) { for (int j = 0; j < dataList.size(); j++) {
XSSFCell cell = row.createCell(j); XSSFCell cell = row.createCell(j);
String data = dataList.get(j); String sourceValue = dataList.get(j);
if (data.contains("##H")) { if (i == 0) {
cell.setCellValue(data.split("##")[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); cell.setCellStyle(highCellStyle);
} else if (data.contains("##L")) { } else if (sourceValue.contains("##L")) {
cell.setCellValue(data.split("##")[0]); cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(sourceValue.split("##")[0]));
cell.setCellStyle(lowCellStyle); cell.setCellStyle(lowCellStyle);
} else { } 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); cell.setCellStyle(normalCellStyle);
} }
} }

View File

@ -399,21 +399,21 @@ public class QcSpecServiceImpl implements QcSpecService {
XSSFRow row = sheet.createRow(i + startRowNum); XSSFRow row = sheet.createRow(i + startRowNum);
for (int j = 0; j < dataList.size(); j++) { for (int j = 0; j < dataList.size(); j++) {
XSSFCell cell = row.createCell(j); XSSFCell cell = row.createCell(j);
if (i == 0) {
cell.setCellStyle(headCellStyle);
} else {
cell.setCellStyle(normalCellStyle);
}
String sourceValue = dataList.get(j); String sourceValue = dataList.get(j);
if (j <= 2) { if (i == 0) {
cell.setCellValue(sourceValue); cell.setCellValue(sourceValue);
continue; cell.setCellStyle(headCellStyle);
} } else if (j <= 2) {
cell.setCellValue(sourceValue);
cell.setCellStyle(normalCellStyle);
} else {
XSSFCellDataBean cellDataBean = new XSSFCellDataBean(sourceValue); XSSFCellDataBean cellDataBean = new XSSFCellDataBean(sourceValue);
if (XSSFCellDataBean.VALUE_TYPE_STRING == cellDataBean.getValueType()) { if (XSSFCellDataBean.VALUE_TYPE_STRING == cellDataBean.getValueType()) {
cell.setCellValue(cellDataBean.getStringValue()); cell.setCellValue(cellDataBean.getStringValue());
} else if (XSSFCellDataBean.VALUE_TYPE_DOUBLE == cellDataBean.getValueType()) { } else if (XSSFCellDataBean.VALUE_TYPE_DOUBLE == cellDataBean.getValueType()) {
cell.setCellValue(LocalDoubleUtils.formatEmptyValue(cellDataBean.getDoubleValue())); cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(cellDataBean.getDoubleValue()));
}
cell.setCellStyle(normalCellStyle);
} }
} }
} }

View File

@ -9,7 +9,11 @@ public class LocalDoubleUtils {
private 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); return decimalFormat.format(value);
} }