1 回答

TA貢獻1798條經(jīng)驗 獲得超3個贊
嘗試這個:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
ExcelWriter excelWriter = new ExcelWriter();
List<Book> listBook = excelWriter.getListBook();
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xls";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=temp.xls");
OutputStream out = response.getOutputStream();
try (FileInputStream in = new FileInputStream(file)) {
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
}
out.flush();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
如果您只想創(chuàng)建 excel,則無需使用響應(yīng)。并且文件名應(yīng)該是 xlsx 類型。這就是您收到兼容模式消息的原因。
try {
String excelFilePath = "C:\\Users\\A7369241\\Desktop\\Temp.xlsx";
ExcelWriter excelWriter = new ExcelWriter();
List<Book> listBook = excelWriter.getListBook();
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
} catch (Exception e) {
System.out.println(e.getMessage());
}
如果您仍然有問題,則意味著您的 servlet 無法正常工作。只需嘗試使用 main 方法和 main 方法類型創(chuàng)建類:
String excelFilePath = "D:\\Temp.xls";
Test excelWriter = new Test();
List<Book> listBook = excelWriter.getListBook();
try {
excelWriter.writeExcel(listBook, excelFilePath);
System.out.println("Excel file written successfully");
} catch (IOException e) {
e.printStackTrace();
}
添加回答
舉報