• 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);
  • 相关阅读:
    [Java] 编写第一个java程序
    [Java] 环境变量设置
    [ActionScript 3.0] 常用的正则表达式
    [ActionScript 3.0] 正则表达式
    Python学习之==>URL编码解码&if __name__ == '__main__'
    Python学习之==>面向对象编程(一)
    Linux下安装redis-4.0.10
    Linux下编译安装Python-3.6.5
    Python学习之==>发送邮件
    Python学习之==>网络编程
  • 原文地址:https://www.cnblogs.com/shihx/p/12109125.html
Copyright © 2020-2023  润新知