Java操作Excel(读、写、搜索关键字、插入图片)

来源:互联网 发布:人工智能用在社区服务 编辑:程序博客网 时间:2024/06/13 03:54

 

  1. package chb.util;  
  2.  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.  
  6. import jxl.Cell;  
  7. import jxl.Sheet;  
  8. import jxl.Workbook;  
  9. import jxl.read.biff.BiffException;  
  10. import jxl.write.Label;  
  11. import jxl.write.WritableImage;  
  12. import jxl.write.WritableSheet;  
  13. import jxl.write.WritableWorkbook;  
  14. import jxl.write.WriteException;  
  15. import jxl.write.biff.RowsExceededException;  
  16.  
  17. public class ExcelUtils {  
  18.  
  19.     /**读取Excel文件的内容  
  20.      * @param file  待读取的文件  
  21.      * @return  
  22.      */ 
  23.     public static String readExcel(File file){  
  24.         StringBuffer sb = new StringBuffer();  
  25.           
  26.         Workbook wb = null;  
  27.         try {  
  28.             //构造Workbook(工作薄)对象  
  29.             wb=Workbook.getWorkbook(file);  
  30.         } catch (BiffException e) {  
  31.             e.printStackTrace();  
  32.         } catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.           
  36.         if(wb==null)  
  37.             return null;  
  38.           
  39.         //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了  
  40.         Sheet[] sheet = wb.getSheets();  
  41.           
  42.         if(sheet!=null&&sheet.length>0){  
  43.             //对每个工作表进行循环  
  44.             for(int i=0;i<sheet.length;i++){  
  45.                 //得到当前工作表的行数  
  46.                 int rowNum = sheet[i].getRows();  
  47.                 for(int j=0;j<rowNum;j++){  
  48.                     //得到当前行的所有单元格  
  49.                     Cell[] cells = sheet[i].getRow(j);  
  50.                     if(cells!=null&&cells.length>0){  
  51.                         //对每个单元格进行循环  
  52.                         for(int k=0;k<cells.length;k++){  
  53.                             //读取当前单元格的值  
  54.                             String cellValue = cells[k].getContents();  
  55.                             sb.append(cellValue+"\t");  
  56.                         }  
  57.                     }  
  58.                     sb.append("\r\n");  
  59.                 }  
  60.                 sb.append("\r\n");  
  61.             }  
  62.         }  
  63.         //最后关闭资源,释放内存  
  64.         wb.close();  
  65.         System.out.println("313");  
  66.         return sb.toString();  
  67.     }  
  68.     /**生成一个Excel文件  
  69.      * @param fileName  要生成的Excel文件名  
  70.      */ 
  71.     public static void writeExcel(String fileName){  
  72.         WritableWorkbook wwb = null;  
  73.         try {  
  74.             //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象  
  75.             wwb = Workbook.createWorkbook(new File(fileName));  
  76.         } catch (IOException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.         if(wwb!=null){  
  80.             //创建一个可写入的工作表  
  81.             //Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置  
  82.             WritableSheet ws = wwb.createSheet("sheet1"0);  
  83.               
  84.             //下面开始添加单元格  
  85.             for(int i=0;i<10;i++){  
  86.                 for(int j=0;j<5;j++){  
  87.                     //这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行  
  88.                     Label labelC = new Label(j, i, "这是第"+(i+1)+"行,第"+(j+1)+"列");  
  89.                     try {  
  90.                         //将生成的单元格添加到工作表中  
  91.                         ws.addCell(labelC);  
  92.                     } catch (RowsExceededException e) {  
  93.                         e.printStackTrace();  
  94.                     } catch (WriteException e) {  
  95.                         e.printStackTrace();  
  96.                     }  
  97.  
  98.                 }  
  99.             }  
  100.  
  101.             try {  
  102.                 //从内存中写入文件中  
  103.                 wwb.write();  
  104.                 //关闭资源,释放内存  
  105.                 wwb.close();  
  106.             } catch (IOException e) {  
  107.                 e.printStackTrace();  
  108.             } catch (WriteException e) {  
  109.                 e.printStackTrace();  
  110.             }  
  111.         }  
  112.     }   
  113.     /**搜索某一个文件中是否包含某个关键字  
  114.      * @param file  待搜索的文件  
  115.      * @param keyWord  要搜索的关键字  
  116.      * @return  
  117.      */ 
  118.     public static boolean searchKeyWord(File file,String keyWord){  
  119.         boolean res = false;  
  120.           
  121.         Workbook wb = null;  
  122.         try {  
  123.             //构造Workbook(工作薄)对象  
  124.             wb=Workbook.getWorkbook(file);  
  125.         } catch (BiffException e) {  
  126.             return res;  
  127.         } catch (IOException e) {  
  128.             return res;  
  129.         }  
  130.           
  131.         if(wb==null)  
  132.             return res;  
  133.           
  134.         //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了  
  135.         Sheet[] sheet = wb.getSheets();  
  136.           
  137.         boolean breakSheet = false;  
  138.           
  139.         if(sheet!=null&&sheet.length>0){  
  140.             //对每个工作表进行循环  
  141.             for(int i=0;i<sheet.length;i++){  
  142.                 if(breakSheet)  
  143.                     break;  
  144.                   
  145.                 //得到当前工作表的行数  
  146.                 int rowNum = sheet[i].getRows();  
  147.                   
  148.                 boolean breakRow = false;  
  149.                   
  150.                 for(int j=0;j<rowNum;j++){  
  151.                     if(breakRow)  
  152.                         break;  
  153.                     //得到当前行的所有单元格  
  154.                     Cell[] cells = sheet[i].getRow(j);  
  155.                     if(cells!=null&&cells.length>0){  
  156.                         boolean breakCell = false;  
  157.                         //对每个单元格进行循环  
  158.                         for(int k=0;k<cells.length;k++){  
  159.                             if(breakCell)  
  160.                                 break;  
  161.                             //读取当前单元格的值  
  162.                             String cellValue = cells[k].getContents();  
  163.                             if(cellValue==null)  
  164.                                 continue;  
  165.                             if(cellValue.contains(keyWord)){  
  166.                                 res = true;  
  167.                                 breakCell = true;  
  168.                                 breakRow = true;  
  169.                                 breakSheet = true;  
  170.                             }  
  171.                         }  
  172.                     }  
  173.                 }  
  174.             }  
  175.         }  
  176.         //最后关闭资源,释放内存  
  177.         wb.close();  
  178.         System.out.print(res);  
  179.         return res;  
  180.     }  
  181.     /**往Excel中插入图片  
  182.      * @param dataSheet  待插入的工作表  
  183.      * @param col 图片从该列开始  
  184.      * @param row 图片从该行开始  
  185.      * @param width 图片所占的列数  
  186.      * @param height 图片所占的行数  
  187.      * @param imgFile 要插入的图片文件  
  188.      */ 
  189.     public static void insertImg(WritableSheet dataSheet, int col, int row, int width,  
  190.             int height, File imgFile){  
  191.         WritableImage img = new WritableImage(col, row, width, height, imgFile);  
  192.         dataSheet.addImage(img);  
  193.     }   
  194.       
  195.       
  196.     public static void main(String[] args) {  
  197.            
  198.         try {  
  199.             //创建一个工作薄  
  200.             WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls"));  
  201.             //待插入的工作表  
  202.             WritableSheet imgSheet = workbook.createSheet("Images",0);  
  203.             //要插入的图片文件  
  204.             File imgFile = new File("D:/1.png");              
  205.             //图片插入到第二行第一个单元格,长宽各占六个单元格  
  206.             insertImg(imgSheet,1,2,4,6,imgFile);//从第2列第3行开始,右占4列,下占6行  
  207.             workbook.write();             
  208.             workbook.close();  
  209.               
  210.             File excel = new File("c:\\test.XLS");//String index out of range: 118  
  211.             System.out.println(readExcel(excel));  
  212.             System.out.println(searchKeyWord(excel,"1"));  
  213.             writeExcel("c:\\test.XLS");  
  214.         } catch (IOException e) {  
  215.             e.printStackTrace();  
  216.         } catch (WriteException e) {  
  217.             e.printStackTrace();  
  218.         }         
  219.     }   
  220.       
  221. }  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/833422

0 0