java - 每次在JAVA中调用函数时在Excel中添加行

标签 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 值都会增加,但不会每次都添加行。谁能告诉我我在这里做错了什么。

最佳答案

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的工作表实例。语句HSSFRow row = worksheet.createRow(last++);使用后增量运算符,因此它将继续写入同一行,“last”变量的值将递增仅在该语句执行后。

关于java - 每次在JAVA中调用函数时在Excel中添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007625/

相关文章:

java - Android 中读取文件的速度很慢

java - 为什么 Json 无法为 java 枚举正确生成?

excel - 检查 VBA 数组中是否已存在数字

vb.net - 如何使用 Visual Studio 2010 创建的 Excel 加载项单击在 Excel 中创建的自定义上下文菜单时执行操作

java - 寻找在 Java 服务器之间传输大文件的好方法

c - 如何处理动态分配的 struct C 中的段错误

java - Java 中的 Azure Function 无法在本地运行

java - 将 ImageView 边缘淡入透明

arrays - 你可以直接将excel VBA中的数组内容写入CSV文件吗?

c - 将文件中的特定单词存储到数组中 (c)