zoukankan      html  css  js  c++  java
  • 页面下载Excel POI操作Excel

    // 从数据库中提取数据保存到Excel中
        public void downExcel(HttpServletResponse response, List fwmxList,
                String fileName) throws Exception {
    
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("用户访问明细");
    
            // 设置公式自动计算
            // sheet.setForceFormulaRecalculation(true);
    
            // 设置单元格下边框
            HSSFCellStyle style1 = workbook.createCellStyle();
            style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    
            // 设置单元格上边框
            HSSFCellStyle style2 = workbook.createCellStyle();
            style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
    
            // 设置单元格宽度
            sheet.setColumnWidth(0, 4000);
            sheet.setColumnWidth(1, 3000);
    
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue("访问时间");
            cell.setCellStyle(style1);
            cell = row.createCell(1);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue("访问人数");
            cell.setCellStyle(style1);
    
            int size = fwmxList.size() + 1, totalCount = 0;
            for (int i = 1; i < size; i++) {
                row = sheet.createRow(i);
                Map mapRow = (Map) fwmxList.get(i - 1);
                cell = row.createCell(0);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(mapRow.get("TIME").toString());
    
                cell = row.createCell(1);
                cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                int count = Integer.parseInt(mapRow.get("SJ_Z").toString());
                cell.setCellValue(count);
                totalCount += count;
            }
            row = sheet.createRow(size);
    
            cell = row.createCell(0);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue("访问总数");
            cell.setCellStyle(style2);
    
            cell = row.createCell(1);
            cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
            // String strFormula = "sum(B2:B" + size + ")";// 设置求和公式
            // cell.setCellFormula(strFormula);
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(totalCount);
            cell.setCellStyle(style2);
    
            row = sheet.createRow(size + 1);
    
            cell = row.createCell(0);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue("平均访问数");
    
            cell = row.createCell(1);
            // cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
            // strFormula = "rounddown(average(B2:B" + size + "),0)";// 设置求平均值与取整数公式
            // cell.setCellFormula(strFormula);
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
            cell.setCellValue(totalCount / (size - 1));
    
            // 将内容保存到文件fileName中
            OutputStream outputStream = null;
            File file = new File(fileName);
            // System.out.println(file.getAbsolutePath());
            outputStream = new FileOutputStream(fileName);
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
    
            // 下载设置
            response.setHeader("Content-Disposition", "inline;filename="
                    + URLEncoder.encode(fileName, "UTF-8"));
            // response.setHeader("Content-Disposition", "attachment;filename="
            // + URLEncoder.encode(fileName, "UTF-8"));
            // response.setContentType("application/msexcel;charset=UTF-8");//
            // 文件名为中文,设置编码为UTF-8
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
    
            ServletOutputStream servletOutputStream = response.getOutputStream();
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            bis = new BufferedInputStream(new FileInputStream(fileName));
            bos = new BufferedOutputStream(servletOutputStream);
            byte[] buff = new byte[2048];
            int buffLen = -1;
            while ((buffLen = bis.read(buff, 0, buff.length)) != -1) {
                bos.write(buff, 0, buffLen);
            }
            bos.close();
            bis.close();
            file.delete();
        }
  • 相关阅读:
    C#反射概念以及实例详解【转】
    .NET(C#):使用反射来获取枚举的名称、值和特性【转】
    探求C#.Net中ArrayList与Array的区别 【转】
    C#中IList<T>与List<T>的区别感想【转】
    C# System.Guid.NewGuid() 【转】
    回车键触发按钮事件
    MVC中Json的使用:Controller中Json的处理【转】
    关于优化性能<主要是速度方面>的个人心得 【转】
    ca72a_c++_标准IO库:面向对象的标准库
    ca71a_c++_指向函数的指针_通过指针调用函数txwtech
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/3071242.html
Copyright © 2011-2022 走看看