java jxl操作excel进行导入导出

来源:互联网 发布:retrofit 提交json 编辑:程序博客网 时间:2024/05/22 03:04

主要的任务还是贴代码,这段代码实现了基本的java操作excel进行导入导出的操作。

ReadWriteExcelUtil.java

Java代码 复制代码收藏代码
  1. package com.dsh.javaexcel.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.WritableSheet; 
  12. import jxl.write.WritableWorkbook; 
  13. import jxl.write.WriteException; 
  14. import jxl.write.biff.RowsExceededException; 
  15.  
  16. public class ReadWriteExcelUtil { 
  17.  
  18.     /**
  19.      * @param args
  20.      */ 
  21.     public staticvoid main(String[] args) { 
  22.         String fileName = "d:" + File.separator +"test.xls"
  23.         System.out.println(ReadWriteExcelUtil.readExcel(fileName)); 
  24.         String fileName1 = "d:" + File.separator +"abc.xls"
  25.         ReadWriteExcelUtil.writeExcel(fileName1); 
  26.     } 
  27.  
  28.     /**
  29.      * 從excel文件中讀取所有的內容
  30.      *
  31.      * @param file
  32.      *            excel文件
  33.      * @return excel文件的內容
  34.      */ 
  35.     public static String readExcel(String fileName) { 
  36.         StringBuffer sb = new StringBuffer(); 
  37.         Workbook wb = null
  38.         try
  39.             // 构造Workbook(工作薄)对象 
  40.             wb = Workbook.getWorkbook(new File(fileName)); 
  41.         } catch (BiffException e) { 
  42.             e.printStackTrace(); 
  43.         } catch (IOException e) { 
  44.             e.printStackTrace(); 
  45.         } 
  46.  
  47.         if (wb == null
  48.             return null
  49.  
  50.         // 获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了 
  51.         Sheet[] sheet = wb.getSheets(); 
  52.  
  53.         if (sheet != null && sheet.length >0) { 
  54.             // 对每个工作表进行循环 
  55.             for (int i =0; i < sheet.length; i++) { 
  56.                 // 得到当前工作表的行数 
  57.                 int rowNum = sheet[i].getRows(); 
  58.                 for (int j =0; j < rowNum; j++) { 
  59.                     // 得到当前行的所有单元格 
  60.                     Cell[] cells = sheet[i].getRow(j); 
  61.                     if (cells != null && cells.length > 0) { 
  62.                         // 对每个单元格进行循环 
  63.                         for (int k =0; k < cells.length; k++) { 
  64.                             // 读取当前单元格的值 
  65.                             String cellValue = cells[k].getContents(); 
  66.                             sb.append(cellValue + "\t"); 
  67.                         } 
  68.                     } 
  69.                     sb.append("\r\n"); 
  70.                 } 
  71.                 sb.append("\r\n"); 
  72.             } 
  73.         } 
  74.         // 最后关闭资源,释放内存 
  75.         wb.close(); 
  76.         return sb.toString(); 
  77.     } 
  78.  
  79.     /**
  80.      * 把內容寫入excel文件中
  81.      *
  82.      * @param fileName
  83.      *            要寫入的文件的名稱
  84.      */ 
  85.     public staticvoid writeExcel(String fileName) { 
  86.         WritableWorkbook wwb = null
  87.         try
  88.             // 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象 
  89.             wwb = Workbook.createWorkbook(new File(fileName)); 
  90.         } catch (IOException e) { 
  91.             e.printStackTrace(); 
  92.         } 
  93.         if (wwb != null) { 
  94.             // 创建一个可写入的工作表 
  95.             // Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置 
  96.             WritableSheet ws = wwb.createSheet("sheet1",0); 
  97.  
  98.             // 下面开始添加单元格 
  99.             for (int i =0; i <10; i++) { 
  100.                 for (int j =0; j <5; j++) { 
  101.                     // 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行 
  102.                     Label labelC = new Label(j, i,"这是第" + (i +1) +"行,第" 
  103.                             + (j + 1) +"列"); 
  104.                     try
  105.                         // 将生成的单元格添加到工作表中 
  106.                         ws.addCell(labelC); 
  107.                     } catch (RowsExceededException e) { 
  108.                         e.printStackTrace(); 
  109.                     } catch (WriteException e) { 
  110.                         e.printStackTrace(); 
  111.                     } 
  112.  
  113.                 } 
  114.             } 
  115.  
  116.             try
  117.                 // 从内存中写入文件中 
  118.                 wwb.write(); 
  119.                 // 关闭资源,释放内存 
  120.                 wwb.close(); 
  121.             } catch (IOException e) { 
  122.                 e.printStackTrace(); 
  123.             } catch (WriteException e) { 
  124.                 e.printStackTrace(); 
  125.             } 
  126.         } 
  127.     } 
  128.  
也没有什么难的,都是别人写好的类和方法,我们只需要调用进行了,此处省略控制excel表格样式的代码,自己需要什么样式的可以查看Api
0 0
原创粉丝点击