简体   繁体   中英

Getting data from Excel and print count of rows into Excel using JAVA

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

Hi, I Have a Excel Sheet like above. I need to count all popular routes and print that count in No.of-Routes column on corresponding S_No row. Also I have an blank line after every S_No's. And these all popular routes are not placed in one cell(each route is one row). I tried with below code. I'm not able to move forward,please help me.

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;
                }

This worked for me. This depends on a blank line to indicate when to stop counting popular routes for a particular S_No. S_No and No.Of-Routes must be numeric fields.

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();
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM