我尝试了以下代码。
但是它会覆盖现有的工作表。
File f= new File(System.getProperty("user.dir")+"\\src\\test\\resources\\Exceldata.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet= workbook.createSheet("Sheet4");
HSSFRow row = worksheet.createRow(1);
HSSFCell cell= row.createCell(1);
cell.setCellValue("admin");
enter code here
workbook.write(f);
workbook.close();
答案 0 :(得分:0)
使用FileInputStream代替XSSFWorkbook的File和and对象 希望此功能对您有帮助,
public static void write(){
try
{
FileInputStream myxls = new FileInputStream(System.getProperty("user.dir")+"\\src\\test\\resources\\Exceldata.xls" );
HSSFWorkbook studentsSheet = new HSSFWorkbook(myxls);
workbook = new XSSFWorkbook(myxls );
workbook.createSheet(sheetname);
HSSFSheet worksheet = studentsSheet.getSheetAt(0);
a=worksheet.getLastRowNum();
System.out.println(a);
Row row = worksheet.createRow(++a);
row.createCell(1).setCellValue("");
myxls.close();
FileOutputStream output_file =new FileOutputStream(new File(System.getProperty("user.dir")+"\\src\\test\\resources\\Exceldata.xls"));
//write changes
workbook.write(output_file );
studentsSheet.write(output_file);
output_file.close();
System.out.println(" is successfully written");
}
尝试从主要方法调用此函数,
public static void main(String args[])
{
write();
}
可能的Append Data in existing Excel file using apache poi in java的副本和 How to add new sheets to existing excel workbook using apache POI?
答案 1 :(得分:0)
我终于找到了解决方案。我曾经使用过poi jar 4.0,但无法成功写入数据。然后,我将jar版本降级为3.14,并且运行正常