我正在制作一个创建 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);
}