package org.cric.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
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.poifs.filesystem.POIFSFileSystem;
public class ExcelReader {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
private FileInputStream input;
private String[] excleTitle;
public String[] readExcelTitle(String excelPath) {// 读取Excel表格表头的内容
try {
input = new FileInputStream(new File(excelPath));// excelPath,Excel
// 文件 的绝对路径
fs = new POIFSFileSystem(input);
wb = new HSSFWorkbook(fs);
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);// 得到标题的内容对象。
int colNum = row.getPhysicalNumberOfCells(); // 得到标题总列数
excleTitle = new String[colNum];
for (int i = 0; i < colNum; i++) {
excleTitle[i] = getStringCellValue(row.getCell((short) i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return excleTitle;
}
public Map<Integer, String> readExcelContent(String excelPath) {// 读取Excel数据内容
Map<Integer, String> content = new HashMap<Integer, String>();
String excelStr = "";// excel 内容
try {
input = new FileInputStream(new File(excelPath));
fs = new POIFSFileSystem(input);
wb = new HSSFWorkbook(fs);
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum(); // 得到总行数
row = sheet.getRow(0);// 得到标题的内容对象。
int colNum = row.getPhysicalNumberOfCells();// 得到每行的列数。
for (int i = 1; i < rowNum; i++) { // 正文内容应该从第二行开始,第一行为表头的标题
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
excelStr += getStringCellValue(row.getCell((short) j)).trim()+ "&";
j++;
}
content.put(i, excelStr);
excelStr = "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}
private String getStringCellValue(HSSFCell cell) {// 获取单元格数据内容为字符串类型的数据
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
private String getDateCellValue(HSSFCell cell) {// 获取单元格数据内容为日期类型的数据
String result = "";
try {
int cellType = cell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
Date date = cell.getDateCellValue();
result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
+ "-" + date.getDate();
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
String date = getStringCellValue(cell);
result = date.replaceAll("[年月]", "-").replace("日", "").trim();
} else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
result = "";
}
} catch (Exception e) {
System.out.println("日期格式不正确!");
e.printStackTrace();
}
return result;
}
}
package org.cric.action;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
public class ExcelReaderTest {
public static void main(String[] args) {
ExcelReader excelReader = new ExcelReader();
String excelPath = new String("E:\\报表.xls");
String[] headers = excelReader.readExcelTitle(excelPath);
for(int i = 0; i < headers.length; i++){
System.out.print(headers[i]+"\t");
if(i == headers.length-1){
System.out.println();
}
}
System.out.println("--------------------------------------------------");
Map<Integer,String> content = excelReader.readExcelContent(excelPath);
Collection<String> collection = content.values();
Iterator<String> it = collection.iterator();
while(it.hasNext()){
String value = it.next();
String[] cellValue = value.split("&");
for(int i = 0; i < cellValue.length; i++){
System.out.print(cellValue[i]+"\t");
if(i == cellValue.length-1){
System.out.println();
}
}
System.out.println("--------------------------------------------------");
}
}
}
分享到:
相关推荐
apache poi 读取 Excel 的 jar 包 博文链接:https://wxinpeng.iteye.com/blog/231895
Java使用apache POI读取xlsx代码以及相关jar包
apache POI,文件读写 ,excel 对于使用apache poi 解析微软excel的一些文件
NULL 博文链接:https://wxb-j2ee.iteye.com/blog/1489526
本案例中利用Apache Poi读取Excel用法,源码注释详细基本用法!
主要给大家介绍了关于Poi读取Excel引发内存溢出问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
apache poi解析Excel时报错,我将Excel打开后保存,就不会报错,我将重写的类从新打包这样问题虽然网上有解决方案,但是需要自己打包重写对应类,我已将这些步骤做好了。
自己花了几天时间写的基于Apache POI的Java 读取excel文件,功能还是比较齐全
spark-excel:一个用于通过Apache POI读取Excel文件的Spark插件
apache poi读取excel所需的jar包
利用 Apache POI 对 excel 操作并存入MySQL数据库的一个小demo。
主要介绍了java如何利用POI读取Excel行数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用apache poi读取excel内容,后缀是xls和xlsx都可以使用
android 使用poi读取高版本excel, 解决以下这两个错误 java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/stream/XMLEventFactory; at org.apache.poi.openxml4j.opc.internal.marshallers....
Apache POI库是一个开源的Java库,可以帮助开发人员处理Microsoft Office格式的文档,例如Word文档、Excel电子表格和PowerPoint演示文稿等。以下是Apache POI库的详细介绍: 支持多种Office格式:Apache POI库支持...
在此存储库中,我们有一个示例,确定如何使用apache poi读取Excel文件并将所有项目保存在mongoDB中 在此存储库中,我们有一些模块用于主要项目类,kafka服务,poi服务,最后保存在MongoDb中 excel文件上传 运行和...
Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件....Apache POI 提供Java操作Excel解决方案。
Excel POI读取封装(文件+示范代码) package org.excel.service; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java....
事件驱动解析是把文件转换成xml,然后一边读取一边解析,这样就对内存的占用就会很少,可以很好的处理poi出现OOM的问题。 maven添加需要的jar包 <groupId>org.apache.poi <artifactId>poi <version>3.15 ...