Java中Excel的导入导出

来源:互联网 发布:jq 将数组导出excel 编辑:程序博客网 时间:2024/05/01 14:57

首先需要一个jxl.jar包,没有的话可以从网上下载一个

 

 

 

一. Excel导入

   

    先在前台页面定义一个获取Excel文件路径的Input

 

    <input type="file" name="excel" title="Excel文件" />

 

    然后把这个路径传到后台类中

 

    String excel=request.getParameter("excel");
    Workbook wb = null;    
    try {

    
        //构造Workbook(工作薄)对象    
        wb=Workbook.getWorkbook(new File(excel)); 

   
    } catch (BiffException e) {    
        e.printStackTrace();    
    } catch (IOException e) {    
        e.printStackTrace();    
    }    
        
    if(wb==null)    
        return null;    
        
    //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了    
    Sheet[] sheet = wb.getSheets();    
        
    if(sheet!=null&&sheet.length>0){    
        //对每个工作表进行循环    
        for(int i=0;i 
            //得到当前工作表的行数    
            int rowNum = sheet[i].getRows();    
            for(int j=0;j 
                //得到当前行的所有单元格    
                Cell[] cells = sheet[i].getRow(j);    
                if(cells!=null&&cells.length>0){    
                    //对每个单元格进行循环    
                    for(int k=0;k 
                        //读取当前单元格的值    
                        String cellValue = cells[k].getContents();    
                    }    
                }    
            }    
        }    
    }    
    //最后关闭资源,释放内存    
    wb.close();    
}

 

 

 

二. Excel导出

 

    Excel导出在前台页面只需要添加一个到后台类的链接

 

   

    //response.reset();  如果前面使用过response则需要reset一下


    OutputStream os4 = response.getOutputStream();

 

    response.setContentType("application/msexcel"); 
    response.addHeader("Content-Disposition","attachment; filename="+ new String("旅游团派工单.xls".getBytes("GBK"), "iso-8859-1"));

 

    WritableWorkbook wwb = null;    
    try {

    
            //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象    
            wwb = Workbook.createWorkbook(os4); 

   
    } catch (IOException e) {    
            e.printStackTrace();    
    }

    
    if(wwb!=null){    
            //创建一个可写入的工作表    
            //Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置   
            WritableSheet ws = wwb.createSheet("sheet1", 0);    
                
            //下面开始添加单元格    
            for(int i=0;i<10;i++){    
                for(int j=0;j<5;j++){ 

   
                    //这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行    
                    Label labelC = new Label(j, i, "这是第"+(i+1)+"行,第"+(j+1)+"列"); 

   
                    try {    
                        //将生成的单元格添加到工作表中    
                        ws.addCell(labelC);    
                    } catch (RowsExceededException e) {    
                        e.printStackTrace();    
                    } catch (WriteException e) {    
                        e.printStackTrace();    
                    }    
   
                }    
            }    
   
            try {    
                //从内存中写入文件中    
                wwb.write();    
                //关闭资源,释放内存    
                wwb.close();

                os4.flush(); 
                os4.close();    
            } catch (IOException e) {    
                e.printStackTrace();    
            } catch (WriteException e) {    
                e.printStackTrace();    
            }    
        }   

0 0
原创粉丝点击