• 从数据库中取出数据表,导入并生成excel


    @RequestMapping("/numericalStatement1")
        public void createExcel(HttpServletResponse resp) throws Exception{
            try {
              String path = "G:/test.xlsx";
              // 创建新的Excel 工作簿
              XSSFWorkbook workbook = new XSSFWorkbook();
              // 在Excel工作簿中建一工作表,其名为缺省值
              // 如要新建一名为"用户表"的工作表,其语句为:
              XSSFSheet sheet = workbook.createSheet("成绩表");
              // 在索引0的位置创建行(最顶端的行)
              XSSFRow row = sheet.createRow((short) 0);
              //在索引0的位置创建单元格(左上端)
              XSSFCell cell = row.createCell((short) 0);
              //创建单元格样式
              CellStyle cellStyle = workbook.createCellStyle();
              // 设置这些样式
              cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
              cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
              cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
              cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
              cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
              cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
              cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    
              // 定义单元格为字符串类型
              cell.setCellType(HSSFCell.CELL_TYPE_STRING);
              // 在单元格中输入一些内容
              cell = row.createCell((short) 0);
              cell.setCellValue("成绩编号");
              cell.setCellStyle(cellStyle);
    
              cell = row.createCell((short) 1);
              cell.setCellValue("组织架构参数表编号");
              cell.setCellStyle(cellStyle);
    
              cell = row.createCell((short) 2);
              cell.setCellValue("试卷编号");
              cell.setCellStyle(cellStyle);
    
              cell = row.createCell((short) 3);
              cell.setCellValue("客观题成绩");
              cell.setCellStyle(cellStyle);
    
              cell = row.createCell((short) 4);
              cell.setCellValue("主观题成绩");
              cell.setCellStyle(cellStyle);
              
              cell = row.createCell((short) 5);
              cell.setCellValue("总成绩");
              cell.setCellStyle(cellStyle);
    
              //查询数据库中所有的数据
    //          ResultMapper mapper = getMapper(ResultMapper.class);
    //          VtUserCriteria cri = new VtUserCriteria();
    //          cri.createCriteria().andUserEnabledEqualTo(1);
            
              List<Result> list = resultService.selectAllResult();
              /*//第一个sheet第一行为标题
              XSSFRow rowFirst = sheet.createRow(0);
              rowFirst.setHeightInPoints(21.75f);*/
              for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow((int) i + 1);
                Result stu = (Result) list.get(i);
                // 第四步,创建单元格,并设置值
                row.createCell((short) 0).setCellValue(stu.getId());
                row.createCell((short) 1).setCellValue(stu.getParaorgleadershipsId());
                row.createCell((short) 2).setCellValue(stu.getPaperId());
                row.createCell((short) 3).setCellValue(stu.getObjResult());
                row.createCell((short) 4).setCellValue(stu.getSubResult());
                row.createCell((short) 5).setCellValue(stu.getTotalResult());
                sheet.autoSizeColumn((short) 0); //调整第一列宽度(自适应),只识别数字、字母
                sheet.autoSizeColumn((short) 1); //调整第二列宽度
                //调整第三列宽度,有中文,先判断这一列的最长字符串
              //  int length = stu.getPaperId().getBytes().length;
             //   sheet.setColumnWidth((short)2,(short)(length*2*256));
                sheet.autoSizeColumn((short) 2); //调整第三列宽度
                sheet.autoSizeColumn((short) 3); //调整第四列宽度
                sheet.autoSizeColumn((short) 4); //调整第五列宽度
                sheet.autoSizeColumn((short) 5); //调整第六列宽度
                /*Font font = workbook.createFont();
                font.setFontHeightInPoints((short)18); //字体大小
                sheet.setDefaultRowHeightInPoints(21.75f);
                font.setFontName("楷体");
                font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体
                font.setColor(HSSFColor.GREEN.index);    //绿字- 字体颜色*/
              }
              // 新建一输出文件流
              FileOutputStream fOut = new FileOutputStream(path);
              // 把相应的Excel 工作簿存盘
              workbook.write(fOut);
              //清空缓冲区数据
              fOut.flush();
              // 操作结束,关闭文件
              fOut.close();
              System.out.println("文件生成...");
            } catch (Exception e) {
              System.out.println("已运行 xlCreate() : " + e);
            }
          }

    代码引自https://www.cnblogs.com/zhxn/p/7016380.html

  • 相关阅读:
    .NET异常处理最佳实践
    Resharper4.5破解程序下载
    Firefox报“使用了无效的安全证书”错误的解决方法
    jQuery培训PPT
    Windows常用命令集即开始→运行→输入的命令集锦
    “NHibernate.Cfg.Environment的类型初始值设定项引发异常”的解决方法
    浅析SQL having子句、如何使用having子句及where子句与having子句的区别
    浅析SQL中 in 与 exists 用法的区别及其各自执行流程、not in/not exists区别、sql优化应该如何选择in还是exists
    浅析SQL优化查询性能的最佳实践
    SQL中的cast和convert的用法和区别以及时间转换
  • 原文地址:https://www.cnblogs.com/minxiaofei/p/10081073.html
Copyright © 2020-2023  润新知