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());
}
}
}
源代碼和測試文件:
http://pan.baidu.com/s/1c0nIBiK
總結
以上是生活随笔為你收集整理的java 读取 excel poi_Java中读取Excel功能实现_POI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mix2s android p功能,已升
- 下一篇: java基础语法实例教程_Java 基础