excel导出

来源:互联网 发布:淘宝客采集软件13.9 编辑:程序博客网 时间:2024/06/02 07:29

java类中

public String exportExcel() throws IOException{          HttpServletRequest request = ServletActionContext.getRequest();          Map beans = new HashMap();          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");          // 填充报表          filePath += "feedBackTemplet.xls";          fileName = "HT.xls";          beans.put("createDate", df.format(new Date()));          String sql ="";        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        /*20160329 yzk 获取归档列表*/        pageInfo.setPageSize(10);        sql = " 1=1 ";        if (kssj !=null && !"".equals(kssj) && zzsj !=null && !"".equals(zzsj)){            sql += " and qdsj>='"+kssj+"' and qdsj<='"+zzsj+"'";        }        if (xmmc !=null && !"".equals(xmmc)){            sql +=" and mc like '%"+xmmc+"%'";        }        if (yfmc !=null && !"".equals(yfmc)){            sql +=" and yfmc like '%"+yfmc+"%'";        }        if (gdwzid !=null && !"".equals(gdwzid)){            sql +=" and gddm = "+gdwzid;        }        System.out.println("sql11---"+sql);        contractExcelList = contractService.listAllBySql(sql);        excelStream = makeReportFromTemplet(request                  .getRealPath("/WEB-INF")                  + filePath, beans);          if (excelStream == null) {              return INPUT;          }          return "excel";      }       public InputStream makeReportFromTemplet(String templetFileName, Map beans) {           HSSFWorkbook workbook = new HSSFWorkbook();            String[] title = new String[]{"编号","合同名称","责任单位(或责任人)","签订日期","归档位置","备注"};            HSSFSheet sheet = workbook.createSheet();            HSSFCellStyle style1 = workbook.createCellStyle();            // 设置字体样式            HSSFFont font1 = workbook.createFont();            font1.setColor(HSSFColor.BLACK.index);            font1.setFontHeight((short)400);            font1.setFontHeightInPoints((short)14);            style1.setFont(font1);            // 设置表格样式            style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);            style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);            style1.setBorderTop(HSSFCellStyle.BORDER_THIN);            style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);            style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);            style1.setBorderRight(HSSFCellStyle.BORDER_THIN);            HSSFRow row0 = sheet.createRow((short)0);            for ( int i =0;i<title.length;i++){                HSSFCell cell0 = row0.createCell((short)i);                cell0.setCellValue(new HSSFRichTextString(title[i]));                cell0.setCellStyle(style1);            }            for(int i=0;i<contractExcelList.size();i++){                HSSFRow row = sheet.createRow((short)(i+1));                HSSFCell cell1 = row.createCell((short)0);                cell1.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("bh"))));                cell1.setCellStyle(style1);                HSSFCell cell2 = row.createCell((short)1);                cell2.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("mc"))));                cell2.setCellStyle(style1);                HSSFCell cell3 = row.createCell((short)2);                cell3.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("yfmc"))));                cell3.setCellStyle(style1);                HSSFCell cell4 = row.createCell((short)3);                cell4.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("qdsj"))));                cell4.setCellStyle(style1);                HSSFCell cell5 = row.createCell((short)4);                cell5.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("gdmc")).equals("null")?"":contractExcelList.get(i).get("gdmc")));                cell5.setCellStyle(style1);                HSSFCell cell6 = row.createCell((short)5);                cell6.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("bz")).equals("null")?"":contractExcelList.get(i).get("bz")));                cell6.setCellStyle(style1);            }            /*HSSFRow row1 = sheet.createRow((short)1);            HSSFCell cell1 = row1.createCell(0);            cell1.setCellStyle(style1);            cell1.setCellValue("序号");            HSSFCell cell2 = row1.createCell(1);            cell2.setCellStyle(style1);            cell2.setCellValue("序号");*/            HSSFRow row = null;            int count = 1;            //读取文件            try {                // 将文件流写入相应流中                ByteArrayOutputStream out = new ByteArrayOutputStream();                workbook.write(out);                //返回一个inputstream类型                InputStream inputStream = new ByteArrayInputStream(out.toByteArray());                return inputStream;            } catch (Exception e) {                JmstChartException.when(true, "写入excel失败!");            }            return null;        } 

struts 配置文件 excelStream,fileName值与类中的一致,且需要getter,setter方法

<action name="contract" class="contractAction">            <result name="excel" type="stream">                  <param name="contentType">                   application/vnd.ms-excel                  </param>                  <param name="inputName">excelStream</param>                      <param name="contentDisposition">                          attachment;filename="${fileName}"                      </param>                  <param name="bufferSize">1024</param>              </result>          </action>
0 0
原创粉丝点击