您的位置:首页 > 其它

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

2016-06-18 19:42 357 查看
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;
}
}


如有不对之处, 欢迎给我留言交流~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: