gpt4 book ai didi

java - report.xlsx 的文件格式和扩展名不匹配

转载 作者:行者123 更新时间:2023-12-02 18:32:22 25 4
gpt4 key购买 nike

Servlet 和 DAO 代码如下所示:

        @WebServlet("/ExcelExportController")
public class ExcelExportController extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ExcelExportController() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
if (session != null) {
FileOutputStream fileOut;
List<LoanOffersDTO> loanOffersDTOs;
ExcelExportDAO excelExportDAO;
HSSFWorkbook hssfWorkbook;
if (session.getAttribute("loanOffers") != null) {
loanOffersDTOs = (List<LoanOffersDTO>) session
.getAttribute("loanOffers");
excelExportDAO = new ExcelExportDAOImpl();
hssfWorkbook = excelExportDAO
.createLoanOffersXls(loanOffersDTOs);
if (hssfWorkbook != null) {
fileOut = new FileOutputStream("report.xlsx");
response.setContentType("application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition",
"attachment; filename=report.xlsx");
hssfWorkbook.write(fileOut);
fileOut.flush();
fileOut.close();
} else {

}
}
}

}

}

public class ExcelExportDAOImpl implements ExcelExportDAO {

@Override
public HSSFWorkbook createLoanOffersXls(List<LoanOffersDTO> loanOffersDTOs) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("LoanOffer");

HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("Bank Name");
headRow.createCell(1).setCellValue("Offer Name");
headRow.createCell(2).setCellValue("Loan Officer");
headRow.createCell(3).setCellValue("Telephone");
headRow.createCell(4).setCellValue("Email");
headRow.createCell(5).setCellValue("Interest Rate");
headRow.createCell(6).setCellValue("Period");
headRow.createCell(7).setCellValue("Pre-Payment");
headRow.createCell(8).setCellValue("Installment");
headRow.createCell(9).setCellValue("Loan details");

HSSFRow dataRow;
int rowCount = 1;
for (LoanOffersDTO loanOffersDTO : loanOffersDTOs) {
dataRow = sheet.createRow(rowCount++);
dataRow.createCell(0).setCellValue(loanOffersDTO.getBankName());
dataRow.createCell(1).setCellValue(loanOffersDTO.getOfferName());
dataRow.createCell(2).setCellValue(
loanOffersDTO.getLoanOfficerName());
dataRow.createCell(3).setCellValue(
loanOffersDTO.getBankerContactNum());
dataRow.createCell(4)
.setCellValue(loanOffersDTO.getBankerEmailId());
dataRow.createCell(5).setCellValue(loanOffersDTO.getInterestRate());
dataRow.createCell(6).setCellValue(loanOffersDTO.getDuration());
dataRow.createCell(7).setCellValue(
loanOffersDTO.getPrePaymentValue());
dataRow.createCell(8).setCellValue(loanOffersDTO.getInstallments());
dataRow.createCell(9).setCellValue(
loanOffersDTO.getLoanDescription());
}

FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Test\\Desktop\\report_"+Calendar.getInstance().getTimeInMillis()+".xls");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();

return workbook;
}
}

摘要:

我有一个jsp,当它提交时,上面的servlet/ Controller 被调用。这个sevelet将创建excel文件并刷新Excel。这里的 Excel 文件是使用“apache poi”创建的。 Excel 被创建并刷新,在尝试打开它时,我看到错误“文件格式和报告的扩展名.xlsx 不匹配”。没有数据。但是,如果我尝试将同一文件刷新到某个特定位置,我会完美地收到该文件。 我什至尝试过将内容类型设置为“application/vnd.ms-excel”,但问题是相同的。 谢谢

最佳答案

您似乎有两个问题。一种是您使用的 HSSF 代码生成 .xls 文件,但发送 .xlsx 样式响应。其次,您将工作簿写入服务器上的文件,而不是将其发送回客户端!

我建议将您的代码更改为通用代码以处理两者,例如

Workbook workbook;
....
workbook = excelExportDAO.createLoanOffersXls(loanOffersDTOs);
if (workbook != null) {
if (workbook instanceof HSSFWorkbook) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=report.xls");
} else {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition",
"attachment; filename=report.xlsx");
}
OutputStream out = response.getOutputStream();
hssfWorkbook.write(out);
out.close();
}

关于java - report.xlsx 的文件格式和扩展名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29705422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com