• 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);
  • 相关阅读:
    【转】由浅入深表达式树(一)创建表达式
    【转】背后的故事之
    【转】背后的故事之
    【转】C#集合类型大盘点
    【转】最近用Timer踩了一个坑,分享一下避免别人继续踩
    【转】《深入理解C# 3.x的新特性》博文系列汇总
    【转】文件各种上传,离不开的表单
    【转】文件下载之断点续传(客户端与服务端的实现)
    【转】权限管理学习 一、ASP.NET Forms身份认证
    【转】C#单元测试,带你快速入门
  • 原文地址:https://www.cnblogs.com/shihx/p/12109125.html
Copyright © 2020-2023  润新知