SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = new Date();
String year = sdf.format(date);
getRequest().setCharacterEncoding("UTF-8");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0,year+"年儀器設備期間覈查計劃表", (short) 1);
HSSFFont font = wb.createFont();
font.setFontName("楷體");
font.setFontHeight((short) 220);
// =====過期狀態
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFFont font2 = wb.createFont();
font2.setFontName("宋體");
font2.setFontHeightInPoints((short) 11); //字體大小
cs2.setFont(font2);
cs2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框
cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框
cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
cs2.setFillForegroundColor(HSSFColor.RED.index);//設置背景色
cs2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//標題樣式
HSSFRow rowheader = sheet.createRow(0);
HSSFCell cellheader = rowheader.createCell((short) 0);
cellheader.setEncoding(HSSFCell.ENCODING_UTF_16);
HSSFCellStyle cellstyleheader = wb.createCellStyle();//樣式
HSSFFont font1 = wb.createFont();
font1.setFontName("宋體");
font1.setFontHeightInPoints((short) 18);//字體大小
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
cellstyleheader.setFont(font1);
cellstyleheader.setAlignment(HSSFCellStyle.ALIGN_CENTER);//指定單元格居中對齊
cellstyleheader.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//指定單元格垂直居中對齊
cellstyleheader.setWrapText(true);//指定單元格自動換行
cellstyleheader.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyleheader.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyleheader.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyleheader.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyleheader.setFont(font1);
cellheader.setCellStyle(cellstyleheader);
cellheader = rowheader.createCell((short) 0);
cellheader.setEncoding(HSSFCell.ENCODING_UTF_16);
cellheader.setCellValue(year+"年儀器設備期間覈查計劃表");
cellheader.setCellStyle(cellstyleheader);
sheet.addMergedRegion(new Region(0,(short) 0, 1,(short) 8));
//記錄表示樣式
HSSFRow rowheaders = sheet.createRow(2);
HSSFCell cellheaders = rowheaders.createCell((short) 0);
cellheaders.setEncoding(HSSFCell.ENCODING_UTF_16);
HSSFCellStyle cellstyleheaders = wb.createCellStyle();//樣式
HSSFFont font1s = wb.createFont();
font1s.setFontName("宋體");
font1s.setFontHeightInPoints((short) 10);//字體大小
font1s.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyleheaders.setFont(font1s);
cellstyleheaders.setAlignment(HSSFCellStyle.ALIGN_LEFT);//指定單元格居右對齊
cellstyleheaders.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//指定單元格居中對齊
cellstyleheaders.setWrapText(true); //指定單元格自動換行
cellstyleheaders.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyleheaders.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyleheaders.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyleheaders.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyleheaders.setFont(font1s);
cellheaders.setCellStyle(cellstyleheaders);
cellheaders = rowheaders.createCell((short) 0);
cellheaders.setEncoding(HSSFCell.ENCODING_UTF_16);
cellheaders.setCellValue("記錄標識:CHJ-CX-25-03-01");
cellheaders.setCellStyle(cellstyleheaders);
sheet.addMergedRegion(new Region(2, (short) 0,2, (short) 8));//合併單元格
sheet.setColumnWidth((short) 0, (short) 7000);
//表頭樣式
HSSFCellStyle titlestyle = wb.createCellStyle();//樣式
HSSFFont titlesfont = wb.createFont();
titlesfont.setFontName("宋體");// 設置字體
titlesfont.setFontHeightInPoints((short) 10);// 字體大小
titlesfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
titlestyle.setFont(titlesfont);
titlestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定單元格居中對齊
titlestyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定單元格垂直居中對齊
titlestyle.setWrapText(true); // 指定單元格自動換行
titlestyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
titlestyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
titlestyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
titlestyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 表列 樣式
HSSFRow row = sheet.createRow(3);
HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
HSSFCellStyle cellstyle = wb.createCellStyle();// 樣式
HSSFFont fonttable = wb.createFont();
fonttable.setFontHeightInPoints((short) 10);// 字體大小
cellstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 指定單元格居中對齊
cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定單元格垂直居中對齊
cellstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平對齊居中
cellstyle.setWrapText(true); // 指定單元格自動換行
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// cellstyle.setBorderBottom((short) 1);
cellstyle.setFont(fonttable);
cell.setCellStyle(cellstyle);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序號");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 0, (short) 2000);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("儀器名稱");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 1, (short) 5000);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("儀器型號");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 2, (short) 4000);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("儀器編號");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 3, (short) 4000);
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("使用部門");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 4, (short) 5000);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("最近檢定時間");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 5, (short) 4000);
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("計劃覈查時間");// cell.setCellValue("測量範圍");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 6, (short) 4000);
cell = row.createCell((short) 7);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("覈查人");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 7, (short) 4000);
cell = row.createCell((short) 8);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("備註");
cell.setCellStyle(titlestyle);
sheet.setColumnWidth((short) 8, (short) 4000);
List<Object> paramValues = new ArrayList<Object>();
StringBuilder SQL = new StringBuilder();
SQL.append("select distinct d.devicename,d.spectype,d.devicenum,d.deptid,dm.maintenancedate,dm.plandate,dm.maintenanceperson,dm.serviceresult from deviceinfo d,devicemaintenance dm where d.deviceid = dm.deviceid");
if(devicenum !=null && !"".equals(devicenum)){
SQL.append(" and d.devicenum like ?");
paramValues.add("%" + devicenum + "%");
}
if(devicename != null && !"".equals(devicename)){
SQL.append(" and d.devicename like ?");
paramValues.add("%" + devicename + "%");
}
if(office != null && !"".equals(office)){
Departmentinfo dept = departmentinfoService.getByName(office);
SQL.append(" and d.deptid like ?");
paramValues.add("%" + dept.getDeptid() + "%");
}
List<Object[]> list = maintenanceManager.createSQLQuery(
SQL.toString(), paramValues.toArray()).list();
int i = 0;
for (Object[] obj : list){
row = sheet.createRow(i+4);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i+1);
cell.setCellStyle(cellstyle);
i++;
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(obj[0] != null ? obj[0].toString() : "");// 儀器設備名稱
cell.setCellStyle(cellstyle);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(obj[1] != null ? obj[1].toString() : "");//儀器型號
cell.setCellStyle(cellstyle);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(obj[2] != null ? obj[2].toString() : "");// 儀器編號
cell.setCellStyle(cellstyle);
String deptname = "";
if (obj[3] != null) {
Departmentinfo dept = departmentinfoService.getByID(obj[3]
.toString());
deptname = dept != null ? dept.getDeptname() : "";
}
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(deptname);//使用科室
cell.setCellStyle(cellstyle);
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
sdf = new SimpleDateFormat("yyyy-MM-dd");
if(obj[4] != null){
int index = obj[4].toString().indexOf(" ");
String time = obj[4].toString().substring(0,index);
Date maintenacedate = sdf.parse(time);
cell.setCellValue(sdf.format(maintenacedate));//最近檢定時間
}else{
cell.setCellValue("");//最近檢定時間
}
cell.setCellStyle(cellstyle);
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if(obj[5] != null){
int index = obj[5].toString().indexOf(" ");
String time = obj[5].toString().substring(0,index);
Date plandate = sdf.parse(time);
cell.setCellValue(sdf.format(plandate));//最近檢定時間
}else{
cell.setCellValue("");//最近檢定時間
}
cell.setCellStyle(cellstyle);
String person = "";
if (obj[6] != null) {
Userinfo user = userInfoManager.getUserInfo(obj[6].toString());
person = user != null ? user.getLoginname() : "";
}
cell = row.createCell((short) 7);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(person);//覈查人
cell.setCellStyle(cellstyle);
cell = row.createCell((short) 8);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(obj[7] != null ? obj[7].toString() : "");//覈查結果、備註
cell.setCellStyle(cellstyle);
}
sdf = new SimpleDateFormat("yyyy");
Date datenow = new Date();
String createTime = sdf.format(datenow);
String datenowFile = createTime+"年儀器期間覈查計劃.xls";
getResponse().setContentType("application/vnd.ms-excel,charset=gbk");
getResponse().setHeader("Content-Disposition",
"attachment;filename=\""
+ java.net.URLEncoder.encode(datenowFile,"UTF-8")
+"\"");
wb.write(getResponse().getOutputStream());