利用jxl操作excel文件

来源:互联网 发布:windows 7系统激活工具 编辑:程序博客网 时间:2024/04/28 19:20
 
一.读取Excel文件内容
java 代码
1/**读取Excel文件的内容 
2.  * 
@param file  待读取的文件 
3.  * 
@return   
4.  
*/   
5public static String readExcel(File file){    
6.     StringBuffer sb = new StringBuffer();    
7.         
8.     Workbook wb = null;    
9.     try {    
10.        //构造Workbook(工作薄)对象    
11.        wb=Workbook.getWorkbook(file);    
12.    } catch (BiffException e) {    
13.        e.printStackTrace();    
14.    } catch (IOException e) {    
15.        e.printStackTrace();    
16.    }    
17.        
18.    if(wb==null)    
19.        return null;    
20.        
21.    //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了    
22.    Sheet[] sheet = wb.getSheets();    
23.        
24.    if(sheet!=null&&sheet.length>0){    
25.        //对每个工作表进行循环    
26.        for(int i=0;i 
27.            //得到当前工作表的行数    
28.            int rowNum = sheet[i].getRows();    
29.            for(int j=0;j 
30.                //得到当前行的所有单元格    
31.                Cell[] cells = sheet[i].getRow(j);    
32.                if(cells!=null&&cells.length>0){    
33.                    //对每个单元格进行循环    
34.                    for(int k=0;k 
35.                        //读取当前单元格的值    
36.                        String cellValue = cells[k].getContents();    
37.                        sb.append(cellValue+" ");    
38.                    }    
39.                }    
40.                sb.append(" ");    
41.            }    
42.            sb.append(" ");    
43.        }    
44.    }    
45.    //最后关闭资源,释放内存    
46.    wb.close();    
47.    return sb.toString();    
48.}  
 
二.写入Excel文件
这里有很多格式了,比如文本内容加粗,加上某些颜色等,可以参考jxl的api,同时还推荐一篇不错的文章:http://www.ibm.com/developerworks/cn/java/l-javaExcel/?ca=j-t10
java 代码
1/**生成一个Excel文件 
2.      * 
@param fileName  要生成的Excel文件名 
3.      
*/   
4.     public static void writeExcel(String fileName){    
5.         WritableWorkbook wwb = null;    
6.         try {    
7.             //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象    
8.             wwb = Workbook.createWorkbook(new File(fileName));    
9.         } catch (IOException e) {    
10.            e.printStackTrace();    
11.        }    
12.        if(wwb!=null){    
13.            //创建一个可写入的工作表    
14.            //Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置    
15.            WritableSheet ws = wwb.createSheet("sheet1"0);    
16.                
17.            //下面开始添加单元格    
18.            for(int i=0;i<10;i++){    
19.                for(int j=0;j<5;j++){    
20.                    //这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行    
21.                    Label labelC = new Label(j, i, "这是第"+(i+1)+"行,第"+(j+1)+"");    
22.                    try {    
23.                        //将生成的单元格添加到工作表中    
24.                        ws.addCell(labelC);    
25.                    } catch (RowsExceededException e) {    
26.                        e.printStackTrace();    
27.                    } catch (WriteException e) {    
28.                        e.printStackTrace();    
29.                    }    
30.   
31.                }    
32.            }    
33.   
34.            try {    
35.                //从内存中写入文件中    
36.                wwb.write();    
37.                //关闭资源,释放内存    
38.                wwb.close();    
39.            } catch (IOException e) {    
40.                e.printStackTrace();    
41.            } catch (WriteException e) {    
42.                e.printStackTrace();    
43.            }    
44.        }    
45.    }    
三.在一个Excel文件中查找是否包含某一个关键字
java 代码
1/**搜索某一个文件中是否包含某个关键字 
2.      * 
@param file  待搜索的文件 
3.      * 
@param keyWord  要搜索的关键字 
4.      * 
@return   
5.      
*/   
6.     public static boolean searchKeyWord(File file,String keyWord){    
7.         boolean res = false;    
8.             
9.         Workbook wb = null;    
10.        try {    
11.            //构造Workbook(工作薄)对象    
12.            wb=Workbook.getWorkbook(file);    
13.        } catch (BiffException e) {    
14.            return res;    
15.        } catch (IOException e) {    
16.            return res;    
17.        }    
18.            
19.        if(wb==null)    
20.            return res;    
21.            
22.        //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了    
23.        Sheet[] sheet = wb.getSheets();    
24.            
25.        boolean breakSheet = false;    
26.            
27.        if(sheet!=null&&sheet.length>0){    
28.            //对每个工作表进行循环    
29.            for(int i=0;i 
30.                if(breakSheet)    
31.                    break;    
32.                    
33.                //得到当前工作表的行数    
34.                int rowNum = sheet[i].getRows();    
35.                    
36.                boolean breakRow = false;    
37.                    
38.                for(int j=0;j 
39.                    if(breakRow)    
40.                        break;    
41.                    //得到当前行的所有单元格    
42.                    Cell[] cells = sheet[i].getRow(j);    
43.                    if(cells!=null&&cells.length>0){    
44.                        boolean breakCell = false;    
45.                        //对每个单元格进行循环    
46.                        for(int k=0;k 
47.                            if(breakCell)    
48.                                break;    
49.                            //读取当前单元格的值    
50.                            String cellValue = cells[k].getContents();    
51.                            if(cellValue==null)    
52.                                continue;    
53.                            if(cellValue.contains(keyWord)){    
54.                                res = true;    
55.                                breakCell = true;    
56.                                breakRow = true;    
57.                                breakSheet = true;    
58.                            }    
59.                        }    
60.                    }    
61.                }    
62.            }    
63.        }    
64.        //最后关闭资源,释放内存    
65.        wb.close();    
66.            
67.        return res;    
68.    }   
四.往Excel中插入图片图标
插入图片的实现很容易,参看以下代码:
java 代码
1/**往Excel中插入图片 
2.  * 
@param dataSheet  待插入的工作表 
3.  * 
@param col 图片从该列开始 
4.  * 
@param row 图片从该行开始 
5.  * 
@param width 图片所占的列数 
6.  * 
@param height 图片所占的行数 
7.  * 
@param imgFile 要插入的图片文件 
8.  
*/   
9public static void insertImg(WritableSheet dataSheet, int col, int row, int width,    
10.        int height, File imgFile){    
11.    WritableImage img = new WritableImage(col, row, width, height, imgFile);    
12.    dataSheet.addImage(img);    
13.}    
以上代码的注释已经很清楚了,大概也就不用再解释了,我们可以用如下程序验证:
java 代码
1.     try {    
2.         //创建一个工作薄    
3. WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls"));    
4//待插入的工作表    
5. WritableSheet imgSheet = workbook.createSheet("Images",0);    
6//要插入的图片文件    
7. File imgFile = new File("D:/1.png");    
8//图片插入到第二行第一个单元格,长宽各占六个单元格    
9. insertImg(imgSheet,0,1,6,6,imgFile);    
10.workbook.write();    
11.workbook.close();    
12catch (IOException e) {    
13.e.printStackTrace();    
14catch (WriteException e) {    
15.e.printStackTrace();    
 但是jxl只支持png格式的图片,jpg格式和gif格式都不支持。
 
原创粉丝点击