学习总结篇 java创建excle 解析excle (poi)

public static void main(String[] args) {
		
		try {
			//createWB();
			readExcle("e:\\woorbook.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void createWB(){
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("学生信息表");
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell((short)0);
		cell.setCellValue("编号");
		row.createCell((short)1).setCellValue("姓名");
		row.createCell((short)2).setCellValue("性别");
		row.createCell((short)3).setCellValue("年龄");
		row.createCell((short)4).setCellValue("性别");
		row.createCell((short)5).setCellValue("班级");
		row.createCell((short)6).setCellValue("住址");
		row.createCell((short)7).setCellValue("入学日期");
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

		for(int i=1;i<=5;i++){
			row = sheet.createRow((short)i);
			createCell(workbook, row, (short)0, String.valueOf(i));
			createCell(workbook, row, (short)1, "姓名"+i);
			createCell(workbook, row, (short)2, "性别"+i);
			createCell(workbook, row, (short)3, "年龄"+i);
			createCell(workbook, row, (short)4, "性别"+i);
			createCell(workbook, row, (short)5, "班级"+i);
			createCell(workbook, row, (short)6, "住址"+i);
			createCell(workbook, row, (short)7, new Date().toLocaleString());
		}
		try {
			FileOutputStream stream = new FileOutputStream("e:\\woorbook.xls");
			workbook.write(stream);
			stream.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	public static void createCell(HSSFWorkbook wb,HSSFRow row,short i,String value){
		HSSFCell cell = row.createCell(i);
		cell.setCellValue(value);
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment((short)10);
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
		cell.setCellStyle(cellStyle);
	}
	
	public static void readExcle(String path) throws Exception{
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(path));
		for(int sheetindex = 0 ; sheetindex<workbook.getNumberOfSheets();sheetindex++){
			HSSFSheet sheet = workbook.getSheetAt(sheetindex);
			if(sheet != null){
				  for (int rowNumOfSheet = 0; rowNumOfSheet <= sheet.getLastRowNum(); rowNumOfSheet++) { 
					  HSSFRow row = sheet.getRow(rowNumOfSheet);
					  if(row != null){
						  for(int cellindex = 0;cellindex<= row.getLastCellNum();cellindex++){
							  HSSFCell cell = row.getCell((short)cellindex);
							  if(cell != null){
								  switch(cell.getCellType()){
								   case 0:
									   String cellVal1 = cell.getStringCellValue();
									   System.out.println(cellVal1);
									   break;
								   case 1:
									   String cellVal = cell.getStringCellValue();
									   System.out.println(cellVal);
									   break;
								  }
							  }
						  }
					  }
				  }
			}
		}
		
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值