gpt4 book ai didi

java - 在使用 poi 读取 Excel 文件之前保存其内容

转载 作者:行者123 更新时间:2023-12-01 04:26:48 25 4
gpt4 key购买 nike

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

最佳答案

不知道我理解是否正确。这能解决您的问题吗

我<行号

关于java - 在使用 poi 读取 Excel 文件之前保存其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18424731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com