Java使用poi操作excel实例解析
本文实例为大家分享了Java使用poi操作excel的具体代码,供大家参考,具体内容如下
依赖poi的jar包,pom.xml配置如下:
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>excelDemo1</groupId> <artifactId>excelDemo1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>excelDemo1MavenWebapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.8</version> </dependency> </dependencies> <build> <finalName>excelDemo1</finalName> </build> </project>
相应的java测试代码分别如下:
packageexcelDemo1; importjava.io.File; importjava.io.FileOutputStream; importjava.io.OutputStream; importorg.apache.poi.hssf.usermodel.HSSFRow; importorg.apache.poi.hssf.usermodel.HSSFSheet; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; publicclassExcelDemo0{ /** *java生成excel文件并写入磁盘 * *@author:tuzongxun *@Title:main *@param@paramargs *@returnvoid *@dateApr28,20167:32:52PM *@throws */ publicstaticvoidmain(String[]args){ //C:\Users\tuzongxun123\Desktop桌面,windows和linux的斜杠不一样,而且java对于“/”需要转义处理,File.separator可以实现跨平台 Filefile=newFile("C:"+File.separator+"Users"+File.separator +"tuzongxun123"+File.separator+"Desktop"+File.separator +"ioFile"+File.separator+"user.xls"); try{ OutputStreamoutputStream=newFileOutputStream(file); //创建excel文件,注意这里的hssf是excel2007及以前版本可用,2007版以后的不可用,要用xssf HSSFWorkbookworkbook=newHSSFWorkbook(); //创建excel工作表 HSSFSheetsheet=workbook.createSheet("user"); //为工作表增加一行 HSSFRowrow=sheet.createRow(0); //在指定的行上增加两个单元格 row.createCell(0).setCellValue("name"); row.createCell(1).setCellValue("password"); //调用输出流把excel文件写入到磁盘 workbook.write(outputStream); //关闭输出流 outputStream.close(); }catch(Exceptione){ e.printStackTrace(); } } }
packageexcelDemo1; importjava.io.BufferedInputStream; importjava.io.File; importjava.io.FileInputStream; importorg.apache.poi.hssf.usermodel.HSSFRow; importorg.apache.poi.hssf.usermodel.HSSFSheet; importorg.apache.poi.hssf.usermodel.HSSFWorkbook; importorg.apache.poi.poifs.filesystem.POIFSFileSystem; /** *读取excel文件 * *@authortuzongxun123 * */ publicclassExcelDemo2{ publicstaticvoidmain(String[]agrs){ try{ //获取excel文件输入流 FileInputStreamfileInputStream=newFileInputStream("C:" +File.separator+"Users"+File.separator +"tuzongxun123"+File.separator+"Desktop" +File.separator+"ioFile"+File.separator+"user.xls"); BufferedInputStreambufferedInputStream=newBufferedInputStream( fileInputStream); POIFSFileSystemfileSystem=newPOIFSFileSystem( bufferedInputStream); //获取excel文件 HSSFWorkbookhssfWorkbook=newHSSFWorkbook(fileSystem); //根据名称获取指定的excel工作薄 HSSFSheetsheet=hssfWorkbook.getSheet("user"); //这里实际上可以用sheet.rowIterator()来遍历 for(inti=1;;i++){ HSSFRowrow=sheet.getRow(i); if(row!=null){ StringnameString1=row.getCell(0).getStringCellValue(); Stringpassword=row.getCell(i).getStringCellValue(); System.out.println("name:"+nameString1); System.out.println("password:"+password); bufferedInputStream.close(); }else{ bufferedInputStream.close(); return; } } }catch(Exceptione){ e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。