apache poi在一个existing excel文件的特定单元格上设置一个值

时间:2016-08-28 14:24:25

标签: java apache apache-poi

Here is my excel file

我的java代码上有一个函数会打开一个excel文件,我想要做的就是点击打印它会打开带有我文本字段值的excel文件。 (以下是我的代码)and Here is what im getting 帮助将真的很感激。提前谢谢

btnPrint = new JButton("Print");
    btnPrint.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try{

                FileInputStream input_document = new FileInputStream(new File("FormTemplates\\Book1.xls"));   
                HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
                HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 

                Cell cell = null;

                cell = my_worksheet.getRow(1).getCell(2);
                cell.setCellValue((String)txtNo.getText());
                cell = my_worksheet.getRow(2).getCell(2);
                cell.setCellValue((String)txtNo.getText());
                cell = my_worksheet.getRow(3).getCell(2);
                cell.setCellValue((String)txtNo.getText());

                input_document.close();


                FileOutputStream output_file =new FileOutputStream(new File("C:\\Users\\Popoy\\Desktop\\Poy ITS\\FORMTEMPLATES\\UpdateXLS.xls"));
                my_xls_workbook.write(output_file); 
                output_file.close();
            }
            catch(Exception q){
                q.printStackTrace();

            }
        }
    });

0 个答案:

没有答案