データのJFrameを介して入力されたを使用してExcelにそれぞれのフィールドに保存されます。書き込みデータは、Java ApacheのPOI
public class Customer {
private String name;
private String Email;
private String Address;
private String Phone;
public Customer(String name, String email, String address, String phone) {
super();
this.name = name;
Email = email;
Address = address;
Phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}...
は、データをCustomer.classからデータを取得するためにsopposedされ、このクラスを使用してExcelファイルに渡されます。
public class C {
public static void main() {
try {
File file = new File("C:\\Users\\Sumuel\\Desktop\\Query.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("C.info");
//Create Heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Name");
rowHeading.createCell(1).setCellValue("Email");
rowHeading.createCell(2).setCellValue("Address");
rowHeading.createCell(3).setCellValue("Phone");
for (int i = 0; i < 4; i++){
CellStyle styleHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
styleHeading.setFont(font);
rowHeading.getCell(i).setCellStyle(styleHeading);
}
for(int i = 0; i<4; i++){
sheet.autoSizeColumn(i);
}
Customer c1;
Row custInfo = sheet.createRow(1);
custInfo.createCell(0).setCellValue(c1.getName());
custInfo.createCell(1).setCellValue(c1.getEmail());
custInfo.createCell(0).setCellValue(c1.getAddress());
custInfo.createCell(1).setCellValue(c1.getPhone());
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Sumuel\\Desktop\\Query.xls"));
workbook.write(out);
out.close();
workbook.close();
System.out.println("file written");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
私の主な問題は、1つのクラスからExcelシートに正しくデータを渡し、完了したらそのクラスを呼び出して実行することです。 何か助けていただければ幸いです。
問題がある場合は、ここをクリックしてください。 –
c1に初期値なしで 'Customer c1;'を使用していて、それを使用して値を取得すると、==> nullポインタ例外、あるタイプの顧客の価値をこの 'c1'に渡す必要があります –
@TuyenNguyen、あなたはより具体的なことができますか(まだまだ新しいですか) –