我创建了一个 csv 文件(| 分隔),其中包含一些数据,但在每行和每列的末尾自动创建了几行。excel 文件中只有 4 行,但是当它转换为 csv 时,它显示 8。
4|sdfa@sdf.nb|plplpl|plplp|1988-11-11|M|asdasd@sdf.ghgh|sdfsadfasdfasdfasdfasdf|asdfasdf|34253242|234234|true|true|||||
1|@gmail.com||kumar|1988-07-10|M|alternate@gmail.com|asfdsfsfsaf|sfdasf|7204722222|7204711111|true|false|||||
2|adad@sdf.in||asdd|1989-09-11|F|sdfa@sad.no|sjdfgjkdghjkdhgjjkjhjk|skflsdfhjskfh|1212121212|4343434343|false|true|||||
3|adads@sdf.in|asdasd|asdsd|1985-09-11|F|sdfsa@sad.no|sjdfgjkasasdghjkdhgjjkjhasask|skflsdfhjskfh|234234234|4343434343|false|true|||||
||||||||||||||||
||||||||||||||||
||||||||||||
||||||||
转换代码:
HSSFWorkbook wBook = new HSSFWorkbook(new FileInputStream(path));
// Get first sheet from the workbook
HSSFSheet sheet = (HSSFSheet) wBook.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
// Iterate through each rows from first sheet
Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator();
rowIterator.next();
while (rowIterator.hasNext()) {
row = (HSSFRow) rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = (HSSFCell) cellIterator.next();
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
if(HSSFDateUtil.isCellDateFormatted(cell))
{
String dobStr = DateUtil.reformatAnyDateTime(cell.getDateCellValue(), DateUtil.POSTGRESQL_DATE_FORMAT);
data.append(dobStr + "|");
}else
data.append((long)(cell.getNumericCellValue()) + "|");
}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
{
data.append(cell.getBooleanCellValue() + "|");
} else if(cell.getCellType() == Cell.CELL_TYPE_STRING)
{
data.append(cell.getStringCellValue() + "|");
}
else if(cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
data.append("|");
}
else
{
data.append("Invalid");
}
}data.append("\r\n");
}
fos.write(data.toString().getBytes());
fos.close();