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

測試結果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM