java的报表下载代码excel

    /**
     * 汇总报表数据下载
     * */    
    private ModelAndView exportSummaryDatadown(HttpServletRequest request,
            HttpServletResponse response, List<InterfaceCost> interfaceCostListNew) throws Exception {
        //保存到磁盘        
        String time = QDateTime.dateToString(new Date(), "yyyy-MM-dd HH:mi:ss");
        time = time.replaceAll("-", "");
        time =time.replace(":", "");
        time =time.replace(" ", "");
        time = time.substring(2);
        String file_name = time + ".xls";
        String uploadDir = request.getRealPath("/resources") + "\\Interfaceparameter\\";
        OutputStream out  = null;
        try {
            
            File dirPath = new File(uploadDir);                    
            if (!dirPath.exists()) {
                  dirPath.mkdirs();
              }
                
             out = new FileOutputStream(uploadDir+file_name);
            
             // 设置第一行(表头的格式)
            jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc); // 表格样式
            wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 水平居中
            wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 垂直居中
            wcfFC.setBorder(Border.ALL, BorderLineStyle.THIN);// 线条
            WritableWorkbook wb = Workbook.createWorkbook(out);// 写入流中
            WritableSheet ws = wb.createSheet("sheet1", 1);
            
            int[] geshi = {30,20,20} ;// 获取列宽
            for (int i = 0; i < geshi.length; i++) {
                ws.setColumnView(i, geshi[i]);
            }
          
        //    int ioNor = 0; // 订单列记录值标识
            //数据总计            
            jxl.write.Label name = new jxl.write.Label(0, 0, "接口名称", wcfFC);
            ws.addCell(name);
            jxl.write.Label count = new jxl.write.Label(1, 0, "使用次数", wcfFC);
            ws.addCell(count);
            jxl.write.Label amount = new jxl.write.Label(2, 0, "消费点数", wcfFC);
            ws.addCell(amount);
                
            // 显示第二行以后的数据
            // 设置第二行以后数据的格式
            wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
            wcfFC = new WritableCellFormat(wfc);
            wcfFC.setAlignment(jxl.format.Alignment.CENTRE);// 水平居中
            wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 垂直居中
            wcfFC.setBorder(Border.ALL, BorderLineStyle.THIN);// 线条
            
            int ioNor = 0; // 订单列记录值标识
            Integer addUpTo = 0;
            for(InterfaceCost interfaceCost:interfaceCostListNew){
                jxl.write.Label optType = new jxl.write.Label(0, ioNor + 1, interfaceCost.getInterfaceName(), wcfFC);
                ws.addCell(optType);
                
                jxl.write.Number totlecount = new jxl.write.Number(1, ioNor + 1, interfaceCost.getTotleCount(), wcfFC);
                ws.addCell(totlecount);
                
                jxl.write.Number totle = new jxl.write.Number(2, ioNor + 1, interfaceCost.getTotleCost(), wcfFC);
                ws.addCell(totle);
                
                addUpTo += interfaceCost.getTotleCost();
                
                ioNor++;
                //ws.mergeCells(0, ioNor, 1, ioNor);//合并单元格
            }
            
            ioNor=ioNor+1;
            jxl.write.Label numberName = new jxl.write.Label(0, ioNor, "合计消费 "+addUpTo.toString()+" 点", wcfFC);
            ws.addCell(numberName);
            
            ws.mergeCells(0, ioNor, 2, ioNor);//合并单元格
            
            wb.write();
            wb.close();
//            out.close();
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally{
            if(out!=null){
                out.close();
            }
        }
    
        //从磁盘读取
        InputStream inStream = null;
        OutputStream outs = null;
         try {        
             response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment; filename="
                    + file_name + "");    
            
                 inStream = new FileInputStream(uploadDir+file_name);
//                String lineTxt = null;
                 outs = response.getOutputStream();
                byte[] buf = new byte[4096];
                int readLength;
                while (((readLength = inStream.read(buf)) != -1)) {
                    outs.write(buf, 0, readLength);
                }
//                inStream.close();
                outs.flush();
//                outs.close();
         } catch (Exception e) {
             log.error("读取文件内容出错");    
             e.printStackTrace();
         }finally{
             if(out!=null){
                out.close();
             }
             if(inStream!=null){
                 inStream.close();
             }
         }
        
        return null;
    }

 

转载于:https://www.cnblogs.com/jinzhiming/p/4826852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是生成java代码导入excel表格的示例代码: ``` import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImporter { public static void main(String[] args) throws IOException { // 读取Excel文件 File file = new File("data.xlsx"); FileInputStream inputStream = new FileInputStream(file); Workbook workbook = new XSSFWorkbook(inputStream); // 选择第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 遍历每一行 Iterator<Row> iterator = sheet.iterator(); while (iterator.hasNext()) { Row currentRow = iterator.next(); // 读取每个单元格的值 Iterator<Cell> cellIterator = currentRow.iterator(); ArrayList<String> rowData = new ArrayList<>(); while (cellIterator.hasNext()) { Cell currentCell = cellIterator.next(); if (currentCell.getCellType() == CellType.STRING) { rowData.add(currentCell.getStringCellValue()); } else if (currentCell.getCellType() == CellType.NUMERIC) { rowData.add(String.valueOf(currentCell.getNumericCellValue())); } } // 在这里你可以将rowData插入到数据库中,或者进行其他操作 System.out.println(rowData); } // 关闭文件输入流 inputStream.close(); } } ``` 这个示例代码使用了Apache POI库来读取Excel文件,并使用ArrayList来存储每一行的数据。在处理完每一行数据后,你可以将rowData插入到数据库中,或者进行其他操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值