导入jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
折线、柱状、饼图实体类
@Accessors(chain = true)
@Data
public class ChartPosition {
private int col1;
private int row1;
private int col2;
private int row2;
private int dx1 = 0;
private int dy1 = 0;
private int dx2 = 0;
private int dy2 = 0;
}
@Data
@Accessors(chain = true)
public class LineChart {
private String chartTitle;
private List<String> titleList;
private List<List<Double>> dataList;
private List<Object> xAxisList;
}
@Data
@Accessors(chain = true)
public class PieChart {
private List<String> titleList;
private List<Integer> dataList;
private String titleName;
}
工具类
public class ChartUtils {
private static XSSFChart createDrawingPatriarch(XSSFSheet sheet, ChartPosition chartPosition, String chartTitle) {
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, chartPosition.getCol1(), chartPosition.getRow1(), chartPosition.getCol2(), chartPosition.getRow2());
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText(chartTitle);
chart.setTitleOverlay(false);
return chart;
}
public static void createBar(XSSFSheet sheet, ChartPosition chartPosition, PieChart pieChart) {
String titleName = pieChart.getTitleName();
List<String> titleList = pieChart.getTitleList();
List<Integer> dataList = pieChart.getDataList();
XSSFChart chart = createDrawingPatriarch(sheet, chartPosition, titleName);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
XDDFDataSource<String> xData = XDDFDataSourcesFactory.fromArray(titleList.toArray(new String[]{}));
XDDFNumericalDataSource<Integer> values = XDDFDataSourcesFactory.fromArray(dataList.toArray(new Integer[]{}));
XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
bar.setVaryColors(true);
bar.setBarDirection(BarDirection.COL);
XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(xData, values);
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE_VIOLET));
series1.setFillProperties(fill);
chart.plot(bar);
}
public static void createPie(XSSFSheet sheet, ChartPosition chartPosition, PieChart pieChart) {
String titleName = pieChart.getTitleName();
List<String> titleList = pieChart.getTitleList();
List<Integer> dataList = pieChart.getDataList();
XSSFChart chart = createDrawingPatriarch(sheet, chartPosition, titleName);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromArray(titleList.toArray(new String[]{}));
XDDFNumericalDataSource<Integer> values = XDDFDataSourcesFactory.fromArray(dataList.toArray(new Integer[]{}));
XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
data.setVaryColors(true);
data.addSeries(countries, values);
chart.plot(data);
CTDLbls ctdLbls = chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).addNewDLbls();
ctdLbls.addNewShowVal().setVal(false);
ctdLbls.addNewShowLegendKey().setVal(false);
ctdLbls.addNewShowCatName().setVal(false);
ctdLbls.addNewShowSerName().setVal(false);
ctdLbls.addNewShowPercent().setVal(true);
ctdLbls.addNewShowLeaderLines().setVal(false);
ctdLbls.setSeparator("\n");
ctdLbls.addNewDLblPos().setVal(STDLblPos.Enum.forString("inEnd"));
}
public static void createLine(XSSFSheet sheet, ChartPosition chartPosition, LineChart lineChart) {
List<Object> xAxisList = lineChart.getXAxisList();
List<String> chartTitleList = lineChart.getTitleList();
List<List<Double>> chartDataList = lineChart.getDataList();
String chartTitle = lineChart.getChartTitle();
XSSFChart chart = createDrawingPatriarch(sheet, chartPosition, chartTitle);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(Arrays.copyOf(xAxisList.toArray(), xAxisList.toArray().length, String[].class));
for (int i = 0; i < chartDataList.size(); i++) {
List<Double> floats = chartDataList.get(i);
XDDFNumericalDataSource<Double> dataSource = XDDFDataSourcesFactory.fromArray(floats.toArray(new Double[]{}));
XDDFLineChartData.Series series = (XDDFLineChartData.Series) data.addSeries(countries, dataSource);
series.setTitle(chartTitleList.get(i), null);
if (chartTitleList.get(i).equals("警戒值") || chartTitleList.get(i).equals("目标值")) {
XDDFLineProperties line4 = new XDDFLineProperties();
line4.setPresetDash(new XDDFPresetLineDash(PresetLineDash.DOT));
series.setLineProperties(line4);
} else {
series.setSmooth(false);
}
series.setMarkerSize((short) 2);
series.setShowLeaderLines(true);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getDLbls()
.addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.CTR);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getDLbls().addNewShowVal().setVal(false);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getDLbls().addNewShowLegendKey().setVal(false);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getDLbls().addNewShowCatName().setVal(false);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(i).getDLbls().addNewShowSerName().setVal(false);
}
chart.plot(data);
if (chartDataList.size() == 1) {
chart.getCTChart().getPlotArea().getLineChartArray(0).addNewVaryColors().setVal(false);
}
}
public static void createTable(int rowNum, XSSFWorkbook wb, XSSFSheet sheet) {
XSSFCellStyle titleStyle = createTitleCellStyle(wb);
XSSFCellStyle contentStyle = createContentCellStyle(wb);
XSSFRow row = sheet.createRow(rowNum);
row.setHeight((short) 500);
XSSFCell cell0 = row.createCell(0);
cell0.setCellValue("动态列表");
cell0.setCellStyle(tableNameCellStyle(wb));
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 2));
XSSFRow row1 = sheet.createRow(rowNum + 1);
XSSFRow row2 = sheet.createRow(rowNum + 2);
row1.setHeight((short) 600);
row2.setHeight((short) 600);
String title0 = "序号";
XSSFCell cell = row1.createCell(0);
cell.setCellValue(title0);
CellRangeAddress region = new CellRangeAddress(rowNum + 1, rowNum + 2, 0, 0);
sheet.addMergedRegion(region);
setRegionStyle(sheet, region, titleStyle);
String title = "城市";
XSSFCell c00 = row1.createCell(1);
c00.setCellValue(title);
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowNum + 1, rowNum + 2, 1, 1);
sheet.addMergedRegion(cellRangeAddress);
setRegionStyle(sheet, cellRangeAddress, titleStyle);
String[] years = {"21年", "22年", "23年"};
int startCellIndex = 2;
int endCellIndex = 4;
for (int i = 0; i < years.length; i++) {
XSSFCell cell1 = row1.createCell(startCellIndex);
cell1.setCellValue(years[i]);
CellRangeAddress cellAddresses = new CellRangeAddress(rowNum + 1, rowNum + 1, startCellIndex, endCellIndex);
sheet.addMergedRegion(cellAddresses);
setRegionStyle(sheet, cellAddresses, titleStyle);
XSSFCell cell11 = row2.createCell(startCellIndex++);
cell11.setCellValue("动态列1");
cell11.setCellStyle(titleStyle);
XSSFCell cell2 = row2.createCell(startCellIndex++);
cell2.setCellValue("动态列2");
cell2.setCellStyle(titleStyle);
XSSFCell cell3 = row2.createCell(startCellIndex++);
cell3.setCellValue("动态列3");
cell3.setCellStyle(titleStyle);
endCellIndex += 3;
}
rowNum += 3;
for (int j = 0; j < 10; j++) {
int k = j + 1;
XSSFRow tempRow = sheet.createRow(rowNum);
rowNum++;
XSSFCell cell11 = tempRow.createCell(0);
cell11.setCellValue(k);
cell11.setCellStyle(contentStyle);
XSSFCell cell2 = tempRow.createCell(1);
cell2.setCellValue("城市" + k);
cell2.setCellStyle(contentStyle);
int columnIndex = 2;
int k1 = 1;
for (int i = 0; i < years.length; i++) {
XSSFCell cell3 = tempRow.createCell(columnIndex++);
cell3.setCellValue("测试" + k1++);
cell3.setCellStyle(contentStyle);
XSSFCell cell4 = tempRow.createCell(columnIndex++);
cell4.setCellValue("测试" + k1++);
cell4.setCellStyle(contentStyle);
XSSFCell cell5 = tempRow.createCell(columnIndex++);
cell5.setCellValue("测试" + k1++);
cell5.setCellStyle(contentStyle);
}
}
}
@SuppressWarnings("deprecation")
public static void setRegionStyle(XSSFSheet sheet, CellRangeAddress region, XSSFCellStyle cs) {
for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
XSSFRow row = sheet.getRow(i);
if (null == row) row = sheet.createRow(i);
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
XSSFCell cell = row.getCell(j);
if (null == cell) cell = row.createCell(j);
cell.setCellStyle(cs);
}
}
}
public static XSSFCellStyle tableNameCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
XSSFFont headerFont1 = wb.createFont();
headerFont1.setBold(true);
headerFont1.setFontName("黑体");
headerFont1.setFontHeightInPoints((short) 12);
cellStyle.setFont(headerFont1);
return cellStyle;
}
private static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
XSSFFont headerFont1 = wb.createFont();
headerFont1.setFontName("黑体");
headerFont1.setFontHeightInPoints((short) 12);
cellStyle.setFont(headerFont1);
return cellStyle;
}
private static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setWrapText(true);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
XSSFFont font = wb.createFont();
font.setColor((short) 8);
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
}
}
接口
public String exportQualityDataContrast(HttpServletResponse response, List<QualityDataContrast> list, QualityDataContrast qualityDataContrast) {
String fileName = "";
String tmpPath = "";
try {
XSSFWorkbook workbook = new XSSFWorkbook();
int nowYear = Integer.parseInt(qualityDataContrast.getDBRQ().substring(0, 4));
int lastYear = nowYear - 1;
String sheetName = lastYear + "、" + nowYear + "数据对比";
XSSFSheet sheet = workbook.createSheet(sheetName);
String title = lastYear + "、" + nowYear + "数据对比";
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setWrapText(true);
Font font1 = workbook.createFont();
font1.setFontHeightInPoints((short) 16);
font1.setColor(Font.COLOR_NORMAL);
font1.setBold(true);
font1.setFontName("微软雅黑");
style.setFont(font1);
Font font2 = workbook.createFont();
font2.setFontHeightInPoints((short) 12);
font2.setColor(Font.COLOR_NORMAL);
font1.setBold(true);
font2.setFontName("微软雅黑");
Font font3 = workbook.createFont();
font3.setFontHeightInPoints((short) 12);
font3.setColor(Font.COLOR_NORMAL);
font3.setFontName("微软雅黑");
CellStyle cellStyleRow2 = workbook.createCellStyle();
cellStyleRow2.setBorderTop(BorderStyle.THIN);
cellStyleRow2.setBorderBottom(BorderStyle.THIN);
cellStyleRow2.setBorderLeft(BorderStyle.THIN);
cellStyleRow2.setBorderRight(BorderStyle.THIN);
cellStyleRow2.setAlignment(HorizontalAlignment.CENTER);
cellStyleRow2.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleRow2.setFont(font2);
cellStyleRow2.setWrapText(true);
CellStyle cellStyleRow3 = workbook.createCellStyle();
cellStyleRow3.setBorderTop(BorderStyle.THIN);
cellStyleRow3.setBorderBottom(BorderStyle.THIN);
cellStyleRow3.setBorderLeft(BorderStyle.THIN);
cellStyleRow3.setBorderRight(BorderStyle.THIN);
cellStyleRow3.setAlignment(HorizontalAlignment.CENTER);
cellStyleRow3.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyleRow3.setFont(font3);
Row row0 = sheet.createRow(0);
row0.setHeight((short) 400);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 24));
Cell cell = row0.createCell(0);
cell.setCellValue(title);
cell.setCellStyle(style);
Map<Integer, Integer> maxWidth = new HashMap<>();
Row row1 = sheet.createRow(1);
row1.setHeight((short) 300);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 4));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 5, 8));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 9, 12));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 13, 16));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 17, 20));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 21, 24));
Cell cWeek0 = row1.createCell(0);
cWeek0.setCellStyle(cellStyleRow2);
Cell cWeek1 = row1.createCell(1);
cWeek1.setCellValue("患者身份识别正确率");
cWeek1.setCellStyle(cellStyleRow2);
Cell cWeek2 = row1.createCell(2);
cWeek2.setCellStyle(cellStyleRow2);
Cell cWeek3 = row1.createCell(3);
cWeek3.setCellStyle(cellStyleRow2);
Cell cWeek4 = row1.createCell(4);
cWeek4.setCellStyle(cellStyleRow2);
Cell cWeek5 = row1.createCell(5);
cWeek5.setCellValue("急救仪器完备率");
cWeek5.setCellStyle(cellStyleRow2);
Cell cWeek6 = row1.createCell(6);
cWeek6.setCellStyle(cellStyleRow2);
Cell cWeek7 = row1.createCell(7);
cWeek7.setCellStyle(cellStyleRow2);
Cell cWeek8 = row1.createCell(8);
cWeek8.setCellStyle(cellStyleRow2);
Cell cWeek9 = row1.createCell(9);
cWeek9.setCellValue("抢救物品完备率");
cWeek9.setCellStyle(cellStyleRow2);
Cell cWeek10 = row1.createCell(10);
cWeek10.setCellStyle(cellStyleRow2);
Cell cWeek11 = row1.createCell(11);
cWeek11.setCellStyle(cellStyleRow2);
Cell cWeek12 = row1.createCell(12);
cWeek12.setCellStyle(cellStyleRow2);
Cell cWeek13 = row1.createCell(13);
cWeek13.setCellValue("抢救药品完备率");
cWeek13.setCellStyle(cellStyleRow2);
Cell cWeek14 = row1.createCell(14);
cWeek14.setCellStyle(cellStyleRow2);
Cell cWeek15 = row1.createCell(15);
cWeek15.setCellStyle(cellStyleRow2);
Cell cWeek16 = row1.createCell(16);
cWeek16.setCellStyle(cellStyleRow2);
Cell cWeek17 = row1.createCell(17);
cWeek17.setCellValue("出车护士反应时间达标率");
cWeek17.setCellStyle(cellStyleRow2);
Cell cWeek18 = row1.createCell(18);
cWeek18.setCellStyle(cellStyleRow2);
Cell cWeek19 = row1.createCell(19);
cWeek19.setCellStyle(cellStyleRow2);
Cell cWeek20 = row1.createCell(20);
cWeek20.setCellStyle(cellStyleRow2);
Cell cWeek21 = row1.createCell(21);
cWeek21.setCellValue("重点环节交接正确率");
cWeek21.setCellStyle(cellStyleRow2);
Cell cWeek22 = row1.createCell(22);
cWeek22.setCellStyle(cellStyleRow2);
Cell cWeek23 = row1.createCell(23);
cWeek23.setCellStyle(cellStyleRow2);
Cell cWeek24 = row1.createCell(24);
cWeek24.setCellStyle(cellStyleRow2);
Row row2 = sheet.createRow(2);
Cell cWeek2_0 = row2.createCell(0);
cWeek2_0.setCellValue("月份");
maxWidth.put(0, "月份".getBytes().length * 256 + 200);
cWeek2_0.setCellStyle(cellStyleRow2);
Cell cWeek2_1 = row2.createCell(1);
cWeek2_1.setCellValue(lastYear + "患者身份识别正确率(%)");
maxWidth.put(1, "患者身份识别正确率(%)".getBytes().length * 256 + 200);
cWeek2_1.setCellStyle(cellStyleRow2);
Cell cWeek2_2 = row2.createCell(2);
cWeek2_2.setCellValue(nowYear + "患者身份识别正确率(%)");
maxWidth.put(2, "患者身份识别正确率(%)".getBytes().length * 256 + 200);
cWeek2_2.setCellStyle(cellStyleRow2);
Cell cWeek2_3 = row2.createCell(3);
cWeek2_3.setCellValue("警戒值");
maxWidth.put(3, "警戒值".getBytes().length * 256 + 200);
cWeek2_3.setCellStyle(cellStyleRow2);
Cell cWeek2_4 = row2.createCell(4);
cWeek2_4.setCellValue("目标值");
maxWidth.put(4, "目标值".getBytes().length * 256 + 200);
cWeek2_4.setCellStyle(cellStyleRow2);
Cell cWeek2_5 = row2.createCell(5);
cWeek2_5.setCellValue(lastYear + "急救仪器完备率(%)");
maxWidth.put(5, "急救仪器完备率(%)".getBytes().length * 256 + 200);
cWeek2_5.setCellStyle(cellStyleRow2);
Cell cWeek2_6 = row2.createCell(6);
cWeek2_6.setCellValue(nowYear + "急救仪器完备率(%)");
maxWidth.put(6, "急救仪器完备率(%)".getBytes().length * 256 + 200);
cWeek2_6.setCellStyle(cellStyleRow2);
Cell cWeek2_7 = row2.createCell(7);
cWeek2_7.setCellValue("警戒值");
maxWidth.put(7, "警戒值".getBytes().length * 256 + 200);
cWeek2_7.setCellStyle(cellStyleRow2);
Cell cWeek2_8 = row2.createCell(8);
cWeek2_8.setCellValue("目标值");
maxWidth.put(8, "目标值".getBytes().length * 256 + 200);
cWeek2_8.setCellStyle(cellStyleRow2);
Cell cWeek2_9 = row2.createCell(9);
cWeek2_9.setCellValue(lastYear + "抢救物品完备率(%)");
maxWidth.put(9, "抢救物品完备率(%)".getBytes().length * 256 + 200);
cWeek2_9.setCellStyle(cellStyleRow2);
Cell cWeek2_10 = row2.createCell(10);
cWeek2_10.setCellValue(nowYear + "抢救物品完备率(%)");
maxWidth.put(10, "抢救物品完备率(%)".getBytes().length * 256 + 200);
cWeek2_10.setCellStyle(cellStyleRow2);
Cell cWeek2_11 = row2.createCell(11);
cWeek2_11.setCellValue("警戒值");
maxWidth.put(11, "警戒值".getBytes().length * 256 + 200);
cWeek2_11.setCellStyle(cellStyleRow2);
Cell cWeek2_12 = row2.createCell(12);
cWeek2_12.setCellValue("目标值");
maxWidth.put(12, "目标值".getBytes().length * 256 + 200);
cWeek2_12.setCellStyle(cellStyleRow2);
Cell cWeek2_13 = row2.createCell(13);
cWeek2_13.setCellValue(lastYear + "抢救药品完备率(%)");
maxWidth.put(13, "抢救药品完备率(%)".getBytes().length * 256 + 200);
cWeek2_13.setCellStyle(cellStyleRow2);
Cell cWeek2_14 = row2.createCell(14);
cWeek2_14.setCellValue(nowYear + "抢救药品完备率(%)");
maxWidth.put(14, "抢救药品完备率(%)".getBytes().length * 256 + 200);
cWeek2_14.setCellStyle(cellStyleRow2);
Cell cWeek2_15 = row2.createCell(15);
cWeek2_15.setCellValue("警戒值");
maxWidth.put(15, "警戒值".getBytes().length * 256 + 200);
cWeek2_15.setCellStyle(cellStyleRow2);
Cell cWeek2_16 = row2.createCell(16);
cWeek2_16.setCellValue("目标值");
maxWidth.put(16, "目标值".getBytes().length * 256 + 200);
cWeek2_16.setCellStyle(cellStyleRow2);
Cell cWeek2_17 = row2.createCell(17);
cWeek2_17.setCellValue(lastYear + "出车护士反应时间达标率(%)");
maxWidth.put(17, "出车护士反应时间达标率(%)".getBytes().length * 256 + 200);
cWeek2_17.setCellStyle(cellStyleRow2);
Cell cWeek2_18 = row2.createCell(18);
cWeek2_18.setCellValue(nowYear + "出车护士反应时间达标率(%)");
maxWidth.put(18, "出车护士反应时间达标率(%)".getBytes().length * 256 + 200);
cWeek2_18.setCellStyle(cellStyleRow2);
Cell cWeek2_19 = row2.createCell(19);
cWeek2_19.setCellValue("警戒值");
maxWidth.put(19, "警戒值".getBytes().length * 256 + 200);
cWeek2_19.setCellStyle(cellStyleRow2);
Cell cWeek2_20 = row2.createCell(20);
cWeek2_20.setCellValue("目标值");
maxWidth.put(20, "目标值".getBytes().length * 256 + 200);
cWeek2_20.setCellStyle(cellStyleRow2);
Cell cWeek2_21 = row2.createCell(21);
cWeek2_21.setCellValue(lastYear + "重点环节交接正确率(%)");
maxWidth.put(21, "重点环节交接正确率(%)".getBytes().length * 256 + 200);
cWeek2_21.setCellStyle(cellStyleRow2);
Cell cWeek2_22 = row2.createCell(22);
cWeek2_22.setCellValue(nowYear + "重点环节交接正确率(%)");
maxWidth.put(22, "重点环节交接正确率(%)".getBytes().length * 256 + 200);
cWeek2_22.setCellStyle(cellStyleRow2);
Cell cWeek2_23 = row2.createCell(23);
cWeek2_23.setCellValue("警戒值");
maxWidth.put(23, "警戒值".getBytes().length * 256 + 200);
cWeek2_23.setCellStyle(cellStyleRow2);
Cell cWeek2_24 = row2.createCell(24);
cWeek2_24.setCellValue("目标值");
maxWidth.put(24, "目标值".getBytes().length * 256 + 200);
cWeek2_24.setCellStyle(cellStyleRow2);
for (int i = 0; i < 25; i++) {
sheet.setColumnWidth(i, maxWidth.get(i));
}
qualityDataContrast.setDBRQ(String.valueOf(nowYear));
Map<String, List<String>> map1 = dataContrastService.selectQualityDataContrastHzsfsb(qualityDataContrast);
String cTitle1 = "患者身份识别正确率";
List<LineChart> lineCharts1 = initLineChart(nowYear, lastYear, map1, cTitle1);
for (LineChart lineChart : lineCharts1) {
chartPosition = new ChartPosition()
.setRow1(16)
.setCol1(1)
.setRow2(32)
.setCol2(4);
ChartUtils.createLine(sheet, chartPosition, lineChart);
}
fileName = title;
tmpPath = EmergencyConfig.getProfile() + "/" + fileName + ".xlsx";
FileOutputStream fos = new FileOutputStream(tmpPath);
workbook.write(fos);
fos.flush();
fos.close();
down(response, tmpPath, fileName);
}catch (Exception e){
return null;
}finally {
File file = new File(tmpPath);
file.delete();
}
return fileName;
}
public List<LineChart> initLineChart(int nowYear, int lastYear, Map<String, List<String>> map, String title) {
List<LineChart> lineCharts = new ArrayList<>();
List<String> strings = new ArrayList<>();
strings.add(lastYear + title);
strings.add(nowYear + title);
strings.add("警戒值");
strings.add("目标值");
List<Double> lastData = new ArrayList<>();
List<Double> nowData = new ArrayList<>();
if (StringUtils.isNotNull(map.get("hzsfsb1"))) {
for (String hzsfsb1 : map.get("hzsfsb1")) {
lastData.add(Double.valueOf(hzsfsb1));
}
for (String hzsfsb2 : map.get("hzsfsb2")) {
nowData.add(Double.valueOf(hzsfsb2));
}
} else if (StringUtils.isNotNull(map.get("jjyq1"))) {
for (String jjyq1 : map.get("jjyq1")) {
lastData.add(Double.valueOf(jjyq1));
}
for (String jjyq2 : map.get("jjyq2")) {
nowData.add(Double.valueOf(jjyq2));
}
} else if (StringUtils.isNotNull(map.get("qjwp1"))) {
for (String qjwp1 : map.get("qjwp1")) {
lastData.add(Double.valueOf(qjwp1));
}
for (String qjwp2 : map.get("qjwp2")) {
nowData.add(Double.valueOf(qjwp2));
}
} else if (StringUtils.isNotNull(map.get("qjyp1"))) {
for (String qjyp1 : map.get("qjyp1")) {
lastData.add(Double.valueOf(qjyp1));
}
for (String qjyp2 : map.get("qjyp2")) {
nowData.add(Double.valueOf(qjyp2));
}
} else if (StringUtils.isNotNull(map.get("cchsfysj1"))) {
for (String cchsfysj1 : map.get("cchsfysj1")) {
lastData.add(Double.valueOf(cchsfysj1));
}
for (String cchsfysj2 : map.get("cchsfysj2")) {
nowData.add(Double.valueOf(cchsfysj2));
}
} else if (StringUtils.isNotNull(map.get("zdhjjj1"))) {
for (String zdhjjj1 : map.get("zdhjjj1")) {
lastData.add(Double.valueOf(zdhjjj1));
}
for (String zdhjjj2 : map.get("zdhjjj2")) {
nowData.add(Double.valueOf(zdhjjj2));
}
}
List<Double> jjz = new ArrayList<>();
List<Double> mbz = new ArrayList<>();
for (String jjz1 : map.get("jjz")) {
jjz.add(Double.valueOf(jjz1));
}
for (String mbz1 : map.get("mbz")) {
mbz.add(Double.valueOf(mbz1));
}
List<List<Double>> list = Arrays.asList(lastData, nowData, jjz, mbz);
lineCharts.add(new LineChart()
.setChartTitle(title)
.setTitleList(strings)
.setDataList(list)
.setXAxisList(Arrays.<Object>asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月")));
return lineCharts;
}
private void down(HttpServletResponse response, String filePath, String realFileName) {
String percentEncodedFileName = null;
try {
percentEncodedFileName = percentEncode(realFileName);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=").append(percentEncodedFileName).append(";").append("filename*=").append("utf-8''").append(percentEncodedFileName);
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
response.setHeader("Content-disposition", contentDispositionValue.toString());
response.setHeader("download-filename", percentEncodedFileName);
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());) {
byte[] buff = new byte[1024];
int len = 0;
while ((len = bis.read(buff)) > 0) {
bos.write(buff, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String percentEncode(String s) throws UnsupportedEncodingException {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode.replaceAll("\\+", "%20");
}