使用JAVA从Excel获取数据并将行数打印到Excel中

时间:2017-07-12 07:33:30

标签: java excel

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行的Noof-Routes列中打印该计数。每个S_No后我都有一个空行。并且这些所有流行路线都不放在一个单元格中(每条路线都是一行)。 我试过下面的代码。我无法继续前进,请帮助我。

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

1 个答案:

答案 0 :(得分: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();
        }
    }

}