在使用poi读取之前保存excel文件的内容

时间:2013-08-25 01:13:22

标签: java excel user-interface apache-poi

我正在创建一个创建2个新电子表格文件的应用程序。这个想法是让用户在这个excel文件中输入数据并让程序读取它。但问题是,在输入并保存数据后(按下excel中的保存图标)程序只是看不到这些数据,它仍然没有输入任何内容。我需要的是以某种方式使用poi以programmicaly方式保存它。有什么建议?

以下是代码:

我的gui课程:

public class GUI extends JFrame{

private Panel buttonHolder;
private Button crossReference;
private Button generateHPD;
private CrossReference ref = null;
private XSSFWorkbook log;
private XSSFWorkbook sheet;
private XSSFSheet logSheet;
private XSSFSheet sSheet;

public GUI () throws Exception{
    this.setTitle("NYWM Cross Reference Application");
    this.setSize(400,100);
    this.setVisible(true);
    this.setLocationRelativeTo(null);

    buttonHolder = new Panel (new BorderLayout());
    this.add(buttonHolder);

    crossReference = new Button ("CrossReference");
    generateHPD = new Button ("Generate HPD");

    buttonHolder.add(crossReference, BorderLayout.NORTH);
    buttonHolder.add(generateHPD, BorderLayout.SOUTH);

    logSheet =  createExcelSheet ("D:/Log.xlsx", "Log");
    sSheet =    createExcelSheet("D:/Spreadsheet.xlsx", "Spreadsheet");

    crossReference.addActionListener(new crossReferenceButtonListener());
    generateHPD.addActionListener(new generateHPDButtonListener());
}



public XSSFSheet getLogSheet() {
    return logSheet;
}



public XSSFSheet getsSheet() {
    return sSheet;
}



private XSSFSheet createExcelSheet (String path, String fileName) throws Exception{
    try{
    FileOutputStream out = new FileOutputStream (path);
    XSSFWorkbook wb = new XSSFWorkbook ();
    XSSFSheet sheet = wb.createSheet(fileName);
    wb.write(out);

    Process p = Runtime.getRuntime().exec(
            "rundll32 url.dll, FileProtocolHandler " + path);

    return sheet;
    }
    catch (Exception e){
        throw e;
    }
}

private class crossReferenceButtonListener implements ActionListener {
    public void actionPerformed (ActionEvent event){

        try { 
            XSSFSheet sSheet = getsSheet();
            XSSFSheet logSheet = getLogSheet();

            ref = new CrossReference();
            //ref = new CrossReference (log.getSheetAt(0), sheet.getSheetAt(0));
            ref.CrossReferenceCont(logSheet, sSheet);


            ref.CrossRef();
            JOptionPane.showMessageDialog(null, "Cross Reference was successful!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

CrossReferenceCont方法

public void CrossReferenceCont(XSSFSheet wsLog, XSSFSheet wsSheet)
        throws Exception {

    log = new PropertyList(wsLog);
    sheet = new PropertyList(wsSheet);

    logArray = log.getPropertyList();
    sheetArray = log.getPropertyList();
}

属性列表方法:

public PropertyList(XSSFSheet ws) throws Exception{

    try{
        //File excel = new File (excelFilePath);
        //FileInputStream fis = new FileInputStream (excel);
    //  XSSFWorkbook wb = new XSSFWorkbook(fis);


        int rowNum = ws.getLastRowNum();
        System.out.println ("Number of rows is " + rowNum);
        propertyList = new ArrayList <Property>(rowNum);

        for (int i = 0; i <= rowNum; i++){
            property  = new Property (ws, i);

            propertyList.add(new Property(property));

        }

最后是属性方法:此处程序崩溃

public Property(XSSFSheet sheet, int row) throws FileNotFoundException {

    Cell cell = sheet.getRow(row).getCell(0); //crashes here, nullPointer

    int type = cell.getCellType();

    //setting the address-------------------------------------------------
    if (type == Cell.CELL_TYPE_STRING) {
        address = cell.getStringCellValue();
    } else if (type == Cell.CELL_TYPE_NUMERIC) {
        int addressTemp = (int) cell.getNumericCellValue();
        address = String.valueOf(addressTemp);
    }

2 个答案:

答案 0 :(得分:0)

不确定我是否理解正确。这是否解决了您的问题

我&lt;的rowNum

答案 1 :(得分:0)

而不是getSheet(),创建一个getFilePath()。然后使用该路径获取工作表。

File file = new File(filePath);
Workbook wb = WorkbookFactory.create(FileUtils.openInputStream(file));
Sheet templateSheet = templateWorkbook.getSheetAt(0);