package jnpf.limsController.export;
|
|
import jnpf.base.entity.DictionaryDataEntity;
|
import jnpf.limsEntity.LimsWendingxingFenxiRowVo;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
import org.apache.poi.ss.usermodel.Font;
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import java.math.BigDecimal;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Locale;
|
import java.util.Map;
|
|
public class LimsWendingxingFenxiWorkbookBuilder {
|
|
private static final String[] HEADERS = {
|
"序号", "品名", "稳定性考察编号", "批号", "录入日期", "考察周期",
|
"周期单位", "项目名称", "数据", "上限", "下限", "单位"
|
};
|
|
public Workbook build(List<LimsWendingxingFenxiRowVo> rows,
|
List<DictionaryDataEntity> periodUnits) {
|
Workbook workbook = new HSSFWorkbook();
|
Sheet sheet = workbook.createSheet("Worksheet");
|
sheet.createFreezePane(0, 1);
|
CellStyle headerStyle = createHeaderStyle(workbook);
|
CellStyle dateStyle = workbook.createCellStyle();
|
dateStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
|
CellStyle decimalStyle = workbook.createCellStyle();
|
decimalStyle.setDataFormat(workbook.createDataFormat().getFormat("0.################"));
|
|
Row header = sheet.createRow(0);
|
for (int column = 0; column < HEADERS.length; column++) {
|
Cell cell = header.createCell(column);
|
cell.setCellValue(HEADERS[column]);
|
cell.setCellStyle(headerStyle);
|
}
|
|
Map<String, String> unitLabels = unitLabels(periodUnits);
|
List<LimsWendingxingFenxiRowVo> safeRows = rows == null
|
? Collections.<LimsWendingxingFenxiRowVo>emptyList() : rows;
|
for (int index = 0; index < safeRows.size(); index++) {
|
LimsWendingxingFenxiRowVo source = safeRows.get(index);
|
Row row = sheet.createRow(index + 1);
|
row.createCell(0).setCellValue(index);
|
setText(row, 1, source.getYangpinMingcheng());
|
setText(row, 2, source.getJihuaBianhao());
|
setText(row, 3, source.getPihao());
|
if (source.getJieguoLuruRiqi() != null) {
|
Cell dateCell = row.createCell(4);
|
dateCell.setCellValue(source.getJieguoLuruRiqi());
|
dateCell.setCellStyle(dateStyle);
|
} else {
|
setText(row, 4, null);
|
}
|
BigDecimal period = decimal(source.getZhouqi());
|
if (period != null) {
|
Cell periodCell = row.createCell(5);
|
periodCell.setCellValue(period.doubleValue());
|
periodCell.setCellStyle(decimalStyle);
|
} else {
|
setText(row, 5, source.getZhouqi());
|
}
|
setText(row, 6, periodUnit(period, source.getZhouqiDanwei(), unitLabels));
|
setText(row, 7, source.getXiangmuMingcheng());
|
setText(row, 8, source.getRawResult());
|
setText(row, 9, source.getShangxian());
|
setText(row, 10, source.getXiaxian());
|
setText(row, 11, source.getUnit());
|
}
|
|
int[] widths = {8, 24, 28, 18, 16, 12, 12, 24, 18, 14, 14, 12};
|
for (int column = 0; column < widths.length; column++) {
|
sheet.setColumnWidth(column, widths[column] * 256);
|
}
|
return workbook;
|
}
|
|
private CellStyle createHeaderStyle(Workbook workbook) {
|
CellStyle style = workbook.createCellStyle();
|
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
Font font = workbook.createFont();
|
font.setBold(true);
|
style.setFont(font);
|
return style;
|
}
|
|
private Map<String, String> unitLabels(List<DictionaryDataEntity> units) {
|
Map<String, String> labels = new HashMap<>();
|
if (units == null) {
|
return labels;
|
}
|
for (DictionaryDataEntity unit : units) {
|
if (unit == null || blank(unit.getEnCode()) || blank(unit.getFullName())) {
|
continue;
|
}
|
labels.put(unit.getEnCode().trim().toLowerCase(Locale.ROOT), unit.getFullName().trim());
|
}
|
return labels;
|
}
|
|
private String periodUnit(BigDecimal period, String code, Map<String, String> labels) {
|
if (period != null && period.compareTo(BigDecimal.ZERO) == 0) {
|
return "/";
|
}
|
if (blank(code)) {
|
return "";
|
}
|
String normalized = code.trim();
|
String label = labels.get(normalized.toLowerCase(Locale.ROOT));
|
return label == null ? normalized : label;
|
}
|
|
private BigDecimal decimal(String value) {
|
if (blank(value)) {
|
return null;
|
}
|
try {
|
return new BigDecimal(value.trim());
|
} catch (NumberFormatException ignored) {
|
return null;
|
}
|
}
|
|
private void setText(Row row, int column, String value) {
|
row.createCell(column).setCellValue(value == null ? "" : value);
|
}
|
|
private boolean blank(String value) {
|
return value == null || value.trim().isEmpty();
|
}
|
}
|