/**
* 導出EXCEL---poi
*/
@Override
public void pivot(HttpServletResponse response) throws Exception{
//創建excel在內存中 .xls
Workbook wb = new XSSFWorkbook();
//創建一個sheet頁
Sheet sheet = wb.createSheet("患者信息一覽表");
// //獲取單元格格式信息
ExcelCellPojo pojo = new ExcelCellPojo(wb);
//表頭
String[] headStr={"id","入ICU時間","出院時間","轉科時間","診斷情況","轉歸","呼吸機使用","高流量","PICCO","ECMO","CRRT"};
//開始的行號
int rowIndex=0;
//創建標題欄
Row title =sheet.createRow(rowIndex);
//設置行高
title.setHeightInPoints(75);
title.createCell(0).setCellValue("患者信息一覽表");
title.getCell(0).setCellStyle(pojo.getTitleCellStyle());
//創建合並單元格
CellRangeAddress cra=new CellRangeAddress(0,0,0,headStr.length-1);
sheet.addMergedRegion(cra);
// 下邊框
RegionUtil.setBorderBottom(BorderStyle.THIN, cra, sheet);
// 左邊框
RegionUtil.setBorderLeft(BorderStyle.THIN, cra, sheet);
// 有邊框
RegionUtil.setBorderRight(BorderStyle.THIN, cra, sheet);
// 上邊框
RegionUtil.setBorderTop(BorderStyle.THIN, cra, sheet);
//創建表頭的一行
Row header = sheet.createRow(++rowIndex);
//設置行高
header.setHeightInPoints(60);
//循環創建表頭賦值
for (int i = 0; i < headStr.length; i++) {
//設置列寬度 -第1列
sheet.setColumnWidth(i,5400);
//創建表頭第一列並且賦值
header.createCell(i).setCellValue(headStr[i]);
//設置單元格樣式
header.getCell(i).setCellStyle(pojo.getHeaderStyle());
}
//查詢數據
List<TestPatientInf> patientList = super.selectAll();
for (TestPatientInf patientInf:patientList) {
++rowIndex;
//從第2行開始創建
Row row = sheet.createRow(rowIndex);
row.setHeightInPoints(40);
row.createCell(0).setCellValue(patientInf.getId());
row.getCell(0).setCellStyle(pojo.getCellStyle());
row.createCell(1).setCellValue(patientInf.getIcuTimeIn());
row.getCell(1).setCellStyle(pojo.getDateCellStyle());
row.createCell(2).setCellValue(patientInf.getDischargeTime());
row.getCell(2).setCellStyle(pojo.getDateCellStyle());
row.createCell(3).setCellValue(patientInf.getCollegeTime());
row.getCell(3).setCellStyle(pojo.getDateCellStyle());
row.createCell(4).setCellValue(patientInf.getDiagnosis());
row.getCell(4).setCellStyle(pojo.getCellStyle());
row.createCell(5).setCellValue(patientInf.getOutcome());
row.getCell(5).setCellStyle(pojo.getCellStyle());
row.createCell(6).setCellValue(StringUtil.isNullOrBlank(patientInf.getHxjsy())?"無":patientInf.getHxjsy());
row.getCell(6).setCellStyle(pojo.getCellStyle());
row.createCell(7).setCellValue(StringUtil.isNullOrBlank(patientInf.getGll())?"無":patientInf.getGll());
row.getCell(7).setCellStyle(pojo.getCellStyle());
row.createCell(8).setCellValue(patientInf.getPicco()==null?0:patientInf.getPicco());
row.getCell(8).setCellStyle(pojo.getDoubleCellStyle());
row.createCell(9).setCellValue(patientInf.getEcmo()==null?0:patientInf.getEcmo());
row.getCell(9).setCellStyle(pojo.getCellStyle());
row.createCell(10).setCellValue(patientInf.getCrrt()==null?0:patientInf.getCrrt());
row.getCell(10).setCellStyle(pojo.getCellStyle());
}
//創建數據透視表--新的sheet頁碼
//為需要匯總和創建分析的數據創建緩存
XSSFSheet pivotSheet = (XSSFSheet)wb.createSheet("患者信息透視表");
//左邊起始單元格
CellReference leftStart=new CellReference(ExcelUtils.getCellLocation(2,1));
//右邊結束單元格
CellReference rightEnd=new CellReference(ExcelUtils.getCellLocation(rowIndex+1,headStr.length));
// 數據透視表生產的起點單元格位置
CellReference ptStartCell = new CellReference("A4");
//創建數據透視表格
AreaReference area = new AreaReference(leftStart, rightEnd, SpreadsheetVersion.EXCEL2007);
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(area,ptStartCell,sheet);
//頭上的列標簽
pivotTable.addColLabel(1);
//透視表 列值
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 0,"計數項:入ICU時間");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 8,"求和項:PICCO");
//透視表 行標簽
pivotTable.addRowLabel(0);
//透視表 行的值
pivotTable.addRowLabel(1);
pivotTable.addRowLabel(2);
pivotTable.addRowLabel(3);
pivotTable.addRowLabel(4);
ExcelUtils.exportExcel(wb,"test_aaa.xlsx",response);
}
package cn.com.zhengya.framework.utils.excel.entity;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.*;
/**
* Excel默認樣式
* @author luwl
*/
public class ExcelCellPojo {
/**
* 表頭樣式
*/
private CellStyle headerStyle;
/**
* 默認單元格樣式
*/
private CellStyle cellStyle;
/**
* 標題欄樣式
*/
private CellStyle titleCellStyle;
/**
* 日期單元格樣式
*/
private CellStyle dateCellStyle;
/**
* 數字類型單元格樣式
*/
private CellStyle doubleCellStyle;
public ExcelCellPojo(Workbook wb){
this.setHeaderStyle(wb);
this.setCellStyle(wb);
this.setDateCellStyle(wb);
this.setTitleCellStyle(wb);
this.setDoubleCellStyle(wb);
}
public CellStyle getHeaderStyle() {
return headerStyle;
}
public CellStyle getCellStyle() {
return cellStyle;
}
public CellStyle getDateCellStyle() {
return dateCellStyle;
}
public CellStyle getTitleCellStyle() {
return titleCellStyle;
}
public CellStyle getDoubleCellStyle() {
return doubleCellStyle;
}
private void setHeaderStyle(Workbook wb){
// 生成表頭單元樣式
headerStyle = wb.createCellStyle();
//表頭樣式
headerStyle.setFillForegroundColor((short)1);
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//表頭邊框
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
headerStyle.setBorderTop(BorderStyle.THIN);
//水平方向-居中對齊
headerStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直方向-垂直居中
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//字體
Font font = wb.createFont();
//字體大小
font.setFontHeightInPoints((short) 16);
//字體加粗
font.setBold(true);
headerStyle.setFont(font);
}
private void setCellStyle(Workbook wb){
// 生成單元格式樣(基礎式樣)
cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
//水平方向-居中對齊
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直方向-垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
}
private void setDateCellStyle(Workbook wb){
// 生成單元格式樣-日期類型(yyyy/mm/dd日期格式)
dateCellStyle = wb.createCellStyle();
dateCellStyle.cloneStyleFrom(this.cellStyle);
DataFormat format = wb.createDataFormat();
dateCellStyle.setDataFormat(format.getFormat("yyyy/mm/dd"));
}
private void setTitleCellStyle(Workbook wb) {
//生成標題單元格樣式
titleCellStyle = wb.createCellStyle();
//表頭樣式
titleCellStyle.setFillForegroundColor((short)1);
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//表頭邊框
titleCellStyle.setBorderBottom(BorderStyle.THIN);
titleCellStyle.setBorderLeft(BorderStyle.THIN);
titleCellStyle.setBorderRight(BorderStyle.THIN);
titleCellStyle.setBorderTop(BorderStyle.THIN);
//水平方向-居中對齊
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直方向-垂直居中
titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//字體
Font font = wb.createFont();
//字體大小
font.setFontHeightInPoints((short) 20);
titleCellStyle.setFont(font);
}
private void setDoubleCellStyle(Workbook wb) {
// 生成單元格式樣(基礎式樣)
doubleCellStyle = wb.createCellStyle();
doubleCellStyle.setBorderBottom(BorderStyle.THIN);
doubleCellStyle.setBorderLeft(BorderStyle.THIN);
doubleCellStyle.setBorderRight(BorderStyle.THIN);
doubleCellStyle.setBorderTop(BorderStyle.THIN);
//水平方向-居中對齊
doubleCellStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直方向-垂直居中
doubleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
doubleCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
doubleCellStyle.setWrapText(true);
}
}
/**
* Http導出Excel
* @param response
*/
public static void exportExcel(Workbook wb,String fileName,HttpServletResponse response){
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream out = null;
try {
// 通過流將excel寫出
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] bytes = bos.toByteArray();
// 獲取輸出流
out = response.getOutputStream();
// 設置頭信息
response.setContentLength(bytes.length);
response.setHeader("Content-disposition", "attachment;filename="
+ new String(fileName.getBytes("GBK"), "ISO8859-1"));
// 通過流將excel寫出
wb.write(out);
wb.close();
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>