使用 Java 将数据动态插入 Excel

Inserting data Dynamically into Excel using Java

我是 Java 的新手,在将动态数据插入我的 Excel 文件时遇到问题。这是以下代码。如果我删除我的 excel 文件并重新 运行 我的程序。然后它将创建一个新文件并插入以下数据。(hello,goodbye,true,date)。在第一个 运行 期间,程序仍然可以插入以下数据,但是当我执行下一个 运行,数据不能存入下一行。这是检查文件是否存在的以下代码。我希望有人能帮助我,因为我已经为这段代码苦苦挣扎了几天。

    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()+1;
    }else{
          last = worksheet.getLastRowNum();
    }

这是写入函数的完整代码:

    public void writeExcel(){
    String fileName = "C:\Users\blslyeoh\Documents\NetBeansProjects\JavaApplication1\poi-test.xls";       
    try {
                    int last=0;
                    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");  
                    }
        // index from 0,0... cell A1 is cell(0,0)
                    if(last != 0){
                        last = worksheet.getLastRowNum()+1;
                    }else{
                        last = worksheet.getLastRowNum();
                    }
                    HSSFRow row = worksheet.createRow(last);                        
        HSSFCell cellA1 = row.createCell((short)0);
        cellA1.setCellValue("hello");   
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        //cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
        //cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //cellA1.setCellStyle(cellStyle);
        HSSFCell cellB1 = row.createCell((short) 1);
        cellB1.setCellValue("goodbye");
        //cellStyle = workbook.createCellStyle();
        //cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
        //cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        //cellB1.setCellStyle(cellStyle);
        HSSFCell cellC1 = row.createCell((short) 2);
        cellC1.setCellValue(true);

        HSSFCell cellD1 = row.createCell((short) 3);
        cellD1.setCellValue(new Date());
        cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(HSSFDataFormat
                .getBuiltinFormat("m/d/yy h:mm"));
        cellD1.setCellStyle(cellStyle);
        workbook.write(fout);
        fout.flush();

        fout.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
  1. 先阅读excel文件!
  2. 如果通过
    成功获取工作表 工作表 = workbook.getSheet("POI Worksheet");
    然后关闭文件输入流。
  3. 如果您无法获得工作表,请通过
    创建一个 workbook = new HSSFWorkbook();
    工作表 = workbook.createSheet("POI Worksheet");
  4. 您有现有的或新的工作表。
    附加行并将其写入文件并通过
    关闭文件输出流 fout = new FileOutputStream(文件);
    workbook.write(fout);
    fout.close();

希望对您有所帮助。祝你有美好的一天:-)