java讀寫excel文件-poi

通過使用poi完成java對excel文件的讀寫,話不多說直接開始。

注意:這裏依賴的jar包需要使用兩個,一個是poi,一個是jxl。

附上依賴:

		<dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

測試excel表格的樣式如下:

姓名 手機號 地址
張三 138******** 蘑菇屯

文章最後是對應該excel格式的bean對象類

下面代碼包括讀,寫excel,中間讀取完數據需要對數據進行處理就不做具體介紹了。

package com.test;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
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;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtils {

    public static void main(String[] args) {
        File file = new File("/excelPath/excel.xls");
        List<PeopleBean> list;
        try {
            //1.讀excel
            list = readExcel(file);
            //2.讀完數據可以對數據進行處理再寫出,這裏就不做舉例了
            //3.寫excel
            createExcel(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *將excel內的數據讀取到list集合裏
     * @param file file
     * @return
     * @throws Exception
     */
    public static List<PeopleBean> readExcel(File file) throws Exception {
        Workbook workbook = Workbook.getWorkbook(new FileInputStream(
                file.getAbsoluteFile()));
        //選擇工作簿,簡單測試只選擇第一個工作簿,爲0
        Sheet sheet = workbook.getSheet(0);
        //獲取該工作簿的行數
        Integer rows = sheet.getRows();
        List<PeopleBean> datalist = new ArrayList<PeopleBean>();
        PeopleBean people ;
        //這裏i從1開始,0是標題行,去掉不作爲數據。
        for(int i = 0; i < rows; i++){
            Cell[] cells = sheet.getRow(i);
            //表格只有三列,這裏簡單測試就直接寫死了
            people = new PeopleBean(cells[0].getContents().trim(),
                    cells[1].getContents().trim(),cells[2].getContents().trim());
            //將創建的bean添加到結果集
            datalist.add(people);
        }
        return datalist;
    }

    /**
     * 將集合內的數據輸出到新的excel
     * @param resultList
     * @throws IOException
     */
    public static void createExcel(List<PeopleBean> resultList) throws IOException {
        // 創建workbook對應的excel
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        //在workbook裏面創建對應excel的工作簿sheet
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        //創建工作簿第0行的標題
        HSSFRow hssfRow = hssfSheet.createRow(0);
        //創建單元格,設置表頭
        HSSFCell hssfCell = hssfRow.createCell(0);
        hssfCell.setCellValue("");
        hssfCell = hssfRow.createCell(1);
        hssfCell.setCellValue("");
        hssfCell = hssfRow.createCell(2);
        hssfCell.setCellValue("");
        hssfCell = hssfRow.createCell(3);

        //數據循環遍歷添加到行信息
        for(int i = 0; i<resultList.size(); i++){
            HSSFRow hssfRow1 = hssfSheet.createRow(i);
            hssfRow1.createCell(0).setCellValue(resultList.get(i).getName());
            hssfRow1.createCell(1).setCellValue(resultList.get(i).getPhone());
            hssfRow1.createCell(2).setCellValue(resultList.get(i).getAdress());
        }

        //寫出生成新的excel文件
        hssfWorkbook.write(new FileOutputStream("/filePath/newExcel.xls"));
        System.out.println("write success!");

    }
}

以及bean對象類:

package com.test;


public class PeopleBean {
    private String name;
    private String phone;
    private String adress;

    public PeopleBean() {
    }

    public PeopleBean(String name, String phone, String adress) {
        this.name = name;
        this.phone = phone;
        this.adress = adress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章