`

使用poi实现xlsx的读取

    博客分类:
  • java
阅读更多
这里只是记录下,使用poi进行读取xlsx。
可以读取全部数据。
这里涉及的jar包比较多,故进行上传了。
代码如下:
package com.duduli.li.excelutil;

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

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil {
	public void getDataFromExcel() throws InvalidFormatException, IOException {
		List<String> list = new ArrayList<String>();
		File excelFile = new File("h:/通讯录2013.11.1更新.xlsx");
		Workbook wb =  new XSSFWorkbook(excelFile);
		Sheet sheet = wb.getSheetAt(0);
		int rowIndex = 0;
		while(true) {
			
			Row row = sheet.getRow(rowIndex);
			if(row != null) {
				int cellIndex = 0;
				while (true) {
//					System.out.println(rowIndex+" "+cellIndex);
					Cell cell = row.getCell(cellIndex);
					if(cell == null) {
						break;
					}else {
						if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
							break;
						}else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
							System.out.println(cell.getStringCellValue());
						}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
							System.out.println(cell.getNumericCellValue());
						}
					}
					cellIndex++;
				}
				rowIndex++;
			}else {
				break;
			}
		}
	}
	
	public static void main(String[] args) throws InvalidFormatException, IOException {
		ExcelUtil eu = new ExcelUtil();
		eu.getDataFromExcel();
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics