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

Posted 愤怒的绿萝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用jxl操作之一: 实现对Excel简单读写操作相关的知识,希望对你有一定的参考价值。

项目目录树

对象类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简单读写操作的主要内容,如果未能解决你的问题,请参考以下文章

Java 读写 Excel

POI读写海量Excel

Java实现Excel表格操作--API:jxl

使用EasyExcel进行读写操作

java 使用jxl poi 操作excel

怎么shell对Excel进入读写数据