导航:首页 > 编程语言 > javaexcel解析poi

javaexcel解析poi

发布时间:2022-12-21 01:36:06

❶ 用java如何取得EXCEL 中指定的几行的数据

可以使用poi来解析excel:
//获取指定行,索引从0开始
hssfRow=hssfSheet.getRow(1);
//获取总行数,获取的是最后一行的编号(编号从0开始)
int rowNum = sheet.getLastRowNum();

然后拿到excel对象循环解析从50开始到100即可。

❷ java 用poi 操作excel 把里面的数据取出后过滤掉非法的字符等 ,在放到数据库中

楼上哥们正解... 帮你再详细点吧
解析Excle使用POI的话 ,你是直接读取文件还是做上传再来,如果上传就稍微麻烦点,直接读取比较简单
解析Exlce的话主要用到的几个类HSSFWorkbook ,Excle对象
通过他获取你的sheet ,可以通过制定名字来wb.getShee(名字);
然后通过sheet 获取行 sheel.getRow(first),通过行再获取每个单元格HSSFCell
你在获取每个单元格的时候最好先判断下是否为空这些,避免空指针
同时POI也提供了 判断取出来的值是什么类型的比如字符串这些。
过滤非法字符串这些 你可以通过Pattern.matches(),这个方法来匹配
最后将读取的excle数据封装成为集合,批量插入数据库...
加油把.... 网上有很多这种资料.. ~~

❸ java excel 解析 什么开源工具好

推荐使用poi,这是用的最多的,导入导出都非常方便。

JAVA 使用POI制作表格,而且都是开源的。POI是Apace公司开发的,对中文的支持比较弱一些;而JExcelAPI是韩国公司开发的,不仅对中文的支持好,而且由于是纯JAVA编写的,所以可以跨平台操作。

HSSFCell cell = row.createCell((short) 0);

//设置此单元格的格式为文本,此句可以省略,Excel会自动识别。

//其他还有几种常用的格式,请参考本文底部的补充部分。

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

//此处是3.0.1版的改进之处,上一版可以直接setCellValue("Hello, World!"),

//但是在3.0.1里,被deprecated了。

cell.setCellValue(new HSSFRichTextString("Hello, World!"));

//创建一个文件输出流,指定到C盘根目录下(C盘都有吧?)

//xls是Excel97-2003的标准扩展名,2007是xlsx,目前的POI能直接生产的还是xls格式,

//如果此处把扩展名改成xlsx,在用Excel2007打开此文件时会报错。

❹ Java对Excel解析(求助)

这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx。

读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL

代码如下

/**
*
*/
packagecom.b510.common;

/**
*@authorHongten
*@created2014-5-21
*/
publicclassCommon{

publicstaticfinalStringOFFICE_EXCEL_2003_POSTFIX="xls";
publicstaticfinalStringOFFICE_EXCEL_2010_POSTFIX="xlsx";

publicstaticfinalStringEMPTY="";
publicstaticfinalStringPOINT=".";
publicstaticfinalStringLIB_PATH="lib";
_INFO_XLS_PATH=LIB_PATH+"/student_info"+POINT+OFFICE_EXCEL_2003_POSTFIX;
_INFO_XLSX_PATH=LIB_PATH+"/student_info"+POINT+OFFICE_EXCEL_2010_POSTFIX;
publicstaticfinalStringNOT_EXCEL_FILE=":NottheExcelfile!";
="Processing...";

}

/**
*
*/
packagecom.b510.excel;

importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;

importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.xssf.usermodel.XSSFCell;
importorg.apache.poi.xssf.usermodel.XSSFRow;
importorg.apache.poi.xssf.usermodel.XSSFSheet;
importorg.apache.poi.xssf.usermodel.XSSFWorkbook;

importcom.b510.common.Common;
importcom.b510.excel.util.Util;
importcom.b510.excel.vo.Student;

/**
*@authorHongten
*@created2014-5-20
*/
publicclassReadExcel{

/**
*readtheExcelfile
*@
*@return
*@throwsIOException
*/
publicList<Student>readExcel(Stringpath)throwsIOException{
if(path==null||Common.EMPTY.equals(path)){
returnnull;
}else{
Stringpostfix=Util.getPostfix(path);
if(!Common.EMPTY.equals(postfix)){
if(Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
returnreadXls(path);
}elseif(Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)){
returnreadXlsx(path);
}
}else{
System.out.println(path+Common.NOT_EXCEL_FILE);
}
}
returnnull;
}

/**
*ReadtheExcel2010
*@
*@return
*@throwsIOException
*/
publicList<Student>readXlsx(Stringpath)throwsIOException{
System.out.println(Common.PROCESSING+path);
InputStreamis=newFileInputStream(path);
XSSFWorkbookxssfWorkbook=newXSSFWorkbook(is);
Studentstudent=null;
List<Student>list=newArrayList<Student>();
//ReadtheSheet
for(intnumSheet=0;numSheet<xssfWorkbook.getNumberOfSheets();numSheet++){
XSSFSheetxssfSheet=xssfWorkbook.getSheetAt(numSheet);
if(xssfSheet==null){
continue;
}
//ReadtheRow
for(introwNum=1;rowNum<=xssfSheet.getLastRowNum();rowNum++){
XSSFRowxssfRow=xssfSheet.getRow(rowNum);
if(xssfRow!=null){
student=newStudent();
XSSFCellno=xssfRow.getCell(0);
XSSFCellname=xssfRow.getCell(1);
XSSFCellage=xssfRow.getCell(2);
XSSFCellscore=xssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
returnlist;
}

/**
*ReadtheExcel2003-2007
*@parampaththepathoftheExcel
*@return
*@throwsIOException
*/
publicList<Student>readXls(Stringpath)throwsIOException{
System.out.println(Common.PROCESSING+path);
InputStreamis=newFileInputStream(path);
HSSFWorkbookhssfWorkbook=newHSSFWorkbook(is);
Studentstudent=null;
List<Student>list=newArrayList<Student>();
//ReadtheSheet
for(intnumSheet=0;numSheet<hssfWorkbook.getNumberOfSheets();numSheet++){
HSSFSheethssfSheet=hssfWorkbook.getSheetAt(numSheet);
if(hssfSheet==null){
continue;
}
//ReadtheRow
for(introwNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
HSSFRowhssfRow=hssfSheet.getRow(rowNum);
if(hssfRow!=null){
student=newStudent();
HSSFCellno=hssfRow.getCell(0);
HSSFCellname=hssfRow.getCell(1);
HSSFCellage=hssfRow.getCell(2);
HSSFCellscore=hssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
returnlist;
}

@SuppressWarnings("static-access")
privateStringgetValue(XSSFCellxssfRow){
if(xssfRow.getCellType()==xssfRow.CELL_TYPE_BOOLEAN){
returnString.valueOf(xssfRow.getBooleanCellValue());
}elseif(xssfRow.getCellType()==xssfRow.CELL_TYPE_NUMERIC){
returnString.valueOf(xssfRow.getNumericCellValue());
}else{
returnString.valueOf(xssfRow.getStringCellValue());
}
}

@SuppressWarnings("static-access")
privateStringgetValue(HSSFCellhssfCell){
if(hssfCell.getCellType()==hssfCell.CELL_TYPE_BOOLEAN){
returnString.valueOf(hssfCell.getBooleanCellValue());
}elseif(hssfCell.getCellType()==hssfCell.CELL_TYPE_NUMERIC){
returnString.valueOf(hssfCell.getNumericCellValue());
}else{
returnString.valueOf(hssfCell.getStringCellValue());
}
}
}

/**
*
*/
packagecom.b510.excel.client;

importjava.io.IOException;
importjava.util.List;

importcom.b510.common.Common;
importcom.b510.excel.ReadExcel;
importcom.b510.excel.vo.Student;

/**
*@authorHongten
*@created2014-5-21
*/
publicclassClient{

publicstaticvoidmain(String[]args)throwsIOException{
Stringexcel2003_2007=Common.STUDENT_INFO_XLS_PATH;
Stringexcel2010=Common.STUDENT_INFO_XLSX_PATH;
//readthe2003-2007excel
List<Student>list=newReadExcel().readExcel(excel2003_2007);
if(list!=null){
for(Studentstudent:list){
System.out.println("No.:"+student.getNo()+",name:"+student.getName()+",age:"+student.getAge()+",score:"+student.getScore());
}
}
System.out.println("======================================");
//readthe2010excel
List<Student>list1=newReadExcel().readExcel(excel2010);
if(list1!=null){
for(Studentstudent:list1){
System.out.println("No.:"+student.getNo()+",name:"+student.getName()+",age:"+student.getAge()+",score:"+student.getScore());
}
}
}
}

/**
*
*/
packagecom.b510.excel.util;

importcom.b510.common.Common;

/**
*@authorHongten
*@created2014-5-21
*/
publicclassUtil{

/**
*getpostfixofthepath
*@parampath
*@return
*/
publicstaticStringgetPostfix(Stringpath){
if(path==null||Common.EMPTY.equals(path.trim())){
returnCommon.EMPTY;
}
if(path.contains(Common.POINT)){
returnpath.substring(path.lastIndexOf(Common.POINT)+1,path.length());
}
returnCommon.EMPTY;
}
}

/**
*
*/
packagecom.b510.excel.vo;

/**
*Student
*
*@authorHongten
*@created2014-5-18
*/
publicclassStudent{
/**
*id
*/
privateIntegerid;
/**
*学号
*/
privateStringno;
/**
*姓名
*/
privateStringname;
/**
*学院
*/
privateStringage;
/**
*成绩
*/
privatefloatscore;

publicIntegergetId(){
returnid;
}

publicvoidsetId(Integerid){
this.id=id;
}

publicStringgetNo(){
returnno;
}

publicvoidsetNo(Stringno){
this.no=no;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetAge(){
returnage;
}

publicvoidsetAge(Stringage){
this.age=age;
}

publicfloatgetScore(){
returnscore;
}

publicvoidsetScore(floatscore){
this.score=score;
}

}

❺ java中poi读取excel时报错:Unable to construct record instance,怎么解决呀

根据你的截图,错误的可能有两个,要分别测试对应一下:
1、excel文档有问题,从截图下方看(就是乱码部分)可能excel文档的第1个sheet是个被删除的sheet,所以名称是很长的乱码,导致无法读取。
修改方法:创建一个新的excel文档,然后将需要的内容以文本的形式复制进去,再调用。
2、poi的问题,这个有可能是poi和excel的版本不对应。
修改方法:下载poi的时候确定清楚里面的hkec访问版本对应的是不是你的excel文件的版本。

阅读全文

与javaexcel解析poi相关的资料

热点内容
dvd光盘存储汉子算法 浏览:757
苹果邮件无法连接服务器地址 浏览:963
phpffmpeg转码 浏览:671
长沙好玩的解压项目 浏览:145
专属学情分析报告是什么app 浏览:564
php工程部署 浏览:833
android全屏透明 浏览:737
阿里云服务器已开通怎么办 浏览:803
光遇为什么登录时服务器已满 浏览:302
PDF分析 浏览:485
h3c光纤全工半全工设置命令 浏览:143
公司法pdf下载 浏览:382
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:683
如何取消命令方块指令 浏览:350
风翼app为什么进不去了 浏览:778
im4java压缩图片 浏览:362
数据查询网站源码 浏览:150
伊克塞尔文档怎么进行加密 浏览:892
app转账是什么 浏览:163