有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Apache POI和Angular在下载并打开excel文件后给出奇怪字符

我正在使用springboot和angular开发将数据下载到excel的功能。在springboot中,我使用ApachePOI依赖项。但当它下载并打开excel文件中显示的奇怪字符时,如下所示

enter image description here

我不知道怎么解决这个问题

以下是我在后端-Spring引导中的代码:

try {
      HSSFWorkbook workbook = new HSSFWorkbook();
      HSSFSheet sheet = workbook.createSheet("sheet1");

      //Row 1
      HSSFRow row1 = sheet.createRow(0);
      HSSFCell cellA1 = row1.createCell(0);
      cellA1.setCellValue("Nation Bank");

      //Row 2
      HSSFRow row2 = sheet.createRow(1);
      HSSFCell cellA2 = row2.createCell(0);
      cellA2.setCellValue("Seattle");

     
      //Row 11
      HSSFRow row11 = sheet.createRow(10);
      HSSFCell cellA11 = row11.createCell(0);
      cellA11.setCellValue("Debits");
      HSSFCell cellB11 = row10.createCell(1);
      cellB11.setCellValue(900000);

      //Row 12
      HSSFRow row12 = sheet.createRow(12);
      HSSFCell cellA12 = row12.createCell(0);
      cellA12.setCellValue("Closing Balance");
      HSSFCell cellB12 = row12.createCell(1);
      cellB12.setCellValue(100000);


    //output Excel file
      OutputStream output =response.getOutputStream();
      response.reset();
      response.setHeader("Content-disposition", "attachment; filename=details.xls");
      response.setContentType("application/msexcel");
      workbook.write(output);
      output.close();
    } catch (final Throwable th) {
      try {
        logger.error(th.getMessage(), th);
        response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), th.getMessage());
      } catch (final IOException e) {
        logger.error("Could not send error message!", e);
      }
    }

这是我在前端的代码:

downloadBankStatement(startDate: string, endDate: string, accountNumber: string, customerIdentifier: string) {
    const dateRange = buildDateRangeParam(startDate, endDate);
    this.downloadService.getText(`${this.baseUrl}/accounts/bankstatement/export?dateRange=${dateRange}&startdate=${startDate}&endDate=${endDate}&accountIdentifier=${accountNumber}&customerIdentifier=${customerIdentifier}`)
      .subscribe();
  }

在下载服务中:

public getText(url: string, params?: HttpParams): Observable<string | null> {
    this.progressService.start();

    return this.http.get(url, {params, observe: 'response', responseType: 'text'})
      .pipe(
        tap((response: HttpResponse<string>) => {
          const blob = new Blob([response.body]);
          const fileName = this.extractFileName(response.headers);
          this.downloadBlob(blob, fileName);
        }),
        map((response: HttpResponse<string>) => response.body),
        catchError(() => Observable.empty()),
        finalize(() => this.progressService.stop())
      );
  }

我错过什么了吗


共 (0) 个答案