2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > java 读取 excel poi_Java中读取Excel功能实现_POI

java 读取 excel poi_Java中读取Excel功能实现_POI

时间:2022-07-26 21:54:11

相关推荐

java 读取 excel poi_Java中读取Excel功能实现_POI

这里使用apache的poi进行读取excel

1,新建javaproject 项目:TestExcel

2,导入包

导入根目录下、lib、ooxml-lib下的所有jar

4,操作读取excel

import java.io.File;

import java.io.IOException;

import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

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;

public class Test {

// 得到一个File

public static void main(String[] args) {

File file = new File("F:/color.xlsx");

Workbook workbook = null;

try {

workbook = WorkbookFactory.create(file);

} catch (InvalidFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Sheet sheet = workbook.getSheetAt(0);

Iterator rows = sheet.rowIterator();

int count = 0;

while (rows.hasNext()) {

count++;

Row row = rows.next();

// 如果不是String类型统一设置为String

if (row.getCell(2).getCellType() == 0

|| row.getCell(2).getCellType() == 2

|| row.getCell(2).getCellType() == 3

|| row.getCell(2).getCellType() == 4) {

row.getCell(2).setCellType(1);

}

System.out.println(row.getCell(2).getStringCellValue());

}

}

}

源代码和测试文件:

/s/1c0nIBiK

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。