每次在JAVA中调用函数时,在excel中添加行

时间:2016-03-15 09:44:33

标签: java excel file apache-poi

我正在使用此代码在Excel工作表的末尾添加行。每次调用函数。初始化时last=0的位置。

connectionTime, mobility, angleofArrival, sender and peer

是一些变量,其值随函数调用而改变。当调用函数时,我希望在每一行中保存值。

public void excelWriter(DTNHost sender, DTNHost peer){
         String fileName = "Results.xls";      

            try {

                            File file = new File(fileName);
                            FileInputStream fin = null;  
                            HSSFWorkbook workbook = null;  
                            HSSFSheet worksheet = null;  
                            FileOutputStream fout = null;
                            POIFSFileSystem lPOIfs = null;
                            if (file.exists()) {
                                try{
                                    fout = new FileOutputStream(fileName, true);
                                    fin = new FileInputStream(fileName);
                                    lPOIfs = new POIFSFileSystem(fin);
                                    workbook = new HSSFWorkbook(lPOIfs);
                                    worksheet = workbook.getSheet("POI Worksheet");

                                    for (int i=0; i<workbook.getNumberOfSheets(); i++) {
                                        System.out.println( workbook.getSheetName(i) );                                    
                                    }
                                    HSSFSheet sheet = workbook.getSheetAt(0);
                                    last = sheet.getLastRowNum();
                                }catch (IOException e) {  
                                    e.printStackTrace();  
                                }catch (NullPointerException e){
                                    e.printStackTrace(); 
                                }
                            } else {
                                //create new file
                                try{
                                fout = new FileOutputStream(file);                            
                                }catch (IOException e) {  
                                    e.printStackTrace();  
                                }
                                workbook = new HSSFWorkbook();                       
                                worksheet = workbook.createSheet("POI Worksheet");  
                            }

                            if(last != 0){
                                last = worksheet.getLastRowNum();
                            }else{

                                last = worksheet.getLastRowNum()+1;
                                System.out.println(last);

                            }

                HSSFRow row = worksheet.createRow(last++);                  
                HSSFCell cellA1 = row.createCell((int)0);
                cellA1.setCellValue(sender+" and "+peer);               
                HSSFCell cellB1 = row.createCell((int) 1);
                cellB1.setCellValue(mobility);      
                HSSFCell cellC1 = row.createCell((int) 2);
                cellC1.setCellValue(angleOfArival);
                HSSFCell cellD1 = row.createCell((int) 3);
                cellD1.setCellValue(connectionTime);    
                System.out.println("Data written");
                workbook.write(fout);
                fout.flush();

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




           ////End
    }

但它只添加一行(仅限第一次)。我已经完成了它的调试。每次调用函数时都会增加last值,但每次都不会添加行。任何人都可以告诉我这里做错了什么。

1 个答案:

答案 0 :(得分:0)

public void excelWriter(DTNHost sender, DTNHost peer) {
    String fileName = "Results.xls";
    int last = 0;
    try {

        File file = new File(fileName);
        FileInputStream fin = null;
        HSSFWorkbook workbook = null;
        HSSFSheet worksheet = null;
        FileOutputStream fout = null;
        POIFSFileSystem lPOIfs = null;
        if (file.exists()) {
            try {

                fin = new FileInputStream(fileName);
                lPOIfs = new POIFSFileSystem(fin);
                workbook = new HSSFWorkbook(lPOIfs);
                worksheet = workbook.getSheet("POI Worksheet");
                last = worksheet.getLastRowNum() + 1;

                fout = new FileOutputStream(fileName);

            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        } else {
            // create new file
            try {

                fout = new FileOutputStream(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            workbook = new HSSFWorkbook();
            worksheet = workbook.createSheet("POI Worksheet");

        }
        HSSFRow row = worksheet.createRow(last);
        HSSFCell cellA1 = row.createCell((int) 0);
        cellA1.setCellValue(sender + " and " + peer);
        HSSFCell cellB1 = row.createCell((int) 1);
        cellB1.setCellValue("mobility");
        HSSFCell cellC1 = row.createCell((int) 2);
        cellC1.setCellValue("angleOfArival");
        HSSFCell cellD1 = row.createCell((int) 3);
        cellD1.setCellValue("connectionTime");
        System.out.println("Data written");
        workbook.write(fout);
        fout.flush();
        fout.close();

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

    //// End
}

上面的方法有效,一旦加载工作表就打开OutputStream,删除行HSSFSheet sheet = workbook.getSheetAt(0);,因为它是冗余的,你已经有了可以从中获取lastRowNum的工作表实例。语句{{1}使用post increment operator,所以它会继续写入同一行,&#34; last&#34;只有在执行此语句后,变量才会增加。