❶ 用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文件的版本。