簡單poi讀取excel

1.添加依賴jar包java

maven配置:apache

<!-- poi being -->
    <dependency>
       <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>
<!-- poi end -->                

或者,不是maven項目的話,直接在buildpath中添加jar包:maven

2.讀取excel簡單例子ide

package com.demo.excel.execute; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import com.demo.excel.modle.example.Book; /** * * @ClassName: ReadBooks * @Description: 讀取圖書列表excel * @author LCL * @date 2016年9月2日 * */
public class ReadBooks { private Workbook workbook; private Sheet sheet; private Row row; private List<Book> books = new ArrayList<Book>(); /** * * @Title: readFile * @Description: 將文件讀入內存 * @param @param * filePath 文件路徑 * @return void null */
    public void readFile(String filePath) { try { File excelFile = new File(filePath); FileInputStream is = new FileInputStream(excelFile); workbook = WorkbookFactory.create(is); sheet = workbook.getSheetAt(0); } catch (Exception e) { e.printStackTrace(); } } /** * * @Title: readLine * @Description: 讀取每一行的圖書信息 * @return void null */
    public void readLine() { Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) { row = (Row) rows.next(); if (row.getRowNum() ==0 ) { continue; } Book book = new Book(); Iterator<Cell> cells = row.cellIterator(); while (cells.hasNext()) { Cell cell = (Cell) cells.next(); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && cell.getColumnIndex() == 0) { book.setCode((int)cell.getNumericCellValue()); continue; } if (cell.getColumnIndex() == 1) { book.setName(cell.getStringCellValue()); continue; } if (cell.getColumnIndex() == 2) { book.setPress(cell.getStringCellValue()); continue; } if (cell.getColumnIndex() == 3) { book.setDate(cell.getDateCellValue()); } } books.add(book); } } public List<Book> getBooks() { return books; } }

Book.java測試

package com.demo.excel.modle.example; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Book { private int code; private String name; private String press; private Date date; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "Book [code=" + code + ", name=" + name + ", press=" + press + ", date=" + formatDate(date) + "]"; } private String formatDate(Date date) { DateFormat format = new SimpleDateFormat("yyyy/MM/dd"); return format.format(date); } }

測試:ui

package com.demo.excel.db; import com.demo.excel.execute.ReadBooks; import junit.framework.TestCase; public class ReadBooksTest extends TestCase { private final String path = "E:/lichenli/storage/file/book.xlsx"; public void testReadBook() { ReadBooks readbooks = new ReadBooks(); readbooks.readFile(path); readbooks.readLine(); assertEquals(6, readbooks.getBooks().size()); } }

文件內容this

相關文章
相關標籤/搜索