Java Excel

Java Excel是一开放源码项目,通过它开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件以及插入图片等等。 
  详细介绍及API查看官方: http://www.andykhan.com/jexcelapi/index.html 
ps:读取结束时注意调用close()方法;释放内存 
写入结束时先调用write()方法,否则得到的是空Excel,因为先前的操作都是存储在缓存中。具体见代码
 
一、读取Excel的例子: 
注意对数字、日期等不同CellType的读取 
Java代码   收藏代码
  1. /** 
  2.  * jxl 读取 
  3.  * @author Michael sun 
  4.  */  
  5. public class JxlRead {  
  6.   
  7.     /** 
  8.      * 读取 excel 文件 
  9.      * @param filePath 
  10.      * @throws Exception 
  11.      */  
  12.     private void readExcel(String filePath) throws Exception {  
  13.         InputStream is = null;  
  14.         Workbook workbook = null;  
  15.         try {  
  16.             is = new FileInputStream(filePath);  
  17.             workbook = Workbook.getWorkbook(is);  
  18.             // sheet row column 下标都是从0开始的  
  19.             Sheet sheet = workbook.getSheet(0);  
  20.   
  21.             int column = sheet.getColumns();  
  22.             int row = sheet.getRows();  
  23.             System.out.println("共有" + row + "行," + column + "列数据");  
  24.   
  25.             // A1是字符  
  26.             Cell cellA1 = sheet.getCell(00);  
  27.             System.out.println("A1 type:" + cellA1.getType());  
  28.             if (cellA1.getType().equals(CellType.LABEL)) {  
  29.                 System.out.println("A1 content:" + cellA1.getContents());  
  30.             }  
  31.   
  32.             // B1是数字  
  33.             Cell cellB1 = sheet.getCell(10);  
  34.             System.out.println("B1 type:" + cellB1.getType());  
  35.             if (cellB1.getType().equals(CellType.NUMBER)) {  
  36.                 NumberCell numberCell = (NumberCell) cellB1;  
  37.                 double douval = numberCell.getValue();  
  38.                 System.out.println("B1 value:" + douval);  
  39.             }  
  40.   
  41.             // C1是日期  
  42.             Cell cellC1 = sheet.getCell(20);  
  43.             System.out.println("C1 type:" + cellC1.getType());  
  44.             if (cellC1.getType().equals(CellType.DATE)) {  
  45.                 DateCell dateCell = (DateCell) cellC1;  
  46.                 Date date = dateCell.getDate();  
  47.                 System.out.println("C1 date:" + date);  
  48.             }  
  49.   
  50.             // 操作完成时,关闭对象,释放占用的内存空间  
  51.             workbook.close();  
  52.             is.close();  
  53.         } catch (Exception e) {  
  54.             e.printStackTrace(System.out);  
  55.         } finally {  
  56.             if (is != null) {  
  57.                 is.close();  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * @param args 
  64.      * @throws Exception 
  65.      */  
  66.     public static void main(String[] args) throws Exception {  
  67.         String filePath = "D:\\test\\testjxlread.xls";  
  68.         JxlRead jxlRead = new JxlRead();  
  69.         jxlRead.readExcel(filePath);  
  70.     }  
  71. }  

excel内容如下: 
 

运行结果: 
共有1行,4列数据 
A1 type:Label 
A1 content:字符 
B1 type:Number 
B1 value:123.0 
C1 type:Date 
C1 date:Wed Feb 24 08:00:00 CST 2010 
二、写入Excel的例子: 
Java代码   收藏代码
  1. /** 
  2.  * 写入excel 
  3.  * @author Michael sun 
  4.  */  
  5. public class JxlWrite {  
  6.   
  7.     /** 
  8.      * 写入 excel 文件 
  9.      * @param filePath 
  10.      * @throws Exception 
  11.      */  
  12.     private void writeExcel(String filePath) throws Exception {  
  13.         OutputStream os = null;  
  14.         try {  
  15.   
  16.             // 构建Workbook对象  
  17.             os = new FileOutputStream(filePath);  
  18.             WritableWorkbook wwb = Workbook.createWorkbook(os);  
  19.   
  20.             // 构建Excel sheet  
  21.             WritableSheet sheet = wwb.createSheet("test write sheet"0);  
  22.   
  23.             // 设置标题格式  
  24.             WritableFont wfTitle = new jxl.write.WritableFont(  
  25.                     WritableFont.ARIAL, 18, WritableFont.BOLD, true);  
  26.             WritableCellFormat wcfTitle = new WritableCellFormat(wfTitle);  
  27.             // 设置水平对齐方式  
  28.             wcfTitle.setAlignment(Alignment.CENTRE);  
  29.             // 设置垂直对齐方式  
  30.             wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE);  
  31.             // 设置是否自动换行  
  32.             wcfTitle.setWrap(true);  
  33.   
  34.             // 合并A1->C2  
  35.             sheet.mergeCells(0021);  
  36.             Label titleCell = new Label(00"Title Cell ", wcfTitle);  
  37.             sheet.addCell(titleCell);  
  38.   
  39.             WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,  
  40.                     WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,  
  41.                     Colour.BLUE);  
  42.             WritableCellFormat wcf = new WritableCellFormat(wf);  
  43.   
  44.             // A3  
  45.             Label labelCell = new Label(02"Label Cell ");  
  46.             sheet.addCell(labelCell);  
  47.             // B3  
  48.             Label labelCellFmt = new Label(12,  
  49.                     "Label Cell with WritableCellFormat ", wcf);  
  50.             sheet.addCell(labelCellFmt);  
  51.   
  52.             // A4 添加jxl.write.Number对象  
  53.             jxl.write.Number labelN = new jxl.write.Number(033.1415926);  
  54.             sheet.addCell(labelN);  
  55.             // B4 添加Number对象 jxl.write.NumberFormat  
  56.             NumberFormat nf = new NumberFormat("#.##");  
  57.             WritableCellFormat wcfN = new WritableCellFormat(nf);  
  58.             jxl.write.Number labelNF = new jxl.write.Number(133.1415926,  
  59.                     wcfN);  
  60.             sheet.addCell(labelNF);  
  61.   
  62.             // A5 添加jxl.write.Boolean对象  
  63.             jxl.write.Boolean labelB = new jxl.write.Boolean(04true);  
  64.             sheet.addCell(labelB);  
  65.   
  66.             // A6 添加 jxl.write.DateTime对象  
  67.             jxl.write.DateTime labelDT = new jxl.write.DateTime(05,  
  68.                     new Date());  
  69.             sheet.addCell(labelDT);  
  70.             // B6 添加DateTime对象 jxl.write.DateFormat  
  71.             jxl.write.DateFormat df = new jxl.write.DateFormat(  
  72.                     "yyyy-MM-dd HH:mm:ss");  
  73.             WritableCellFormat wcfDF = new WritableCellFormat(df);  
  74.             jxl.write.DateTime labelDTF = new jxl.write.DateTime(15,  
  75.                     new Date(), wcfDF);  
  76.             sheet.addCell(labelDTF);  
  77.             //先调用write();再调用close();  
  78.             wwb.write();  
  79.             wwb.close();  
  80.             os.close();  
  81.         } catch (Exception e) {  
  82.             e.printStackTrace();  
  83.         } finally {  
  84.             if (null != os) {  
  85.                 os.close();  
  86.             }  
  87.         }  
  88.   
  89.     }  
  90.   
  91.     /** 
  92.      * @param args 
  93.      * @throws Exception 
  94.      */  
  95.     public static void main(String[] args) throws Exception {  
  96.         String filePath = "D:\\test\\testjxlwrite.xls";  
  97.         JxlWrite jxlwrite = new JxlWrite();  
  98.         jxlwrite.writeExcel(filePath);  
  99.     }  
  100.   
  101. }  

运行结果: 
 
ps:添加DateTime对象时 如果没有加格式时,出现1900/1/0 不知啥原因? 
三、插入图片 
Java代码   收藏代码
  1. /** 
  2.  * jxl 插入图片(图像格式只支持png) 
  3.  * @author Michael sun 
  4.  */  
  5. public class JxlWriteImg {  
  6.     /** 
  7.      *  
  8.      * @param filePath 
  9.      */  
  10.     private void writeImg(String filePath) throws Exception {  
  11.   
  12.         OutputStream os = null;  
  13.         try {  
  14.             String imgPath = "d:\\test\\xx.png";  
  15.             os = new FileOutputStream(filePath);  
  16.             WritableWorkbook wwb = Workbook.createWorkbook(os);  
  17.             WritableSheet ws = wwb.createSheet("write img"0);  
  18.             File imgFile = new File(imgPath);  
  19.   
  20.             // WritableImage(col, row, width, height, imgFile);  
  21.             WritableImage image = new WritableImage(21820, imgFile);  
  22.             ws.addImage(image);  
  23.             wwb.write();  
  24.             wwb.close();  
  25.   
  26.         } catch (Exception e) {  
  27.             System.out.println(e);  
  28.         } finally {  
  29.             if (null != os) {  
  30.                 os.close();  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     /** 
  36.      * @param args 
  37.      * @throws Exception 
  38.      */  
  39.     public static void main(String[] args) throws Exception {  
  40.         String filePath = "D:\\test\\testjxlwriteimg.xls";  
  41.         JxlWriteImg jxlWriteImg = new JxlWriteImg();  
  42.         jxlWriteImg.writeImg(filePath);  
  43.   
  44.     }  
  45.   
  46. }  

运行结果: 
 
四、更新Excel 
Java代码   收藏代码
  1. /** 
  2.  * jxl 更新excel 
  3.  * @author Michael sun 
  4.  */  
  5. public class JxlUpdate {  
  6.   
  7.     /** 
  8.      *  
  9.      * @param filePath 
  10.      */  
  11.     private void doUpdate(String filePath) {  
  12.   
  13.         try {  
  14.             // 获得原Excel文件  
  15.             Workbook wb = Workbook.getWorkbook(new File(filePath));  
  16.             // 打开一个文件的副本,并且指定数据写回到原文件  
  17.             WritableWorkbook wwb = Workbook.createWorkbook(new File(filePath),  
  18.                     wb);  
  19.             // 对第一个工作簿的A1 更新  
  20.             WritableSheet wsheet0 = wwb.getSheet(0);  
  21.             WritableCell wc00 = wsheet0.getWritableCell(00);  
  22.             if (wc00.getType() == CellType.LABEL) {  
  23.                 Label label00 = (Label) wc00;  
  24.                 label00.setString("updata data");  
  25.             }  
  26.             // 添加一个工作表  
  27.             WritableSheet sheet = wwb.createSheet("新增工作簿"1);  
  28.             // 写入一些测试数据  
  29.             sheet.addCell(new Label(00"test data"));  
  30.   
  31.             // 关闭工作薄对象  
  32.             wwb.write();  
  33.             wwb.close();  
  34.             wb.close();  
  35.         } catch (Exception e) {  
  36.             System.out.println(e);  
  37.         }  
  38.     }  
  39.   
  40.     /** 
  41.      * @param args 
  42.      */  
  43.     public static void main(String[] args) {  
  44.         String filePath = "D:\\test\\testjxlupdate.xls";  
  45.         JxlUpdate jxlUpdate = new JxlUpdate();  
  46.         jxlUpdate.doUpdate(filePath);  
  47.     }  
  48.   
  49. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值