java实现Excel表格导出

导出excel学习网址:http://www.cnblogs.com/fx2008/p/3941908.html

poi.jar可以操作的EXcel版本? poi.jar中只能操作97-2003版本的Excel

了解Excel组成:

1.excel文档后缀名.xls结尾
2.excel文档有若干个(小页)组成,小页之间空间,彼此独立
3.每一个【小页】都有自己的名称以及位置
4.每一个【小页】都会包含大量的【单元格】这些单元格是Excel保存数据的基本单元
5.【单元格】{保存数据,坐在行数,所在列数}

poi提供的API:

1.HSSFWorkbook------》描述内存中的Excel文档的对象    
2.HSSFSheet---------》描述内存中的【小页】对象
3.HSSFCell-----------》描述内存中的【单元格】对象
4.HSSFRow------------》描述内存中的【行】对象

poi的maven依赖:

  <!--export的依赖poi  -->
   <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.9</version>
    </dependency>
    <!-- 使用poi生成excel表格 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.9</version>
    </dependency>

把页面数据以Excel导出:

response.reset();清除buffer。设置页面不缓存注意事项:
response.setHeader("Content- disposition","attachment;filename="+new                   String(str.getBytes("gb2312"),"iso8859-1")); // 客户使用目标另存为对话框保存指定文件
response.setHeader("Content_Length",length);设置头文件的长度为指定文件的长度

实例一:

public ActionResult excelPrint() {   
    HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件   
    HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet   
    sheet.createFreezePane(1, 3);// 冻结   
    // 设置列宽   
    sheet.setColumnWidth(0, 1000);   
    sheet.setColumnWidth(1, 3500);   
    sheet.setColumnWidth(2, 3500);   
    sheet.setColumnWidth(3, 6500);   
    sheet.setColumnWidth(4, 6500);   
    sheet.setColumnWidth(5, 6500);   
    sheet.setColumnWidth(6, 6500);   
    sheet.setColumnWidth(7, 2500);   
    // Sheet样式   
    HSSFCellStyle sheetStyle = workbook.createCellStyle();   
    // 背景色的设定   
    sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);   
    // 前景色的设定   
    sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);   
    // 填充模式   
    sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);   
    // 设置列的样式   
    for (int i = 0; i <= 14; i++) {   
      sheet.setDefaultColumnStyle((short) i, sheetStyle);   
    }   
    // 设置字体   
    HSSFFont headfont = workbook.createFont();   
    headfont.setFontName("黑体");   
    headfont.setFontHeightInPoints((short) 22);// 字体大小   
    headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗   
    // 另一个样式   
    HSSFCellStyle headstyle = workbook.createCellStyle();   
    headstyle.setFont(headfont);   
    headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
    headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中   
    headstyle.setLocked(true);   
    headstyle.setWrapText(true);// 自动换行   
    // 另一个字体样式   
    HSSFFont columnHeadFont = workbook.createFont();   
    columnHeadFont.setFontName("宋体");   
    columnHeadFont.setFontHeightInPoints((short) 10);   
    columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
    // 列头的样式   
    HSSFCellStyle columnHeadStyle = workbook.createCellStyle();   
    columnHeadStyle.setFont(columnHeadFont);   
    columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
    columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中   
    columnHeadStyle.setLocked(true);   
    columnHeadStyle.setWrapText(true);   
    columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色   
    columnHeadStyle.setBorderLeft((short) 1);// 边框的大小   
    columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色   
    columnHeadStyle.setBorderRight((short) 1);// 边框的大小   
    columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体   
    columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色   
    // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)   
    columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);   
  
    HSSFFont font = workbook.createFont();   
    font.setFontName("宋体");   
    font.setFontHeightInPoints((short) 10);   
    // 普通单元格样式   
    HSSFCellStyle style = workbook.createCellStyle();   
    style.setFont(font);   
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中   
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中   
    style.setWrapText(true);   
    style.setLeftBorderColor(HSSFColor.BLACK.index);   
    style.setBorderLeft((short) 1);   
    style.setRightBorderColor(HSSFColor.BLACK.index);   
    style.setBorderRight((short) 1);   
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体   
    style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.   
    style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.   
    // 另一个样式   
    HSSFCellStyle centerstyle = workbook.createCellStyle();   
    centerstyle.setFont(font);   
    centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中   
    centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中   
    centerstyle.setWrapText(true);   
    centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);   
    centerstyle.setBorderLeft((short) 1);   
    centerstyle.setRightBorderColor(HSSFColor.BLACK.index);   
    centerstyle.setBorderRight((short) 1);   
    centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体   
    centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.   
    centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.   
  
    try {   
      // 创建第一行   
      HSSFRow row0 = sheet.createRow(0);   
      // 设置行高   
      row0.setHeight((short) 900);   
      // 创建第一列   
      HSSFCell cell0 = row0.createCell(0);   
      cell0.setCellValue(new HSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));   
      cell0.setCellStyle(headstyle);   
      /**  
       * 合并单元格  
       *    第一个参数:第一个单元格的行数(从0开始)  
       *    第二个参数:第二个单元格的行数(从0开始)  
       *    第三个参数:第一个单元格的列数(从0开始)  
       *    第四个参数:第二个单元格的列数(从0开始)  
       */  
      CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);   
      sheet.addMergedRegion(range);   
      // 创建第二行   
      HSSFRow row1 = sheet.createRow(1);   
      HSSFCell cell1 = row1.createCell(0);   
      cell1.setCellValue(new HSSFRichTextString("本次会议时间:2009年8月31日       前次会议时间:2009年8月24日"));   
      cell1.setCellStyle(centerstyle);   
      // 合并单元格   
      range = new CellRangeAddress(1, 2, 0, 7);   
      sheet.addMergedRegion(range);   
      // 第三行   
      HSSFRow row2 = sheet.createRow(3);   
      row2.setHeight((short) 750);   
      HSSFCell cell = row2.createCell(0);   
      cell.setCellValue(new HSSFRichTextString("责任者"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(1);   
      cell.setCellValue(new HSSFRichTextString("成熟度排序"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(2);   
      cell.setCellValue(new HSSFRichTextString("事项"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(3);   
      cell.setCellValue(new HSSFRichTextString("前次会议要求/n/新项目的项目概要"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(4);   
      cell.setCellValue(new HSSFRichTextString("上周工作进展"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(5);   
      cell.setCellValue(new HSSFRichTextString("本周工作计划"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(6);   
      cell.setCellValue(new HSSFRichTextString("问题和建议"));   
      cell.setCellStyle(columnHeadStyle);   
      cell = row2.createCell(7);   
      cell.setCellValue(new HSSFRichTextString("备 注"));   
      cell.setCellStyle(columnHeadStyle);   
      // 访问数据库,得到数据集   
      List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();   
      int m = 4;   
      int k = 4;   
      for (int i = 0; i < deitelVOList.size(); i++) {   
        DeitelVO vo = deitelVOList.get(i);   
        String dname = vo.getDname();   
        List<Workinfo> workList = vo.getWorkInfoList();   
        HSSFRow row = sheet.createRow(m);   
        cell = row.createCell(0);   
        cell.setCellValue(new HSSFRichTextString(dname));   
        cell.setCellStyle(centerstyle);   
        // 合并单元格   
        range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);   
        sheet.addMergedRegion(range);   
        m = m + workList.size();   
  
        for (int j = 0; j < workList.size(); j++) {   
          Workinfo w = workList.get(j);   
          // 遍历数据集创建Excel的行   
          row = sheet.getRow(k + j);   
          if (null == row) {   
            row = sheet.createRow(k + j);   
          }   
          cell = row.createCell(1);   
          cell.setCellValue(w.getWnumber());   
          cell.setCellStyle(centerstyle);   
          cell = row.createCell(2);   
          cell.setCellValue(new HSSFRichTextString(w.getWitem()));   
          cell.setCellStyle(style);   
          cell = row.createCell(3);   
          cell.setCellValue(new HSSFRichTextString(w.getWmeting()));   
          cell.setCellStyle(style);   
          cell = row.createCell(4);   
          cell.setCellValue(new HSSFRichTextString(w.getWbweek()));   
          cell.setCellStyle(style);   
          cell = row.createCell(5);   
          cell.setCellValue(new HSSFRichTextString(w.getWtweek()));   
          cell.setCellStyle(style);   
          cell = row.createCell(6);   
          cell.setCellValue(new HSSFRichTextString(w.getWproblem()));   
          cell.setCellStyle(style);   
          cell = row.createCell(7);   
          cell.setCellValue(new HSSFRichTextString(w.getWremark()));   
          cell.setCellStyle(style);   
        }   
        k = k + workList.size();   
      }   
      // 列尾   
      int footRownumber = sheet.getLastRowNum();   
      HSSFRow footRow = sheet.createRow(footRownumber + 1);   
      HSSFCell footRowcell = footRow.createCell(0);   
      footRowcell.setCellValue(new HSSFRichTextString("                    审  定:XXX      审  核:XXX     汇  总:XX"));   
      footRowcell.setCellStyle(centerstyle);   
      range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);   
      sheet.addMergedRegion(range);   
  
      HttpServletResponse response = getResponse();   
      HttpServletRequest request = getRequest();   
      String filename = "未命名.xls";//设置下载时客户端Excel的名称   
      // 请见:http://zmx.javaeye.com/blog/622529   
      filename = Util.encodeFilename(filename, request);   
      response.setContentType("application/vnd.ms-excel");   
      response.setHeader("Content-disposition", "attachment;filename=" + filename);   
      OutputStream ouputStream = response.getOutputStream();   
      workbook.write(ouputStream);   
      ouputStream.flush();   
      ouputStream.close();   
  
    } catch (Exception e) {   
      e.printStackTrace();   
    }   
    return null;   
  }  

实例二 :

 String fileName="EXCEL.xls";

 response.reset();//清除buffer。设置页面不缓存

 response.setContentType( "application/vnd.ms-excel;charset=GBK");

 response.setHeader("Content-disposition","attachment; filename=\"" + fileName + "\"");

 request.setCharacterEncoding("GBK");

 

 //银行承兑汇票

 Bill[] bankBills = (Bill[])request.getAttribute("bankBills");

 //商业承兑汇票

 Bill[]commercialBills = (Bill[])request.getAttribute("commercialBills");

 

 HSSFWorkbook  wb = new HSSFWorkbook();

 HSSFCellStyle style = wb.createCellStyle();

 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

 SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

 

 HSSFSheet banksheet = wb.createSheet();

 wb.setSheetName(0,"银行承兑汇票",HSSFWorkbook.ENCODING_UTF_16);

 HSSFRow bankrow0 = banksheet.createRow((int) 0);

 //设置表头

 HSSFCell bankcell0 = bankrow0.createCell((short) 0);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("票据流水号");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 1);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("到期日期");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 2);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("当前持有人");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 3);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("票据号码");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 4);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("票面金额");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 5);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("出票人");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 6);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("承兑人");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 7);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("收票人");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 8);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("出票保证人");

 bankcell0.setCellStyle(style);

 bankcell0 = bankrow0.createCell((short) 9);

 bankcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 bankcell0.setCellValue("出票日期");

 bankcell0.setCellStyle(style);

 

 //填充数据

 if(bankBills!=null){

  //填充数据

  HSSFRow row;

  for(int i=0;i<bankBills.length;i++){

   row = banksheet.createRow((int) i + 1);

   Bill bill = bankBills[i];

   //日期格式

   HSSFCellStyle datecellStyle = wb.createCellStyle();

            HSSFDataFormat dateformat= wb.createDataFormat();

            datecellStyle.setDataFormat(dateformat.getFormat("yyyy-MM-dd"));

            //金额格式

            HSSFCellStyle cellStyle = wb.createCellStyle();

            HSSFDataFormat format= wb.createDataFormat();

         cellStyle.setDataFormat(format.getFormat("#,##0.00"));

           

   //票据流水

   HSSFCell cell = row.createCell((short) 0);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   cell.setCellValue(bill.getBillId().toString());

   //票据到期日

   cell= row.createCell((short)1);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellStyle(datecellStyle);

   cell.setCellValue(bill.getDueDate());

   //当前持有人

   String memberName = bankBills[i].getHolderBankAccount()==null?"":

    bankBills[i].getHolderBankAccount().getMember()==null?"":

     bankBills[i].getHolderBankAccount().getMember().getMemberName();

   cell= row.createCell((short)2);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellValue(memberName);

   //票据号

   cell= row.createCell((short)3);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   cell.setCellValue(bill.getBillNo());

   //票面金额

    cell= row.createCell((short)4);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

    cell.setCellStyle(cellStyle);

    cell.setCellValue(bill.getAmount().doubleValue());

   //出票人

    cell= row.createCell((short)5);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

    cell.setCellValue(bill.getDrawerName());

   //承兑人

    cell= row.createCell((short)6);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

    cell.setCellValue(bill.getAcceptorName());

   //收票人

    cell= row.createCell((short)7);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

    cell.setCellValue(bill.getPayeeName());

   //出票保证人

   Set suretySet = bankBills[i].getSureties();

   Iterator it = suretySet.iterator();

   String warr = "";

   while(it.hasNext()){

    Surety surety = (Surety)it.next();

    if(surety.getSuretyMode().intValue() == Integer.valueOf(SureMode.register.getCode()).intValue()){

     warr=surety.getWarrantorName();

    }

   cell= row.createCell((short)8);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   cell.setCellValue(warr);

   //出票日期

   cell= row.createCell((short)9);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

            cell.setCellStyle(datecellStyle);

   cell.setCellValue(bill.getIssueDate());

  }

   }

 }

 

 // 填充商业承兑汇票数据

 

 HSSFSheet commercialsheet = wb.createSheet();

 wb.setSheetName(1,"商业承兑汇票",HSSFWorkbook.ENCODING_UTF_16);

 HSSFRow commercialrow0 = commercialsheet.createRow((int) 0);

 //设置表头

 HSSFCell commercialcell0 = commercialrow0.createCell((short) 0);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("票据流水号");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 1);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("到期日期");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 2);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("当前持有人");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 3);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("票据号码");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 4);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("票面金额");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 5);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("出票人");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 6);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("承兑人");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 7);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("收票人");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 8);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("出票保证人");

 commercialcell0.setCellStyle(style);

 commercialcell0 = commercialrow0.createCell((short) 9);

 commercialcell0.setEncoding(HSSFCell.ENCODING_UTF_16);

 commercialcell0.setCellValue("出票日期");

 commercialcell0.setCellStyle(style);

 //填充数据

 if(commercialBills!=null){

  //填充数据

  HSSFRow row;

  for(int i=0;i<commercialBills.length;i++){

   row = commercialsheet.createRow((int) i + 1);

   Bill bill = commercialBills[i];

   //日期格式

   HSSFCellStyle datecellStyle = wb.createCellStyle();

            HSSFDataFormat dateformat= wb.createDataFormat();

            datecellStyle.setDataFormat(dateformat.getFormat("yyyy-MM-dd"));

            //金额格式

            HSSFCellStyle cellStyle = wb.createCellStyle();

            HSSFDataFormat format= wb.createDataFormat();

         cellStyle.setDataFormat(format.getFormat("#,##0.00"));

   //票据流水

   HSSFCell cell = row.createCell((short) 0);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   cell.setCellValue(bill.getBillId().toString());

   //票据到期日

   cell= row.createCell((short)1);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellStyle(datecellStyle);

   cell.setCellValue(df.format(bill.getDueDate()));

   //当前持有人

   String memberName = bankBills[i].getHolderBankAccount()==null?"":

    bankBills[i].getHolderBankAccount().getMember()==null?"":

     bankBills[i].getHolderBankAccount().getMember().getMemberName();

   cell= row.createCell((short)2);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellValue(memberName);

   //票据号

   cell= row.createCell((short)3);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   cell.setCellValue(bill.getBillNo());

   //票面金额

    cell= row.createCell((short)4);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

    cell.setCellStyle(cellStyle);

    cell.setCellValue(bill.getAmount().doubleValue());

   //出票人

    cell= row.createCell((short)5);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

    cell.setCellValue(bill.getDrawerName());

   //承兑人

    cell= row.createCell((short)6);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

    cell.setCellValue(bill.getAcceptorName());

   //收票人

    cell= row.createCell((short)7);

    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

    cell.setCellType(HSSFCell.CELL_TYPE_STRING);

    cell.setCellValue(bill.getPayeeName());

   //出票保证人

   Set suretySet = bankBills[i].getSureties();

   Iterator it = suretySet.iterator();

   String warr = "";

   while(it.hasNext()){

    Surety surety = (Surety)it.next();

    if(surety.getSuretyMode().intValue() == Integer.valueOf(SureMode.register.getCode()).intValue()){

     warr=surety.getWarrantorName();

    }

   cell= row.createCell((short)8);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellType(HSSFCell.CELL_TYPE_STRING);

   cell.setCellValue(warr);

   //出票日期

   cell= row.createCell((short)9);

   cell.setEncoding(HSSFCell.ENCODING_UTF_16);

   cell.setCellStyle(datecellStyle);

   cell.setCellValue(df.format(bill.getIssueDate()));

  }

   }

 }

 OutputStream os=response.getOutputStream();

    try{

  wb.write(os);

 }catch(Exception e){

  e.printStackTrace();

 }finally{

  try {

   os.flush();

   os.close();

   os=null;

   response.flushBuffer();

   out.clear();

   out = pageContext.pushBody();

   }catch (IOException e) {

     e.printStackTrace();

   }

 }

实例三:

    FileInputStream fis = null;
        OutputStream myout = null;
        try{
             //得到要下载的文件名称
            String filename="zhangsan.txt";
            //文件存放的路径,合成绝对路径
//            String dir = this.getServletContext().getRealPath("/");
            String filepath="D:/works/test/src/test/test.java";
            //得到这个文件的对象
            File file =new File(filepath);
            response.reset();
            //response的编码方式为.doc下载
            response.setContentType("application/text;charset=utf-8");
            //写明要下载的文件的大小
            response.setContentLength((int)file.length());
            //文件名
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            //独处文件的IO流
            fis = new FileInputStream(file);
//            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            BufferedInputStream buff=new BufferedInputStream(fis);
            byte [] b = new byte[1024];//相当读文件的缓存
            long k=0;//该值用于计算当前实际下载了多少字节
//            int flag = 0;
//            //response对象得到输出流
            myout = response.getOutputStream();
//            
//            while( (flag = fis.read(b)) > 0 ){
//                myout.write(b);
//            }

            //开始循环下载

            while(k<file.length()){

                int j=buff.read(b,0,1024);
                k+=j;

                //将b中的数据写到客户端的内存
                myout.write(b,0,j);

            }

            //将写入到客户端的内存的数据,刷新到磁盘
            myout.flush();


        }catch(Exception e){

        }finally{
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(myout != null){
                try {
                    myout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


             try {
                response.flushBuffer();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

实例四

--------------------------------------------------------------------------------------------------            
         String fileName = "d:/qqq.txt";
BufferedReader br = null;
BufferedWriter bw = null;
try {
     String loadFileName = "\"" + escapeURL("zhangsan.txt") + "\"";;
     //重置输出流
     response.reset();
     //设置下载框
     response.setHeader("Content-disposition", "attachment; filename=" + loadFileName);
     //设置文件类型
     response.setContentType("text/plain;charset=UTF-8");
     
     br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
     bw = new BufferedWriter(new OutputStreamWriter(response.getOutputStream())) ;
     String lineStr = null;
     while((lineStr = br.readLine()) != null){
         bw.write(lineStr);
     }
     bw.flush();
 } catch (FileNotFoundException e) {
     e.printStackTrace();
 } catch (IOException e) {
     e.printStackTrace();
 }finally{
     if(br != null){
         try {
             br.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     if(bw != null){
         try {
             bw.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
     response.flushBuffer();
 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏微信公众号:Java团长

Java POI 导出EXCEL经典实现

在web开发中,有一个经典的功能,就是数据的导入导出。特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作。而数据导出...

892
来自专栏码匠的流水账

java9系列(六)HTTP/2 Client (Incubator)

本文主要研究下JEP 110: HTTP/2 Client (Incubator)

451
来自专栏杂烩

本地eclipse下开发storm的Topology

这个Topology的功能是从mysql数据库读取数据,然后将数据写入到本地文件里

783
来自专栏张善友的专栏

MSBUILD 命令行编译的时候请注意msbuild文件名称或路经中空格导致出错

在使用MSBUILD 去编译msbuild文件的时候,如果这个方案或者项目的名称或者路经中间有空格符号,需要把这个方案或者项目整个用引号引起来,否则编译的时候会...

1825
来自专栏XAI

Excel导出工具类.

 Excel导出工具类.--POI import java.io.OutputStream; import java.lang.reflect.Field; i...

18310
来自专栏杂烩

storm togolopy转换jstorm topology

本文参照https://my.oschina.net/shyloveliyi/blog/785812中代码,进行转换。

572
来自专栏Pythonista

Golang之http编程

793
来自专栏冷冷

MongoDB 整合spring-data-jpa,spring 4.x

Spring Data提供了repository 抽象方式,可以极大的减少数据访问层千篇一律的类似的重复的代码。 基本DAO都会实现,find,findAll,...

52510
来自专栏码匠的流水账

聊聊storm的submitTopology

storm-core-1.1.0-sources.jar!/org/apache/storm/StormSubmitter.java

655
来自专栏我和未来有约会

xml-rpc(2)-first demo_v2

[XmlRpcMethod("blogger.getUsersBlogs", Description = "获取博客信息")]         public B...

1845

扫码关注云+社区