poi方式写入数据到Excel
在java數據庫編程中,常常會用到向excel中讀寫數據,一方面可以將數據從數據庫導出到Excel,進行數據展示,另一方面可以批量的向數據庫插入多條數據,這對于軟件開發是必不可少的,今天先介紹如何使用java向excel中寫入數據,這里以2003版本的excel版本為例,分享我的實戰經驗。(在后續的經驗中會介紹excel數據導出,敬請瀏覽)
工具/原料
eclipse, java poi的jar包
方法/步驟
導入POI的jar包
新建一個項目,在根目錄在新建一個lib文件夾,將jar包復制粘貼到lib文件夾后,右鍵將其添加到項目的build path中,最后的結果如圖所示:
步驟閱讀編寫java類,新建一個實體類,比如我們要導出數據庫的有關電腦的信息,那么就建一個Computer實體類,代碼如下:
package com.qiang.poi;
public class Computer {
?private int id;
?private String name;
?private String description;
?private double price;
?private double credit;
?public int getId() {
? return id;
?}
?public Computer(int id, String name, String description, double price,
? ?double credit) {
? super();
? this.id = id;
? this.name = name;
? this.description = description;
? this.price = price;
? this.credit = credit;
?}
?public void setId(int id) {
? this.id = id;
?}
?public String getName() {
? return name;
?}
?public void setName(String name) {
? this.name = name;
?}
?public String getDescription() {
? return description;
?}
?public void setDescription(String description) {
? this.description = description;
?}
?public double getPrice() {
? return price;
?}
?public void setPrice(double price) {
? this.price = price;
?}
?public double getCredit() {
? return credit;
?}
?public void setCredit(double credit) {
? this.credit = credit;
?}
}
新建一個寫入excel的方法,如write2excel,參數可以后面邊寫邊決定(站在一個不熟悉POI的角度)
public static void write2Excel(){}?????????????????????????????????
創建操作Excel的HSSFWorkbook對象
HSSFWorkbook excel= new HSSFWorkbook();
創建HSSFSheet對象
Excel中的一個sheet(工作表)對應著java中的一個HSSFSheet對象,利用HSSFWorkbook對象可以創建一個HSSFSheet對象
??? 如:創建一個sheet名為computer的excel??
HSSFSheet sheet = excel.createSheet("computer");
創建第一行標題信息的HSSFRow對象
我們都知道excel是表格,即由一行一行組成的,那么這一行在java類中就是一個HSSFRow對象,我們通過HSSFSheet對象就可以創建HSSFRow對象
??? 如:創建表格中的第一行(我們常用來做標題的行)? HSSFRow firstRow = sheet.createRow(0); 注意下標從0開始
創建標題行中的HSSFCell數組
當然,excel中每一行是由若干個單元格,我們常稱為cell,它對應著java中的HSSFCell對象
??? 如:創建5個單元格?????? HSSFCell cells[] = new HSSFCell[5];?
//假設我們一行有五列數據
創建標題數據,并通過HSSFCell對象的setCellValue()方法對每個單元格進行賦值
既然單元格都準備好了,那最后是不是該填充數據了呀。對的,沒錯。填充數據之前,得把數據準備好吧,
??? 數據:String[] titles = new String[]{"id","name","description","price","credit"};
????插入一句話: 在這個時代,能讓機器做的,盡量不讓人來做,記住這句話。
??? 好的,繼續。現在就通過for循環來填充第一行標題的數據
for (int i = 0; i < 5; i++) {
? ?cells[0] = firstRow.createCell(i);
? ?cells[0].setCellValue(titles[i]);
? }
數據分析
第一行標題欄創建完畢后,就準備填充我們要寫入的數據吧,在java中,面向對象給我們帶來的好處在這里正好體現了,沒錯
把要填寫的數據封裝在對象中,即一行就是一個對象,n行就是一個對象列表嘛,好的,走起。
創建對象Computer,私有屬性id,name,description,price,credit,以及各屬性的setter和getter方法,如步驟二所示。
假設我們要寫入excel中的數據從數據庫查詢出來的,最后就生成了一個List<Computer>對象computers
數據寫入
具體數據有了,又該讓機器幫我們干活了,向excel中寫入數據。
for (int i = 0; i < computers.size(); i++) {
? ?HSSFRow row = sheet.createRow(i + 1);
? ?Computer computer = computers.get(i);
? ?HSSFCell cell = row.createCell(0);
? ?cell.setCellValue(computer.getId());
? ?cell = row.createCell(1);
? ?cell.setCellValue(computer.getName());
? ?cell = row.createCell(2);
? ?cell.setCellValue(computer.getDescription());
? ?cell = row.createCell(3);
? ?cell.setCellValue(computer.getPrice());
? ?cell = row.createCell(4);
? ?cell.setCellValue(computer.getCredit());
? }
將數據真正的寫入excel文件中
做到這里,數據都寫好了,最后就是把HSSFWorkbook對象excel寫入文件中了。
??????? OutputStream out = null;
?? ??? ?try {
?? ??? ??? ?out = new FileOutputStream(file);
?? ??? ??? ?excel.write(out);
?? ??? ??? ?out.close();
?? ??? ?} catch (FileNotFoundException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
??????? System.out.println("數據已經寫入excel"); //溫馨提示
看看我的main方法吧
public static void main(String[] args) throws IOException {
? File file = new File("test1.xls");
? if(!file.exists()){
? ?file.createNewFile();
? }
? List<Computer> computers = new ArrayList<Computer>();
? computers.add(new Computer(1,"宏碁","筆記本電腦",3333,9.0));
? computers.add(new Computer(2,"蘋果","筆記本電腦,一體機",8888,9.6));
? computers.add(new Computer(3,"聯想","筆記本電腦,臺式機",4444,9.3));
? computers.add(new Computer(4, "華碩", "筆記本電腦,平板電腦",3555,8.6));
? computers.add(new Computer(5, "注解", "以上價格均為捏造,如有雷同,純屬巧合", 1.0, 9.9));
? write2excel(computers, file);
?}
工程目錄及執行main方法后的test1.xls數據展示
步驟閱讀步驟閱讀源碼分享,computer就不貼了
package com.qiang.poi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ReadExcel {
?public static void main(String[] args) throws IOException {
? File file = new File("test1.xls");
? if(!file.exists()){
? ?file.createNewFile();
? }
? List<Computer> computers = new ArrayList<Computer>();
? computers.add(new Computer(1,"宏碁","筆記本電腦",3333,9.0));
? computers.add(new Computer(2,"蘋果","筆記本電腦,一體機",8888,9.6));
? computers.add(new Computer(3,"聯想","筆記本電腦,臺式機",4444,9.3));
? computers.add(new Computer(4, "華碩", "筆記本電腦,平板電腦",3555,8.6));
? computers.add(new Computer(5, "注解", "以上價格均為捏造,如有雷同,純屬巧合", 1.0, 9.9));
? write2excel(computers, file);
?}
?
?public static void write2excel(List<Computer> computers,File file) {
? HSSFWorkbook excel = new HSSFWorkbook();
? HSSFSheet sheet = excel.createSheet("computer");
? HSSFRow firstRow = sheet.createRow(0);
? HSSFCell cells[] = new HSSFCell[5];
? String[] titles = new String[] { "id", "name", "description", "price",
? ? "credit" };
? for (int i = 0; i < 5; i++) {
? ?cells[0] = firstRow.createCell(i);
? ?cells[0].setCellValue(titles[i]);
? }
? for (int i = 0; i < computers.size(); i++) {
? ?HSSFRow row = sheet.createRow(i + 1);
? ?Computer computer = computers.get(i);
? ?HSSFCell cell = row.createCell(0);
? ?cell.setCellValue(computer.getId());
? ?cell = row.createCell(1);
? ?cell.setCellValue(computer.getName());
? ?cell = row.createCell(2);
? ?cell.setCellValue(computer.getDescription());
? ?cell = row.createCell(3);
? ?cell.setCellValue(computer.getPrice());
? ?cell = row.createCell(4);
? ?cell.setCellValue(computer.getCredit());
? }
? ? ? ? OutputStream out = null;
? ? ? ? try {
? ? ? ? ? ? out = new FileOutputStream(file);
? ? ? ? ? ? excel.write(out);
? ? ? ? ? ? out.close();
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
?}
}
總結
以上是生活随笔為你收集整理的poi方式写入数据到Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么说开高尔夫低调?
- 下一篇: 为什么汽车?