`
紫_色
  • 浏览: 142772 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java 使用POI操作Excel(1)----基本使用

    博客分类:
  • J2EE
阅读更多

      Apache的POI项目为我们提供了非常方便的操作Excel以及Microsoft的其它办公软件操作.

关于java操作Excel所需要的jar包如下:大家可以在apache网站下载得到.



 

下面演示使用POI简单读取Excel:

//Hello POI
	@Test
	public void testRead() {
		try {
			//第一步:创建工作对象
			Workbook wb = WorkbookFactory.create(new File("D:/Project/MAVEN_ANT/poi_1/excel/IT.xls")) ;  
			//第二步:使用工作对象获取工作溥
			Sheet sheet = wb.getSheetAt(0) ;
			//第三步:在工作溥对象中获取行
			Row row = sheet.getRow(0) ;
			//第四步:根据行获取列
			Cell cell = row.getCell(0) ;
			//注意在Excel是有类型的.
			/**
			 *  CELL_TYPE_BLANK Blank Cell type (3)
				CELL_TYPE_BOOLEAN Boolean Cell type (4)
				CELL_TYPE_ERROR Error Cell type (5)
				CELL_TYPE_FORMULA Formula Cell type (2)
				CELL_TYPE_NUMERIC Numeric Cell type (0)
				CELL_TYPE_STRING String Cell type (1)
			 */
			System.out.println(cell.getCellType()); //获取类型
			System.out.println(cell.getStringCellValue()); //获取数据
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 	}
	
	//遍历整个工作溥
	@Test
	public void testList() {
		try {
			Workbook wb = WorkbookFactory.create(new File("D:/Project/MAVEN_ANT/poi_1/excel/IT.xls")) ;
			Sheet sheet = wb.getSheetAt(0) ;
			//sheet.getFirstRowNum() ;  //获取第一行
			//sheet.getLastRowNum() ;  //获取最后一行
			
			//普通方法
			for(int i = 0; i < sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i) ;
				//row.getFirstCellNum() ; //获取第一列
				//row.getLastCellNum() ; //获取最后一列
				for(int j = 0; j < row.getLastCellNum(); j++) {
					Cell cell = row.getCell(j) ;
					//getCellValue:方便,统计将读取的内容转为String类型
					System.out.print(getCellValue(cell) + "-----") ;
				}
				System.out.println() ;
			}
			
			//增强for
			for(Row row : sheet) {
				for(Cell cell : row) {
					System.out.print(getCellValue(cell) + "-----") ;
				}
				System.out.println() ;
			}
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
 	}
	
	/**
	 * 获取读取的数据统一转为String
	 * @param cell Cell
	 * @return String
	 */
	private String getCellValue(Cell cell) {
		String value = null ;
		switch(cell.getCellType()) {
			case Cell.CELL_TYPE_NUMERIC :
				value = String.valueOf(cell.getNumericCellValue()) ;
				break ;
			case Cell.CELL_TYPE_STRING : 
				value = cell.getStringCellValue() ;
				break ;
			case Cell.CELL_TYPE_FORMULA :
				value = String.valueOf(cell.getCellFormula()) ;
				break ;
			case Cell.CELL_TYPE_BLANK : 
				value = "" ;
				break ;
			case Cell.CELL_TYPE_BOOLEAN :
				value = String.valueOf(cell.getBooleanCellValue()) ;
				break ;
			default :
				value = null ;
				break ;
		}
		return value ;
	}

 

 下面是关于Excel的写操作:

@Test
	public void testWrite() {
		Workbook workbook = new HSSFWorkbook() ;
		FileOutputStream fos = null ;
		try {
			fos = new FileOutputStream("D:/Project/MAVEN_ANT/poi_1/excel/poiTest.xls") ;
			Sheet sheet = workbook.createSheet("hello") ; //创建工作溥.
			Row row = sheet.createRow(0) ; //创建表格的第一行(从0开始)
			row.setHeightInPoints(30) ;  //设置行高
			Cell cell = row.createCell(0) ; //创建第一列
			CellStyle cellCss = workbook.createCellStyle() ;  //创建列样式
			cellCss.setAlignment(CellStyle.ALIGN_CENTER) ;
			cell.setCellValue("hll") ;
			cell = row.createCell(1) ;  //创建第二列
			cell.setCellValue("lfd") ;
			cell.setCellStyle(cellCss) ;
			workbook.write(fos) ;  //输出到Excel
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("文件格式错误!请检查文件格式.") ;
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("找不到文件!") ;
		} finally {
			if(fos != null) {
				try {
					fos.close() ;
				} catch (IOException e) {
					e.printStackTrace();
					throw new RuntimeException("文件不能关闭") ;
				}
			}
		}
	}

 

 下面是该示例的项目(基于Maven建立):

http://www.kuaipan.cn/file/id_129263676497613792.htm

  • 大小: 17.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics