2017-07-12 3 views
0
S_No Operators About No.Of-Busses Main-Routes No.Of-Routes Popular-Routes 
1  A-G-Holidays ***   10  Delhi - Haridwar    Delhi - Haridwar 
                     Delhi - Dehradun 
                     Delhi - Kanpur 
                     Delhi - Lucknow 
                     Delhi - Rishikesh 
                    Rishikesh - Delhi 
                     Kanpur - Lucknow 
                     Haridwar - Delhi 
                    Haridwar - Rishikesh 
                    Haridwar - Dehradun 
blank line----------------------------------------------------------------------- 

2  A-K-Travels ***   2           0       
                     Mumbai - Indore                    
                     Indore - Mumbai 

こんにちは、上記のExcelシートがあります。私はすべての一般的なルートを数え、対応するS_No行のNo.of-Routes列にその数を表示する必要があります。また、すべてのS_Noの後に空白行があります。そしてこれらのすべての一般的なルートは1つのセルに配置されません(各ルートは1つの行です)。 私は以下のコードを試しました。私は前進できません、助けてください。Excelからデータを取得し、JAVAを使用して行数をExcelに出力する

public class PrintNoOfRoutes 
{ 
public static void main(String args[]) throws Exception 
{ 
     List list=new ArrayList(); 
     FileInputStream file = new FileInputStream(new File("D:/BusOperators/sample.xlsx")); 
     XSSFWorkbook workbook = new XSSFWorkbook(file); 
     XSSFSheet sheet = workbook.getSheetAt(0); 
     Iterator<Row> rowIterator = sheet.iterator(); 
     rowIterator.next(); 
     Row row = rowIterator.next(); 
     int S_No=(int) row.getCell(0).getNumericCellValue(); 
     System.out.println(S_No); 
     Iterator<Cell> cellIterator = row.cellIterator();      
     while(cellIterator.hasNext()) 
      { 
       Cell cell = cellIterator.next();    
       switch(cell.getCellType()) 
       { 
        case Cell.CELL_TYPE_BOOLEAN:        

System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t"); 
         break; 
        case Cell.CELL_TYPE_NUMERIC: 
         int S_No=(int) row.getCell(0).getNumericCellValue(); 
          System.out.println(S_No); 
         break; 
        case Cell.CELL_TYPE_STRING: 
         //list.add(cell.getStringCellValue()); 
         if(c==S_No) 
          { 
           System.out.println("done"); 
           row.getCell(6);     

          Row row2 = rowIterator.next(); 

          if(cell.getStringCellValue() != null) 
          { 
           count=1; 
           System.out.println(count); 
           count++; 
          } 
         break; 
       } 
+0

は、実際には空白行ですか、空白行を含んでいますか?------------------------------- ---------------------------------------- '? – XtremeBaumer

+0

No.その空(空白行) –

答えて

0

これは私に役立ちました。これは、特定のS_Noの一般的なルートのカウントを停止する時期を示す空白行に依存します。 S_NoとNo.Of-Routesは数値フィールドでなければなりません。

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Iterator; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellType; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class PrintNoOfRoutes 
{ 
    private static final String INFILE_NAME = "C:\\sample.xlsx"; 
    private static final String OUTFILE_NAME = "C:\\sampleout.xlsx"; 

    public static void main(String[] args) { 

     try { 

      FileInputStream excelFile = new FileInputStream(new File(INFILE_NAME)); 
      Workbook workbook = new XSSFWorkbook(excelFile); 
      Sheet datatypeSheet = workbook.getSheetAt(0); 
      Iterator<Row> iterator = datatypeSheet.iterator(); 
      boolean firstTime = true; 
      int numRoutes = 0; 
      Cell routeCell = null; 
      while (iterator.hasNext()) { 

       Row currentRow = iterator.next(); 
       Iterator<Cell> cellIterator = currentRow.iterator(); 

       while (cellIterator.hasNext()) { 
        Cell currentCell = cellIterator.next(); 
        if (currentCell.getCellTypeEnum() == CellType.STRING && currentCell.getColumnIndex() == 6) { 
         //found a route to count 
         String val = currentCell.getStringCellValue(); 
         if (!firstTime) //don't count header row 
          numRoutes++; 
        } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC && currentCell.getColumnIndex() == 0) { 
         //found an S_No cell with a value 
         if (!firstTime) { 
          //set the number of routes for the previous S_No 
          routeCell.setCellValue(numRoutes); 
          numRoutes = 0; //reset 
         } else { 
          firstTime = false; 
         } 
         routeCell = currentRow.getCell(5); //save the No.Of-Routes cell for this S_No 
        } 
       } 
      } 

      //write last route count 
      routeCell.setCellValue(numRoutes); 

      //write out the workbook 
      File outfile = new File(OUTFILE_NAME); 
      FileOutputStream fos = new FileOutputStream(outfile); 
      workbook.write(fos); 
      workbook.close(); 
      fos.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
+0

ありがとうございます。追加の瓶が必要ですか? –

+0

私はちょうどあなたの例に従って、関連するジャーを使いました。 –

関連する問題