zoukankan      html  css  js  c++  java
  • Java中excel生成

      public String toExcel( HttpServletResponse res){

    ArrayList<String> arrayList1 = new ArrayList<String>();
    arrayList1.add("管道段落名称");
    arrayList1.add("(若未填写将根据A-Z端自动生");
    arrayList1.add("成)");
    String content1 = String.join(" ", arrayList1);

    String []header=new String[]{"序号","专业类型","项目编号","微观规划工单号","地市","区域",content1,
    "管道A端","管道Z端","管道级别","管道路由点坐标","需求来源","紧急程度","状态","长度(米)","单价(万元)","概算价格(万元)","预算价格(万元)","完工价格(万元)","需求联系人及电话","备注"};

    String []examples=new String[]{"1","家宽预覆盖业务 接入","12","","东莞","东城区","12312323","XXX11323","XXX","干线","113.51434769,22.28574978, " +
    "113.51451901,22.28576881","业务类","特急","需求","10.000","","","","","",""};
    // 创建HSSFWorkbook对象(excel的文档对象)
    HSSFWorkbook wb = new HSSFWorkbook();
    // 建立新的sheet对象(excel的表单)
    HSSFSheet sheet = wb.createSheet("student");

    sheet.setDefaultRowHeight((short) (34.5*17));
    // 设置表头表体样式
    // 设置表头居中
    HSSFCellStyle style = wb.createCellStyle();
    // 创建一个居中格式
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    // 设置字体颜色
    Font font2 = wb.createFont();
    font2.setFontHeightInPoints((short) 22); // 字体高度
    font2.setFontName("黑体"); // 字体
    font2.setColor(HSSFColor.BLACK.index); //颜色
    style.setFont(font2);
    // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
    HSSFRow row1 = sheet.createRow(0);
    row1.setHeight((short) (49.5*15));
    // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
    HSSFCell cell = row1.createCell(0);
    // 设置单元格内容
    cell.setCellValue("管道类清单明细(红色字体为必填项)");
    cell.setCellStyle(style);
    // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 20));


    // 表格第二行
    HSSFRow row= sheet.createRow((int) 1);
    row.setHeight((short) (34.5*17));
    sheet.autoSizeColumn(1);
    HSSFCellStyle headerStyle1 = wb.createCellStyle();
    HSSFCellStyle headerStyle2 = wb.createCellStyle();
    headerStyle2.setWrapText(true);
    // 创建一个居中格式
    headerStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    headerStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    // 创建一个居中格式
    headerStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    headerStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    // 设置字体颜色 红色
    Font headerFont1 = wb.createFont();
    headerFont1.setFontHeightInPoints((short) 12); // 字体高度
    headerFont1.setFontName("仿宋_GB2312"); // 字体
    headerFont1.setColor(HSSFColor.RED.index); //颜色
    headerStyle1.setFont(headerFont1);
    // 设置字体颜色 黑色
    Font headerFont2 = wb.createFont();
    headerFont2.setFontHeightInPoints((short) 12); // 字体高度
    headerFont2.setFontName("仿宋_GB2312"); // 字体
    headerFont2.setColor(HSSFColor.BLACK.index); //颜色
    headerStyle2.setFont(headerFont2);
    // 添加excel title
    HSSFCell titleCell = null;
    for (int i = 0; i < header.length; i++) {
    titleCell = row.createCell((short) i);
    titleCell.setCellValue(header[i]);
    if(i==10){
    sheet.setColumnWidth(i, header[i].getBytes().length*2*265);
    titleCell.setCellStyle(headerStyle2);
    }else{
    sheet.setColumnWidth(i, header[i].getBytes().length*2*200);
    if(i==0||i==1||i==2||i==4||i==5||i==7||i==8||i==9||i==11||i==12||i==13||i==14){
    titleCell.setCellStyle(headerStyle1);
    }else{
    titleCell.setCellStyle(headerStyle2);
    }
    }
    }

    // 第三行
    HSSFRow row3= sheet.createRow((int) 2);
    row3.setHeight((short) (34.5*17));
    HSSFCell examplesCell = null;
    for (int i = 0; i < examples.length; i++) {
    examplesCell = row3.createCell((short) i);
    examplesCell.setCellValue(examples[i]);
    examplesCell.setCellStyle(headerStyle2);
    }

    String []str6=new String[]{"家宽预覆盖业务接入","干线","业务类"};
    // 第六行
    HSSFRow row7=sheet.createRow(6);
    row7.setHeight((short) (34.5*17));
    HSSFCell str6Cell = null;
    for (int i = 0; i < 23; i++) {
    str6Cell = row7.createCell((short) i);
    str6Cell.setCellValue("");
    }
    for (int i = 23; i < 26; i++) {
    str6Cell = row7.createCell((short) i);
    str6Cell.setCellValue(str6[i-23]);
    str6Cell.setCellStyle(headerStyle2);
    }

    String []str7=new String[]{"企业宽带预覆盖业务接入","汇聚层","迁改类"};
    // 第七行
    HSSFRow row8=sheet.createRow(7);
    row8.setHeight((short) (34.5*17));
    HSSFCell str7Cell = null;
    for (int i = 0; i < 23; i++) {
    str7Cell = row8.createCell((short) i);
    str7Cell.setCellValue("");
    }
    for (int i = 23; i < 26; i++) {
    str7Cell = row8.createCell((short) i);
    str7Cell.setCellValue(str7[i-23]);
    str7Cell.setCellStyle(headerStyle2);
    }

    // // 在sheet里创建第三行
    // HSSFRow row3 = sheet.createRow(2);
    // row3.createCell(0).setCellValue("花花");
    // row3.createCell(1).setCellValue("一年级一班");
    // row3.setHeight((short) (34.5*15));

    //将excel的数据写入文件
    ByteArrayOutputStream fos = null;
    byte[] retArr = null;
    try {
    fos = new ByteArrayOutputStream();
    wb.write(fos);
    retArr = fos.toByteArray();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    fos.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    String fileName="管道类清单明细模板";
    OutputStream os = null;
    try {
    os = res.getOutputStream();
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    res.reset();
    res.setContentType("application/octet-stream; charset=utf-8");
    try {
    res.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(fileName, "UTF-8")+".xls");//要保存的文件名
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    try {
    os.write(retArr);
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    os.flush();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } finally {
    if (os != null) {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    return "redirect:/index.jsp";
    }
  • 相关阅读:
    Head first javascript(七)
    Python Fundamental for Django
    Head first javascript(六)
    Head first javascript(五)
    Head first javascript(四)
    Head first javascript(三)
    Head first javascript(二)
    Head first javascript(一)
    Sicily 1090. Highways 解题报告
    Python GUI programming(tkinter)
  • 原文地址:https://www.cnblogs.com/Athena-life/p/14048689.html
Copyright © 2011-2022 走看看