英文:
Apache POI / new row in Excel with one button press
问题
以下是你的代码的中文翻译部分:
我目前正在尝试编写一个可以更好地组织你的工作时间的程序。因此,我使用Apache POI来创建一个Excel文件。我已经相对进展较多,但在某一点上无法继续前进。我的问题是,你每天输入工时,程序将其写入相应的Excel文件。但是,如果我想输入下一天,程序会覆盖Excel文件并删除前一天的内容。
所以我的问题是我不知道如何在下面的行中写入数据。
我已经尝试过FileInputStream,但因为我每个月都创建一个新文件,所以它并没有真正起作用。
这是我用于创建和写入Excel文件的代码。
public void speichern(ActionEvent event) throws Exception {
LocalDate localDate = date.getValue();
datum = String.valueOf(localDate);
do {
if(btnNextMon.isArmed()){
monClick++;
System.out.println(monClick);
}
if (btnSave.isArmed()){
saveClick++;
}
try {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Stundenabrechnung");
sheet.setDefaultColumnWidth(18);
Map<String, Object[]> data = new TreeMap<>();
data.put("1", new Object[]{"DATUM:", " INS. ABG. STUNDEN:", " ABGR. STUNDEN:", " BESCHREIBUNG:"});
data.put("2", new Object[]{datum, ergbnis + "0", LHabg.getText(), taBes.getText()});
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
FileOutputStream outputStream = new FileOutputStream("Stundenabrechnung" + monClick + ".xlsx");
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ExcelFile is created succsessfully");
} while (btnNextMon.isPressed());
}
希望这对你有所帮助!
英文:
I am currently trying to write a program that can better organize your working hours. So I use Apache POI to create an Excel file. I'm already relatively far, but I can't get any further at one point. My problem is that you enter your hours per day and the program writes them in the corresponding Excel file. However, if I want to enter the next day, the program overwrites the Excel file and the previous day is deleted.
So my problem is that I don't know how to write in the rows underneath.
I've already tried FileInputStream, but it didn't really work because I create a new file every month.
This is my code for creating and writing the Excel file.
public void speichern(ActionEvent event) throws Exception {
LocalDate localDate = date.getValue();
datum = String.valueOf(localDate);
do {
if(btnNextMon.isArmed()){
monClick++;
System.out.println(monClick);
}
if (btnSave.isArmed()){
saveClick++;
}
try {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Stundenabrechnung");
sheet.setDefaultColumnWidth(18);
Map<String, Object[]> data = new TreeMap<>();
data.put("1", new Object[]{"DATUM:", " INS. ABG. STUNDEN:", " ABGR. STUNDEN:", " BESCHREIBUNG:"});
data.put("2", new Object[]{datum, ergbnis + "0", LHabg.getText(), taBes.getText()});
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
FileOutputStream outputStream = new FileOutputStream("Stundenabrechnung" + monClick +".xlsx");
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ExcelFile is created succsessfully");
}while (btnNextMon.isPressed());
}
thanks for the help
答案1
得分: 1
我认为在创建新的书和电子表格之前,你应该检查文件是否已经存在。
你可以使用类似以下的代码:
编辑: 我已经更改了引发问题的那一行
try {
boolean fileExists = new File("Stundenabrechnung" + monClick + ".xlsx").exists();
XSSFWorkbook workbook;
XSSFSheet sheet;
if (fileExists) {
workbook = new XSSFWorkbook(new FileInputStream(new File("Stundenabrechnung" + monClick + ".xlsx")));
sheet = workbook.getSheetAt(0);
Map<String, Object[]> data = new TreeMap<>();
data.put("3", new Object[]{datum, ergbnis + "0", LHabg.getText(), taBes.getText()});
Set<String> keyset = data.keySet();
int rownum = sheet.getLastRowNum() + 1;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
} else {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Stundenabrechnung");
sheet.setDefaultColumnWidth(18);
Map<String, Object[]> data = new TreeMap<>();
data.put("1", new Object[]{"DATUM:", " INS. ABG. STUNDEN:", " ABGR. STUNDEN:", " BESCHREIBUNG:"});
data.put("2", new Object[]{datum, ergbnis + "0", LHabg.getText(), taBes.getText()});
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
}
FileOutputStream outputStream = new FileOutputStream("Stundenabrechnung" + monClick + ".xlsx");
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
英文:
I think you should check if the file already exists before creating a new book and spreadsheet
You could use something like:
EDIT: I changed the line that was causing the problem
try {
boolean fileExists = new File("Stundenabrechnung" + monClick +".xlsx").exists();
XSSFWorkbook workbook;
XSSFSheet sheet;
if (fileExists) {
workbook = new XSSFWorkbook(new FileInputStream(new File("Stundenabrechnung" + monClick +".xlsx")));
sheet = workbook.getSheetAt(0);
Map<String, Object[]> data = new TreeMap<>();
data.put("3", new Object[]{datum, ergbnis + "0", LHabg.getText(), taBes.getText()});
Set<String> keyset = data.keySet();
int rownum = sheet.getLastRowNum() + 1;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
} else {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Stundenabrechnung");
sheet.setDefaultColumnWidth(18);
Map<String, Object[]> data = new TreeMap<>();
data.put("1", new Object[]{"DATUM:", " INS. ABG. STUNDEN:", " ABGR. STUNDEN:", " BESCHREIBUNG:"});
data.put("2", new Object[]{datum, ergbnis + "0", LHabg.getText(), taBes.getText()});
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
XSSFRow row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
XSSFCell cell = row.createCell(cellnum++);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cell.setCellStyle(cellStyle);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Integer) {
cell.setCellValue((Integer) obj);
}
}
}
}
FileOutputStream outputStream = new FileOutputStream("Stundenabrechnung" + monClick +".xlsx");
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论