java中实现Excel导出


            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());
        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章