1. 程式人生 > >Java實現Excel的匯入和匯出

Java實現Excel的匯入和匯出

一、下載jxl.jar包匯入到工程中

二、新建Book.java

package com.cc.reflection;

public class Book {
	
	private int id;
	private String name;
	private String type;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	

}


三、新建ExcelBook

package com.cc.reflection;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelBook {

	//將資料匯出到Excel
	public void excelOut(ArrayList<Book> arrayList) {
		WritableWorkbook bWorkbook = null;
		try {
			// 建立Excel物件
			bWorkbook = Workbook.createWorkbook(new File("D:/book.xls"));
			// 通過Excel物件建立一個選項卡物件
			WritableSheet sheet = bWorkbook.createSheet("sheet1", 0);
			//使用迴圈將資料讀出
			for (int i = 0; i < arrayList.size(); i++) {
				Book book=arrayList.get(i);
				Label label=new Label(0,i,String.valueOf(book.getId()));
				Label label1=new Label(1,i,String.valueOf(book.getName()));
				Label label2=new Label(2,i,String.valueOf(book.getType()));
				sheet.addCell(label);
				sheet.addCell(label1);
				sheet.addCell(label2);
			}
			
			
			// 建立一個單元格物件,第一個為列,第二個為行,第三個為值
			Label label = new Label(0, 2, "test");
			// 將建立好的單元格放入選項卡中
			//sheet.addCell(label);
			// 寫如目標路徑
			bWorkbook.write();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				bWorkbook.close();
			} catch (WriteException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	
	//將Excel中的資料匯入
	public ArrayList<Book> ExcelIn(){
		ArrayList<Book>arrayList=new ArrayList<Book>();
		Workbook bWorkbook=null;
		try {
			bWorkbook=Workbook.getWorkbook(new File("D:/book.xls"));
			Sheet sheet=bWorkbook.getSheet(0);
			for (int i = 0; i < sheet.getRows(); i++) {
				Book book=new Book();
				//獲取單元格物件
				Cell cell =sheet.getCell(0,i);
				//獲取單元格的值
				book.setId(Integer.valueOf(cell.getContents()));
				book.setName(sheet.getCell(1,i).getContents());
				book.setType(sheet.getCell(2, i).getContents() );
				arrayList.add(book);
				
			}
			
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bWorkbook.close();
		}
		return arrayList;
	}
	

	public static void main(String[] args) {
		
		//將資料匯出到Excel中
		ExcelBook book = new ExcelBook();
//		ArrayList<Book> arrayList = new ArrayList<Book>();
//		Book bo = new Book();
//		bo.setId(1);
//		bo.setName("酒館");
//		bo.setType("生活");
//
//		Book bo1 = new Book();
//		bo1.setId(2);
//		bo1.setName("酒館1");
//		bo1.setType("生活1");
//
//		arrayList.add(bo);
//		arrayList.add(bo1);
//		book.excelOut(arrayList);
		
		//將資料從Excel中匯入
		ArrayList<Book> arrayList1 = book.ExcelIn();
		for(Book bo2:arrayList1){
			System.out.println(bo2.getName()+bo2.getType());
		}
	}
}

點選執行