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);
  • 相关阅读:
    c#之继承
    javascript post数据到WCF反馈json或者XML
    五:理解控件的运行机制(例:基于Control命名空间的简单控件)
    c#之委托和事件
    项目管理中的十一个原则
    Linq:基本语法group by, order by ,into(3)
    Linq:基本语法form ,select, where(2)
    PL/SQL学习笔记视图
    七:理解控件的运行机制(例:基于CompositeControl命名空间的控件)
    PL/SQL学习笔记常量变量及数据类型初步
  • 原文地址:https://www.cnblogs.com/shihx/p/12109125.html
Copyright © 2011-2022 走看看