• 使用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();
                }
                
            }
        
        }

    测试结果

  • 相关阅读:
    课堂测试-文本操作
    异常的总结
    动手动脑实验-异常
    从小工到专家-读后感3
    从小工到专家-读后感2
    从小工到专家-读后感1
    构建之法读书笔记(一)
    2.12日总结
    BaseAdapter的使用
    Activity之Bundle的使用
  • 原文地址:https://www.cnblogs.com/djoker/p/6363190.html
Copyright © 2020-2023  润新知