package com.poi.excel;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 读取 excel
* @author LGF
*
*/
public class ReadExcel {
public static void main(String[] args) {
ReadExcel read = new ReadExcel();
try {
read.read("ORACLE.xlsx");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取 excel 文件
* @param name 文件名称
* @throws IOException
*/
public void read(String name) throws IOException{
//获取文件流
InputStream input = getInputStream(name);
//构造 XSSFWorkbook 对象传入 文件流
XSSFWorkbook wb = new XSSFWorkbook(input);
//获取 excel中第一个表
XSSFSheet hs = wb.getSheetAt(0);
//获取第一个表的总行数
int count = hs.getLastRowNum();
//获取excel中有几个表
int sheetCount = wb.getNumberOfSheets();
//简单打印一下
System.out.println("sheet1 count:" + count);
System.out.println("sheetCount:" + sheetCount);
//循环遍历第一个表中的数据
for (int i = 0; i <= count; i++) {
//获取第 i 行对象
XSSFRow row = hs.getRow(i);
//获取第 i 行对象中的总列数
int column = row.getLastCellNum();
//循环遍历取值
for (int j = 0; j <column; j++) {
XSSFCell cell = row.getCell(j);
System.out.println("type:"+cell.getCellType());
String value = cell.getStringCellValue();
if(value.trim().isEmpty())continue;
System.out.print(value + "|");
}
System.out.println("\n---------------------------------------------------------------------------");
}
//最后释放资源
wb.close();
input.close();
}
/**
* 获取 class path 中的文件流
* @param name 名称
* @return InputStream
*/
public InputStream getInputStream(String name){
return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
}
}
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 读取 excel
* @author LGF
*
*/
public class ReadExcel {
public static void main(String[] args) {
ReadExcel read = new ReadExcel();
try {
read.read("ORACLE.xlsx");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读取 excel 文件
* @param name 文件名称
* @throws IOException
*/
public void read(String name) throws IOException{
//获取文件流
InputStream input = getInputStream(name);
//构造 XSSFWorkbook 对象传入 文件流
XSSFWorkbook wb = new XSSFWorkbook(input);
//获取 excel中第一个表
XSSFSheet hs = wb.getSheetAt(0);
//获取第一个表的总行数
int count = hs.getLastRowNum();
//获取excel中有几个表
int sheetCount = wb.getNumberOfSheets();
//简单打印一下
System.out.println("sheet1 count:" + count);
System.out.println("sheetCount:" + sheetCount);
//循环遍历第一个表中的数据
for (int i = 0; i <= count; i++) {
//获取第 i 行对象
XSSFRow row = hs.getRow(i);
//获取第 i 行对象中的总列数
int column = row.getLastCellNum();
//循环遍历取值
for (int j = 0; j <column; j++) {
XSSFCell cell = row.getCell(j);
System.out.println("type:"+cell.getCellType());
String value = cell.getStringCellValue();
if(value.trim().isEmpty())continue;
System.out.print(value + "|");
}
System.out.println("\n---------------------------------------------------------------------------");
}
//最后释放资源
wb.close();
input.close();
}
/**
* 获取 class path 中的文件流
* @param name 名称
* @return InputStream
*/
public InputStream getInputStream(String name){
return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
}
}