zoukankan      html  css  js  c++  java
  • java 导出表格 poi的使用

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("excel表格");
    HSSFRow rowm = sheet.createRow(0);
    HSSFCell cellTitle = rowm.createCell(0);
    HSSFFont font = workbook.createFont();

    // 设置字体大小
    font.setFontHeightInPoints((short) 12);
    // 字体加粗
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    // 设置字体名字
    font.setFontName("宋体");
    // 设置样式
    HSSFCellStyle style = workbook.createCellStyle();
    // 设置低边框
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    // 设置低边框颜色
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    // 设置右边框
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    // 设置顶边框
    style.setTopBorderColor(HSSFColor.BLACK.index);
    // 设置顶边框颜色
    style.setTopBorderColor(HSSFColor.BLACK.index);
    // 在样式中应用设置的字体
    style.setFont(font);
    // 设置自动换行
    style.setWrapText(false);
    // 设置水平对齐的样式为居中对齐;
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

    /**
    * 发送响应流方法
    * @param response
    * @param fileName
    */
    public void setResponseHeader(HttpServletResponse response, String fileName) {
    try {
    try {
    fileName = new String(fileName.getBytes(), "ISO8859-1");
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    response.setContentType("application/octet-stream");
    //这后面可以设置导出Excel的名称,此例中名为student.xls
    response.setHeader("Content-disposition", "attachment;filename=" + fileName);
    response.flushBuffer();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }


    //响应到客户端
    try {
    // response.setContentType("application/x-download");
    // response.setHeader("content-type", "application/octet-stream");
    // response.setContentType("application/octet-stream");
    exp.setResponseHeader(response, fileName);
    OutputStream os = response.getOutputStream();
       workbook.write(out);
      os.flush();
    os.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    设置单元个斜线
    HSSFCellStyle style = exp.getStyle(workbook);
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    sheet.setColumnWidth(0,22*256);
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    //前四个参数代表自动布满单元格,后四个参数代表第1列第3行
    HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255, (short)0, 2, (short)0, 2);
    HSSFSimpleShape shape1 = patriarch.createSimpleShape(a);
    shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
    shape1.setLineStyle(HSSFSimpleShape.LINESTYLE_SOLID);
    hssfCell.setCellValue(value);
    sheet.createFreezePane(1,3,1,3);
  • 相关阅读:
    struts2文件下载的编写步骤(文件导出)和输入流转换的方法
    spring的applicationContext.xml配置SessionFactory抛异常
    引用第三方高德地图接口---使用js脚本进行开发地图定位的步骤
    登陆时不同浏览器获取session存在的相关疑问?
    统一的异常处理和自定义的全局异常处理器的配置使用
    国际化的工具类ognl utils
    oracle中decode的用法(例子)
    PLSQL连接本机oracle 11g 64 数据库的步骤
    处理oracle 报ORA-12505 信息:listener does not currently know of SID given in connect descriptor...
    spring容器的配置和springmvc的配置
  • 原文地址:https://www.cnblogs.com/shihx/p/12109125.html
Copyright © 2011-2022 走看看