由java程序在excel表中按数字分隔值

时间:2014-06-17 12:00:48

标签: java excel collections

嗨,我有一张excel表我需要用数字分隔值,如果数字的值改变它应该在下一行程序

示例

    1234.456  hello.jsp
    1234.456  hello.jsp
    1234.457  hello.jsp
    1234.457  hello.jsp
    1234.459  hello.jsp

我需要输出

    1234.456 hello.jsp,1234.456 hello.jsp
    1234.457 hello.jsp,1234.457 hello.jsp
    1234.459 hello.jsp

如何分隔值并在控制台中打印?我试过了

XSSFWorkbook wb = new XSSFWorkbook((fs));

        // Get first sheet from the workbook
        XSSFSheet sheet = wb.getSheetAt(0);

        Row row;
        Cell cell;

        // Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();
       int i=0;
        while (rowIterator.hasNext()) 
        {
                row = rowIterator.next();

                // For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();

                String value="";

                while (cellIterator.hasNext()) 
                {
                cell = cellIterator.next();

                switch (cell.getCellType()) 
                {
                case Cell.CELL_TYPE_STRING:
                        //pw.print(cell.getStringCellValue()+   "  ");

                    value=value+" ";
                    value=value+cell.getStringCellValue();

                    //System.out.print(cell.getStringCellValue()+   "  ");
                //System.out.println();
                        break;

                }
                }
                strArray[i]=value;
                i++;
               //System.out.println();
               // pw.flush();

        }
        for(String tsr:strArray)
        System.out.println(tsr);

我正在

    1234.456  hello.jsp
    1234.456  hello.jsp
    1234.457  hello.jsp
    1234.457  hello.jsp
    1234.459  hello.jsp

在Java中,我现在如何分离它并在控制台中打印上述输出值?

1 个答案:

答案 0 :(得分:0)

 // Get first sheet from the workbook
        XSSFSheet sheet = wb.getSheetAt(0);
        double pre=0.0,next=0.0;
        String valueStr = " ";
        for(Row row : sheet){
            if(row.getRowNum()==0){
                for (Cell cell : row) {
                    int i = cell.getColumnIndex();
                    switch (i) {
                    // column 0 
                    case 0:
                        pre = cell.getNumericCellValue();
                        break;
                    // column 1
                    case 1:
                        valueStr = cell.getStringCellValue();
                        System.out.print(pre+" "+valueStr+",");
                        break;
                    }
            }
            }else{
                for (Cell cell : row) {
                    int i = cell.getColumnIndex();
                    switch (i) {
                    // column 0
                    case 0:
                        next = cell.getNumericCellValue();
                        break;
                    // column 1
                    case 1:
                        valueStr = cell.getStringCellValue();
                        if(pre == next)
                            System.out.print(next+" "+valueStr+" ");
                        else
                            System.out.println();
                            System.out.print(next+" "+valueStr+",");
                        pre = next;
                        break;
                    }
                    }
            }
        }