gpt4 book ai didi

java - 如何打开从java arraylist导出的excel文件

转载 作者:行者123 更新时间:2023-11-30 08:23:37 24 4
gpt4 key购买 nike

我有一个 Web 项目,其中显示来自数据库的 GridView 。 Arraylist 名称是 leadSchoolList。所以,我保留了一个按钮,当单击它时,它会在 Struts 操作类中运行一个方法(名为:-actionExportToExcel)。现在我可以将列表元素导出到 excel。

但我面临的问题是在窗口中打开导出的 excel 工作表。因此在 actionExportToExcel 中调用了另一个方法(名为:-open)。但是我不知道我哪里错了,所以任何人都可以帮助我吗?

public String actionExportToExcel(){
try {
FileOutputStream fileOut = new FileOutputStream("D:/poi-test.xls");

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");


leadSchoolList = leadSchoolService.getAllLeadSchool();
for(int rowNum = 0; rowNum < leadSchoolList.size(); rowNum++){
HSSFRow row = sheet.createRow(rowNum);

//for(int colNum = 0; colNum < 5; colNum++ ){
HSSFCell cell1 = row.createCell(0);
LeadSchool leadSchool = leadSchoolList.get(rowNum);
cell1.setCellValue(leadSchool.getLeadSchool_String_LSchool_ID());

HSSFCell cell2 = row.createCell(1);
cell2.setCellValue(leadSchool.getLeadSchool_String_Name());

HSSFCell cell3 = row.createCell(2);
cell3.setCellValue(leadSchool.getLeadSchool_String_Address());

HSSFCell cell4 = row.createCell(3);
cell4.setCellValue(leadSchool.getLeadSchool_String_Phone_No());

HSSFCell cell5 = row.createCell(4);
cell5.setCellValue(leadSchool.getLeadSchool_String_Remarks());
System.out.println("Successfully exported to Excel");






}
try {
wb.write(fileOut);

// fileOut.flush();
open(fileOut);
//fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
addActionError("The process cannot access the file because it is being used by another process");
e.printStackTrace();
}

return SUCCESS;
}

private void open(FileOutputStream f) {

String[] cmd = new String[4];
try{
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = "start";
cmd[3] = "D:/poi-test.xls";

Runtime.getRuntime().exec(cmd);
//Runtime.getRuntime().exec("cmd.exe /c start D:/poi-test.xls");
System.out.print("file opened");
}
catch(Exception e){
e.printStackTrace();
}
}

最佳答案

幸运的是我自己找到了解决方案。 .First 需要运行 flush() 和 close() 方法,然后是 second(named:-open) 方法。在 open 方法中,而不是 R

Runtime.getRuntime().exec(cmd);

我把它扩展成两行:-

Runtime run = Runtime.getRuntime();
Run.exec(cmd);

就是这样..:D

关于java - 如何打开从java arraylist导出的excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533886/

24 4 0