为什么我的程序会自动添加空格?

时间:2013-08-08 06:50:37

标签: spring csv apache-poi xssf hssf

我创建了一个包含一些数据的csv文件(| delimited),但是在每一行和每一行的末尾都会自动创建很少的行。 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();

0 个答案:

没有答案