我正在编写一个程序,要求我写入Excel工作表。我已将文件拖入Eclipse项目中,并使用了" PrintWriter"写入文件的方法。我已经导入了必要的库,但是当我运行程序并按Enter键写入文件时,我没有收到错误,也没有按预期写入文件。有人能帮我吗?非常感谢。我的代码在这里:
JButton addClientButton = new JButton("Add");
addClientButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("Client Details.csv"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
sb.append(firstNameTextField.getText());
sb.append(',');
sb.append(lastNameTextField.getText());
sb.append(',');
sb.append(emailTextField.getText());
sb.append(',');
sb.append(phoneTextField.getText());
sb.append(',');
sb.append(weightTextField.getText());
sb.append(',');
sb.append(heightTextField.getText());
sb.append(',');
sb.append(ageSpinner.getValue());
sb.append(',');
sb.append(activityLevelComboBox.getSelectedItem());
pw.write(sb.toString());
pw.close();
}
});
addClientButton.setBounds(374, 335, 89, 23);
newClients.add(addClientButton);