使用Java将数据动态插入Excel

时间:2015-01-15 01:19:02

标签: java excel apache-poi

我是Java的新手,我在将动态数据插入Excel文件时遇到问题。这是以下代码。如果我删除我的Excel文件并重新运行我的程序。然后它将创建一个新文件并插入以下数据。(你好,再见,真,日期)。在第一次运行时,程序仍然可以插入以下数据,但是当我执行下一次运行时,数据不能是存储到下一行。这是检查文件存在的以下代码。我希望我可以找人帮助我,因为我正在努力使用这段代码几天。

    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 个答案:

答案 0 :(得分:1)

  1. 首先阅读excel文件!
  2. 如果您通过
    成功获得工作表 worksheet = workbook.getSheet(&#34; POI工作表&#34;);
    然后关闭文件输入流。
  3. 如果您无法获得工作表,请通过
    创建一个 workbook = new HSSFWorkbook();
    worksheet = workbook.createSheet(&#34; POI工作表&#34;);
  4. 你有现有或新的wooksheet 追加行并将其写入文件并通过
    关闭文件输出流 fout = new FileOutputStream(file);
    workbook.write(FOUT);
    fout.close();
  5. 我希望这可以帮到你。祝你有愉快的一天: - )