java操作excel比较简单,但是时间长了就会忘记,因此基本的简单操作做个记录。 依赖poi的jar包,pom.xml配置如下: 1. <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3. <modelVersion>4.0.0</modelVersion> 4. <groupId>excelDemo1</groupId> 5. <artifactId>excelDemo1</artifactId> 6. <packaging>war</packaging> 7. <version>0.0.1-SNAPSHOT</version> 8. <name>excelDemo1 Maven Webapp</name> 9. <url>http://maven.apache.org</url> 10. <dependencies> 11. <dependency> 12. <groupId>junit</groupId> 13. <artifactId>junit</artifactId> 14. <version>3.8.1</version> 15. <scope>test</scope> 16. </dependency> 17. <dependency> 18. <groupId>org.apache.poi</groupId> 19. <artifactId>poi</artifactId> 20. <version>3.8</version> 21. </dependency> 22. </dependencies> 23. <build> 24. <finalName>excelDemo1</finalName> 25. </build> 26. </project>
相应的java测试代码分别如下: 1. package excelDemo1; 2. 3. import java.io.File; 4. import java.io.FileOutputStream; 5. import java.io.OutputStream; 6. import org.apache.poi.hssf.usermodel.HSSFRow; 7. import org.apache.poi.hssf.usermodel.HSSFSheet; 8. import org.apache.poi.hssf.usermodel.HSSFWorkbook; 9. 10. public class ExcelDemo0 { 11. /** 12. * java生成excel文件并写入磁盘 13. * 14. * @author:tuzongxun 15. * @Title: main 16. * @param@param args 17. * @return void 18. * @date Apr 28,2016 7:32:52 PM 19. * @throws 20. */ 21. public static void main(String[] args) { 22. //C:\Users\tuzongxun123\Desktop桌面,windows和linux的斜杠不一样,而且java对于“/”需要转义处理,File.separator可以实现跨平台 23. File file = new File("C:" + File.separator + "Users" + File.separator 24. + "tuzongxun123" + File.separator + "Desktop" + File.separator 25. + "ioFile" + File.separator + "user.xls"); 26. try { 27. OutputStream outputStream = new FileOutputStream(file); 28. // 创建excel文件,注意这里的hssf是excel2007及以前版本可用,2007版以后的不可用,要用xssf 29. HSSFWorkbook workbook = new HSSFWorkbook(); 30. // 创建excel工作表 31. HSSFSheet sheet = workbook.createSheet("user"); 32. // 为工作表增加一行 33. HSSFRow row = sheet.createRow(0); 34. // 在指定的行上增加两个单元格 35. row.createCell(0).setCellValue("name"); 36. row.createCell(1).setCellValue("password"); 37. // 调用输出流把excel文件写入到磁盘 38. workbook.write(outputStream); 39. // 关闭输出流 40. outputStream.close(); 41. } catch (Exception e) { 42. e.printStackTrace(); 43. } 44. } 45. } 1. package excelDemo1; 2. 3. import java.io.BufferedInputStream; 4. import java.io.File; 5. import java.io.FileInputStream; 6. import org.apache.poi.hssf.usermodel.HSSFRow; 7. import org.apache.poi.hssf.usermodel.HSSFSheet; 8. import org.apache.poi.hssf.usermodel.HSSFWorkbook; 9. import org.apache.poi.poifs.filesystem.POIFSFileSystem; 10. 11. /** 12. * 读取excel文件 13. * 14. * @author tuzongxun123 15. * 16. */ 17. public class ExcelDemo2 { 18. public static void main(String[] agrs) { 19. try { 20. // 获取excel文件输入流 21. FileInputStream fileInputStream = new FileInputStream("C:" 22. + File.separator + "Users" + File.separator 23. + "tuzongxun123" + File.separator + "Desktop" 24. + File.separator + "ioFile" + File.separator + "user.xls"); 25. BufferedInputStream bufferedInputStream = newBufferedInputStream( 26. fileInputStream); 27. POIFSFileSystem fileSystem = new POIFSFileSystem( 28. bufferedInputStream); 29. // 获取excel文件 30. HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem); 31. // 根据名称获取指定的excel工作薄 32. HSSFSheet sheet = hssfWorkbook.getSheet("user"); 33. // 这里实际上可以用sheet.rowIterator()来遍历 34. for (int i = 1;; i++) { 35. HSSFRow row = sheet.getRow(i); 36. if (row != null) { 37. String nameString1 = row.getCell(0).getStringCellValue(); 38. String password = row.getCell(i).getStringCellValue(); 39. System.out.println("name:" + nameString1); 40. System.out.println("password:" + password); 41. bufferedInputStream.close(); 42. } else { 43. bufferedInputStream.close(); 44. return; 45. } 46. } 47. 48. } catch (Exception e) { 49. e.printStackTrace(); 50. } 51. } 52. } 53.
|