码迷,mamicode.com
首页 > 其他好文 > 详细

使用jxl操作之一: 实现对Excel简单读写操作

时间:2017-02-03 19:40:32      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:output   writer   his   code   this   out   jxl   方法   ble   

项目目录树

技术分享

对象类UserObject

UserObject.java
    package com.dlab.jxl;
    
    public class UserObject {
    
        private String userName;
        
        private String age;
        
        private String address;
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
        
        
        
    }

Excel简单写操作类ExcelWriter

ExcelWriter.java

package com.dlab.jxl;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.List;
    
    import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    
    public class ExcelWriter {
    
        private OutputStream os = null;
        
        public ExcelWriter(String excelPath) throws FileNotFoundException{
            this.os = new FileOutputStream(excelPath);
        }
        
        public boolean excelWrite(List list){
            try {
                //创建一个可写的Workbook
                WritableWorkbook wwb = Workbook.createWorkbook(os);
                
                //创建一个可写的sheet,第一个参数是名字,第二个参数是第几个sheet
                WritableSheet sheet = wwb.createSheet("第一个sheet", 0);
                
                for(int i=0; i<list.size(); i++){
                    UserObject user = (UserObject)list.get(i);
                    
                    //创建一个Label,第一个参数是x轴,第二个参数是y轴,第三个参数是内容,第四个参数可选,指定类型
                    Label label1 = new Label(0, i, user.getUserName());
                    Label label2 = new Label(1, i, user.getAddress());
                    Label label3 = new Label(2, i, user.getAge());
                    
                    //把label加入sheet对象中
                    sheet.addCell(label1);
                    sheet.addCell(label2);
                    sheet.addCell(label3);
                    
                }
                //保存到Workbook中
                wwb.write();
                //只有执行close时才会写入到文件中,可能在close方法中执行了io操作
                wwb.close();
                
                return true;
                
            } catch (IOException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }
            
            return false;
        }
        
    }

Excel简单读操作ExcelReader

ExcelReader.java

package com.dlab.jxl;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    public class ExcelReader {
    
        private InputStream is = null;
        
        public ExcelReader(String excelPath) throws FileNotFoundException{
            this.is = new FileInputStream(excelPath);
        }
        
        public void excelReader(){
            try {
                Workbook book = Workbook.getWorkbook(is);
                Sheet sheet =  book.getSheet(0);
                for(int i=0; i<sheet.getRows(); i++){
                    for(int j=0; j<sheet.getColumns(); j++){
                        
                        System.out.print(sheet.getCell(j, i).getContents() + " ");
                        
                        if(j == sheet.getColumns() - 1){
                            System.out.println();
                        }
                    }
                }
            } catch (BiffException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

测试类ExcelTest

package com.dlab.jxl;
    
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ExcelTest {
    
        public static void main(String[] args) {
        
            List userList = new ArrayList();
            
            for(int i=0; i<100; i++){
                UserObject user = new UserObject();
                user.setUserName("用户名称" + Integer.valueOf(i));
                user.setAddress("地址" + Integer.valueOf(i));
                user.setAge("年龄" + Integer.valueOf(i));
                userList.add(user);
            }
            
            try {
                ExcelWriter ew = new ExcelWriter("ExcelWriter.xls");
                ew.excelWrite(userList);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
            try {
                ExcelReader er = new ExcelReader("ExcelWriter.xls");
                er.excelReader();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
        }
    
    }

测试结果

技术分享

 

技术分享

 

使用jxl操作之一: 实现对Excel简单读写操作

标签:output   writer   his   code   this   out   jxl   方法   ble   

原文地址:http://www.cnblogs.com/djoker/p/6363190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!