jar包下载 http://witchlovelearning.iteye.com/blog/1986284
附件为源码
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
public class DataFromExcel {
/**
* @param filePath excel文件存储名称及路径 例:"E:\\person.xls" 或 "E:\\person.xlsx"
*/
private static List<List<String>> read2003Excel(String filePath) {
File excelFile = null;
InputStream inputs = null;
String cellStr = null;// 单元格,最终按字符串处理
List<List<String>> sList = new ArrayList<List<String>>();// 返回封装数据的List
try {
excelFile = new File(filePath);
if(!excelFile.exists()){
System.out.println("文件不存在") ;
}
else{
inputs = new FileInputStream(excelFile);
HSSFWorkbook wb = new HSSFWorkbook(inputs);
HSSFSheet sheet = wb.getSheetAt(0);
// 开始循环遍历行,表头不处理,从1开始
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
// 循环遍历单元格
List<String> rowStr = new ArrayList<String>(); //每行的值组成的list
for (int j = 1; j < row.getLastCellNum(); j++) { //不读入序号
HSSFCell cell = row.getCell(j);// 获取单元格对象
if (cell == null) {
cellStr = "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
cellStr = String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
cellStr = cell.getNumericCellValue() + "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {// 对公式的处理
cellStr = cell.getCellFormula();
}else {// 其余按照字符串处理
cellStr = cell.getStringCellValue();
}
rowStr.add(cellStr);
}
if("".equals(row.getCell(0)) || row.getCell(0) == null)
break;
else
sList.add(rowStr);// 数据装入List
} }
} catch (IOException e) {
e.printStackTrace();
} finally {// 关闭文件流
if (inputs != null) {
try {
inputs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sList;
}
private static List<List<String>> read2007Excel(String filePath) {
File excelFile = null;
InputStream inputs = null;
String cellStr = null;
List<List<String>> sList = new ArrayList<List<String>>();
try {
excelFile = new File(filePath);
if(!excelFile.exists()){
System.out.println("文件不存在") ;
}
else{
inputs = new FileInputStream(excelFile);
XSSFWorkbook wb = new XSSFWorkbook(inputs);// 创建Excel2007文件对象
XSSFSheet sheet = wb.getSheetAt(0);
// 开始循环遍历行,表头不处理,从1开始
for (int i = 1; i < sheet.getLastRowNum(); i++) {
XSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
// 循环遍历单元格
List<String> rowStr = new ArrayList<String>();
for (int j = 1; j < row.getLastCellNum(); j++) { //不读入序号
XSSFCell cell = row.getCell(j);// 获取单元格对象
if (cell == null) {// 单元格为空设置cellStr为空串
cellStr = "";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理
cellStr = String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理
cellStr = cell.getNumericCellValue() + "";
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {// 对公式的处理
cellStr = cell.getCellFormula();
} else {// 其余按照字符串处理
cellStr = cell.getStringCellValue();
}
rowStr.add(cellStr);
}
if("".equals(row.getCell(0)) || row.getCell(0) == null)
break;
else
sList.add(rowStr);// 数据装入List
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {// 关闭文件流
if (inputs != null) {
try {
inputs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sList;
}
public static void main(String[] args) {
List<List<String>> lists = read2007Excel("E:\\readtests.xlsx");
// List<List<String>> lists = read2003Excel("E:\\readtest.xls");
for (List<String> ls : lists) {
for (String s : ls) {
System.out.print(s+"-");
}
System.out.print("\n");
}
}
}
分享到:
相关推荐
java读取excel 表格数据。 public static void main(String[] args) { String[][] content=POIExcelUtils.read("E:\\1.xlsx"); for(int i=0;i;i++) { if(content[i]!=null){ for(int j=0;j[i].length;j...
使用poi读取写入复杂excel内容包括样式,工具类
Java通过POI读取Excel遍历数据,根本word模板批量生成word文档,demo可运行,可根据需求修改
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
JAVA IO操作 POI读取excel的内容
通过poi读取excel表格信息,可以实现对97,2003,2007,2010等excel表格的读取
Java用poi读取excel文件.pdf
Java POI根据模板生成Excel文件并写入磁盘,资源文件仅为实现的简单测试Demo,并没有进行代码优化,可以直接导入运行,资源文件仅供参考。
实现了POI读取Excel文件中的数据,并且能导出Excel文件。
Java读取Word中的表格(Excel),并导出文件为Excel
Java 操作Excel poi读写excel,所需要的jar包 https://blog.csdn.net/u014646662/article/details/83217382
ExcelImport.zip java-poi-excel 导入导出excel表格 由java读取流进行操作
java poi读取excel表格数据一整套jar包 包括(dom4j和xmlbeans包)
poi导入/导出Excel表格,合并单元格的读取和设置
JAVA通过poi对Excel进行表格的读取,含有jar包,代码可以直接用。
主要介绍了Java POI读取excel中数值精度损失问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL查看数据库 在MYSQL中运行项目db目录下的sql文件,创建...
1.java使用poi生成excel插入图表曲线 2.demo包含完成jar包 3.主要用途是过滤数据自动生成曲线图
最新版poi 相关6个jar包 for java。Java读取Excel表格数据时(.xls、.xlsx 格式),需要引用的jar包 亲测as上可以读取到2010excel内容,内附读取源码