JXL操作

java 代码
  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    
  45.                                 //得到当前工作表的行数       
  46.                                 int rowNum = sheet[i].getRows();       
  47.                                 for(int j=0;j    
  48.                                         //得到当前行的所有单元格       
  49.                                         Cell[] cells = sheet[i].getRow(j);       
  50.                                         if(cells!=null&&cells.length>0){       
  51.                                                 //对每个单元格进行循环       
  52.                                                 for(int k=0;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.                 return sb.toString();       
  66.         }       
  67.         /**生成一个Excel文件     
  68.          * @param fileName  要生成的Excel文件名     
  69.          */      
  70.         public static void writeExcel(String fileName){       
  71.                 WritableWorkbook wwb = null;       
  72.                 try {       
  73.                         //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象       
  74.                         wwb = Workbook.createWorkbook(new File(fileName));       
  75.                 } catch (IOException e) {       
  76.                         e.printStackTrace();       
  77.                 }       
  78.                 if(wwb!=null){       
  79.                         //创建一个可写入的工作表       
  80.                         //Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置       
  81.                         WritableSheet ws = wwb.createSheet("sheet1"0);       
  82.                                
  83.                         //下面开始添加单元格       
  84.                         for(int i=0;i<10;i++){       
  85.                                 for(int j=0;j<5;j++){       
  86.                                         //这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行       
  87.                                         Label labelC = new Label(j, i, "这是第"+(i+1)+"行,第"+(j+1)+"列");       
  88.                                         try {       
  89.                                                 //将生成的单元格添加到工作表中       
  90.                                                 ws.addCell(labelC);       
  91.                                         } catch (RowsExceededException e) {       
  92.                                                 e.printStackTrace();       
  93.                                         } catch (WriteException e) {       
  94.                                                 e.printStackTrace();       
  95.                                         }       
  96.       
  97.                                 }       
  98.                         }       
  99.       
  100.                         try {       
  101.                                 //从内存中写入文件中       
  102.                                 wwb.write();       
  103.                                 //关闭资源,释放内存       
  104.                                 wwb.close();       
  105.                         } catch (IOException e) {       
  106.                                 e.printStackTrace();       
  107.                         } catch (WriteException e) {       
  108.                                 e.printStackTrace();       
  109.                         }       
  110.                 }       
  111.         }        
  112.         /**搜索某一个文件中是否包含某个关键字     
  113.          * @param file  待搜索的文件     
  114.          * @param keyWord  要搜索的关键字     
  115.          * @return     
  116.          */      
  117.         public static boolean searchKeyWord(File file,String keyWord){       
  118.                 boolean res = false;       
  119.                        
  120.                 Workbook wb = null;       
  121.                 try {       
  122.                         //构造Workbook(工作薄)对象       
  123.                         wb=Workbook.getWorkbook(file);       
  124.                 } catch (BiffException e) {       
  125.                         return res;       
  126.                 } catch (IOException e) {       
  127.                         return res;       
  128.                 }       
  129.                        
  130.                 if(wb==null)       
  131.                         return res;       
  132.                        
  133.                 //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了       
  134.                 Sheet[] sheet = wb.getSheets();       
  135.                        
  136.                 boolean breakSheet = false;       
  137.                        
  138.                 if(sheet!=null&&sheet.length>0){       
  139.                         //对每个工作表进行循环       
  140.                         for(int i=0;i    
  141.                                 if(breakSheet)       
  142.                                         break;       
  143.                                        
  144.                                 //得到当前工作表的行数       
  145.                                 int rowNum = sheet[i].getRows();       
  146.                                        
  147.                                 boolean breakRow = false;       
  148.                                        
  149.                                 for(int j=0;j    
  150.                                         if(breakRow)       
  151.                                                 break;       
  152.                                         //得到当前行的所有单元格       
  153.                                         Cell[] cells = sheet[i].getRow(j);       
  154.                                         if(cells!=null&&cells.length>0){       
  155.                                                 boolean breakCell = false;       
  156.                                                 //对每个单元格进行循环       
  157.                                                 for(int k=0;k    
  158.                                                         if(breakCell)       
  159.                                                                 break;       
  160.                                                         //读取当前单元格的值       
  161.                                                         String cellValue = cells[k].getContents();       
  162.                                                         if(cellValue==null)       
  163.                                                                 continue;       
  164.                                                         if(cellValue.contains(keyWord)){       
  165.                                                                 res = true;       
  166.                                                                 breakCell = true;       
  167.                                                                 breakRow = true;       
  168.                                                                 breakSheet = true;       
  169.                                                         }       
  170.                                                 }       
  171.                                         }       
  172.                                 }       
  173.                         }       
  174.                 }       
  175.                 //最后关闭资源,释放内存       
  176.                 wb.close();       
  177.                        
  178.                 return res;       
  179.         }       
  180.     /**往Excel中插入图片     
  181.      * @param dataSheet  待插入的工作表     
  182.      * @param col 图片从该列开始     
  183.      * @param row 图片从该行开始     
  184.      * @param width 图片所占的列数     
  185.      * @param height 图片所占的行数     
  186.      * @param imgFile 要插入的图片文件     
  187.      */      
  188.     public static void insertImg(WritableSheet dataSheet, int col, int row, int width,       
  189.             int height, File imgFile){       
  190.             WritableImage img = new WritableImage(col, row, width, height, imgFile);       
  191.             dataSheet.addImage(img);       
  192.     }        
  193.            
  194.            
  195.     public static void main(String[] args) {       
  196.                     
  197.             try {       
  198.                     //创建一个工作薄       
  199.                         WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls"));       
  200.                         //待插入的工作表       
  201.                         WritableSheet imgSheet = workbook.createSheet("Images",0);       
  202.                         //要插入的图片文件       
  203.                         File imgFile = new File("D:/1.png");       
  204.                         //图片插入到第二行第一个单元格,长宽各占六个单元格       
  205.                         insertImg(imgSheet,0,1,6,6,imgFile);       
  206.                         workbook.write();       
  207.                         workbook.close();       
  208.                 } catch (IOException e) {       
  209.                         e.printStackTrace();       
  210.                 } catch (WriteException e) {       
  211.                         e.printStackTrace();       
  212.                 }                   
  213.     }        
  214.                
  215. }       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值