簡體   English   中英

使用poi讀取之前保存excel文件的內容

[英]save contents of excel file before reading it using poi

我正在制作一個創建2個新電子表格文件的應用程序。 這個想法是讓用戶將數據輸入到該excel文件中,並使程序讀取它。 但是問題是,在輸入並保存數據后(按excel中的保存圖標),程序只是看不到該數據,就好像沒有在此輸入任何數據一樣。 我需要的是以某種方式使用poi將其保存在程序中。 有什么建議么?

這是代碼:

我的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);
    }

不知道我是否正確理解。 這能解決您的問題嗎

我<rowNum

代替getSheet(),創建一個getFilePath()。 然后使用該路徑獲取工作表。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM