欢迎来到天天文库
浏览记录
ID:34723689
大小:101.68 KB
页数:12页
时间:2019-03-10
《javaexcel导入数据库》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、借助于apathe的poi.jar,由于上传文件不支持.jar所以请下载后将文件改为.jar,在应用程序中添加poi.jar包,并将需要读取的excel文件放入根目录即可本例使用java来读取excel的内容并展出出结果,代码如下:importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava
2、.text.DecimalFormat;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Date;importjava.util.List;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFDateUtil;importorg.apache.poi.hssf.usermod
3、el.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.poifs.filesystem.POIFSFileSystem;publicclassExcelOperate{publicstaticvoidmain(String[]args)throwsException{Filefile=newFile("ExcelDemo.xls");Str
4、ing[][]result=getData(file,1);introwLength=result.length;for(inti=0;i5、的行数,比喻行头不需要读入忽略的行数为1*@return读出的Excel中数据的内容*@throwsFileNotFoundException*@throwsIOException*/publicstaticString[][]getData(Filefile,intignoreRows)throwsFileNotFoundException,IOException{Listresult=newArrayList();introwSize=0;BufferedInputStreamin=newBu6、fferedInputStream(newFileInputStream(file));//打开HSSFWorkbookPOIFSFileSystemfs=newPOIFSFileSystem(in);HSSFWorkbookwb=newHSSFWorkbook(fs);HSSFCellcell=null;for(intsheetIndex=0;sheetIndex7、for(introwIndex=ignoreRows;rowIndex<=st.getLastRowNum();rowIndex++){HSSFRowrow=st.getRow(rowIndex);if(row==null){continue;}inttempRowSize=row.getLastCellNum()+1;if(tempRowSize>rowSize){rowSize=tempRowSize;}String[]values=newString[rowSize];Arrays.fill(values,"");booleanh8、asValue=false;for(shortcolumnIndex=0;columnIndex<=row.getLastCellNum();columnIndex++){Stringvalue="";ce
5、的行数,比喻行头不需要读入忽略的行数为1*@return读出的Excel中数据的内容*@throwsFileNotFoundException*@throwsIOException*/publicstaticString[][]getData(Filefile,intignoreRows)throwsFileNotFoundException,IOException{Listresult=newArrayList();introwSize=0;BufferedInputStreamin=newBu
6、fferedInputStream(newFileInputStream(file));//打开HSSFWorkbookPOIFSFileSystemfs=newPOIFSFileSystem(in);HSSFWorkbookwb=newHSSFWorkbook(fs);HSSFCellcell=null;for(intsheetIndex=0;sheetIndex7、for(introwIndex=ignoreRows;rowIndex<=st.getLastRowNum();rowIndex++){HSSFRowrow=st.getRow(rowIndex);if(row==null){continue;}inttempRowSize=row.getLastCellNum()+1;if(tempRowSize>rowSize){rowSize=tempRowSize;}String[]values=newString[rowSize];Arrays.fill(values,"");booleanh8、asValue=false;for(shortcolumnIndex=0;columnIndex<=row.getLastCellNum();columnIndex++){Stringvalue="";ce
7、for(introwIndex=ignoreRows;rowIndex<=st.getLastRowNum();rowIndex++){HSSFRowrow=st.getRow(rowIndex);if(row==null){continue;}inttempRowSize=row.getLastCellNum()+1;if(tempRowSize>rowSize){rowSize=tempRowSize;}String[]values=newString[rowSize];Arrays.fill(values,"");booleanh
8、asValue=false;for(shortcolumnIndex=0;columnIndex<=row.getLastCellNum();columnIndex++){Stringvalue="";ce
此文档下载收益归作者所有