对于解析xls(excle表格数据 )

1)对于解析excle表格数据

     思路: 当然用的是poi的第三方jar包poi-3.7。  先解析一个numSheet, 然后再解析行rowNum,然后在解析cellNum, 然后将解析的数据放在StringBuffer, 在将StringBuffer放到一个集合里返回。

public List<String> parse(String channelCharset, byte[] fileContent, Map<String, IFileParser> fileParserMap) throws Exception {
		log.info("funds xls parse start...");
		List<String> columnList = new ArrayList<String>();
		InputStream inputStream = null;
		try {
			inputStream = new BufferedInputStream(new ByteArrayInputStream(fileContent)); // 获得文件字节流
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
			for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
				HSSFSheet sheet = hssfWorkbook.getSheetAt(numSheet);
				if (sheet != null) {
					// 循环行Row
					for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
						HSSFRow row = sheet.getRow(rowNum);
						StringBuffer bufs = new StringBuffer("");
						if (row != null) {
							// 循环列Cell
							for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
								HSSFCell cell = row.getCell(cellNum);
								if (cell != null) {
									<span style="color:#ff0000;">String cellStr = getValue(cell);</span>
									if(cellStr.indexOf("E")>0 && cellStr.lastIndexOf(".")>0){
										 DecimalFormat df = new DecimalFormat("0.00");//保留两位小数
										 cellStr = df.format(cell.getNumericCellValue());
									 }
									bufs.append(cellStr);
									if (cellNum < row.getLastCellNum() - 1) { // 最后一个值不能有下划线隔开
										bufs.append(Constants.SYMBOL_1);
									}
								}else{
									bufs.append(Constants.SYMBOL_1);
									bufs.append("");//空格拼空字符
								}
							}
						}
						bufs.append(Constants.SYMBOL_1);
						bufs.append(String.valueOf(rowNum+1));//拼接行号
						columnList.add(bufs.toString());
					}
				}
			}
		} catch (Exception e) {
			log.error("excel文件解析失败", e);
		} finally {
			IOUtils.closeQuietly(inputStream);
		}

		if (CollectionUtils.isEmpty(columnList)) {
			log.info("excel 解析为空");
			return null;
		}
		log.info("xls parse end.解析的excel行一共有:" + columnList.size());

		return columnList;
	}

2) 对于获取列的值得时候, 有些列的格式是要做处理的, 所以定义了一个getValue()方法, 供上述调用。比如时间的格式有可能是多种。

private static String getValue(HSSFCell cell) {
		String result="";
		SimpleDateFormat sdf = null; 
		
		 if(cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm:ss")){
			 sdf = new SimpleDateFormat("HH:mm:ss");  
			 if(cell.getDateCellValue()!=null){
				 result = sdf.format(cell.getDateCellValue());  
			 }
			return result;
		}else if(cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("m/d/yy")){
			 sdf = new SimpleDateFormat("yyyy/MM/dd");  
			 if(cell.getDateCellValue()!=null){
				 result = sdf.format(cell.getDateCellValue());  
			 }
			return result;
		}else if(cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("d-mmm-yy")){
			 sdf = new SimpleDateFormat("yyyy-MM-dd");  
			 result = sdf.format(cell.getDateCellValue());  
			return result;			
		}
		
		if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(cell.getBooleanCellValue());
		} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(cell.getNumericCellValue());
		} else{
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			result=String.valueOf(cell.getStringCellValue());
			result=result.replaceAll("\\t", "");
			return result;
		}
	}


如有不对之处, 欢迎给我留言交流~





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值