我使用以下代码生成了一个 Excel 文件:
try {
FileOutputStream out = new FileOutputStream(new File("D:\\new1.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
}
那么这个文件写好后怎么下载呢?
更新1:
我没有使用servlet。我正在使用 struts2 和这个类来生成带有 poi.jar 的 Excel 文件。
public class ExportToExcel{
public void execToexcel() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("1", new Object[] {"Emp No.", "Name", "Salary"});
...
}
}
try {
FileOutputStream out =
new FileOutputStream(new File("D:\\new1.xls"));
workbook.write(out);
DownloadManager.downloadFile("out");
//out.flush();
// Runtime.getRuntime().exec("new1.xls");
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}