【发布时间】:2014-07-10 00:22:00
【问题描述】:
我已将 .xls 转换为 csv 文件。我的代码运行良好。但我遇到了一个问题。就像某些单元格值有 (xxx,Md) 这些单元格值应该考虑一个值但取两列。如何在这里纠正问题,我附上了我的代码。
try
{
FileOutputStream fos = new FileOutputStream(outputFile);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
HSSFSheet sheet = workbook.getSheetAt(0);
for(int rowIndex = sheet.getFirstRowNum(); rowIndex <= sheet.getLastRowNum(); rowIndex++)
{
Cell cell=null;
Row row = null;
int previousCell = -1;
int currentCell = 0;
row = sheet.getRow(rowIndex);
for(int colIndex=row.getFirstCellNum(); colIndex < row.getLastCellNum(); colIndex++)
{
cell = row.getCell(colIndex);
currentCell = cell.getColumnIndex();
/* Cell processing starts here*/
System.out.println("coll"+colIndex);
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
cellDData.append(cell.getBooleanCellValue() + ",");
System.out.println("boo"+ cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell))
{
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCellValue = dateFormat.format(cell.getDateCellValue());
cellDData.append(strCellValue +",");
}
else
{
System.out.println(cell.getNumericCellValue());
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
String strCellValue1 = new String(longValue.toString());
cellDData.append(strCellValue1 +",");
}
break;
case Cell.CELL_TYPE_STRING:
String out=cell.getRichStringCellValue().getString();
cellDData.append(cell.getRichStringCellValue().getString() + ",");
System.out.println("string"+cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
cellDData.append("" +",");
System.out.print("THIS IS BLANK");
break;
default:
break;
}
}
}
}
【问题讨论】:
标签: java excel csv apache-poi