java - How to download an excel file in Spring RestController

Java - How to download an excel file in Spring RestController

To download an Excel file in a Spring REST controller, you can use the ResponseEntity class to return the file as a response. Here's how you can do it:

import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @RestController public class ExcelDownloadController { @GetMapping("/download/excel") public ResponseEntity<byte[]> downloadExcel() throws IOException { // Create sample Excel workbook Workbook workbook = new XSSFWorkbook(); // Add data to workbook if needed // Write workbook to ByteArrayOutputStream ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); workbook.close(); // Set response headers HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "example.xlsx"); // Return the byte array of the Excel file return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK); } } 

In this example:

  • We have a REST controller with a GET mapping for /download/excel.
  • Inside the method, we create a sample Excel workbook using Apache POI (XSSFWorkbook in this case). You can add data to the workbook as needed.
  • We write the workbook to a ByteArrayOutputStream.
  • We set the response headers, including the content type (application/octet-stream) and content disposition (attachment with the filename example.xlsx).
  • Finally, we return a ResponseEntity<byte[]> with the byte array of the Excel file, the headers, and the HTTP status HttpStatus.OK.

When a client accesses the /download/excel endpoint, the browser will prompt the user to download the Excel file named example.xlsx.

Examples

  1. "Java Spring RestController download excel file example"

    • Description: This query seeks an example of how to download an Excel file from a Spring RestController in Java.
    @GetMapping("/downloadExcel") public ResponseEntity<InputStreamResource> downloadExcelFile() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  2. "Java Spring RestController export excel file"

    • Description: Users search for ways to export or download an Excel file from a Spring RestController in Java.
    @GetMapping("/exportExcel") public ResponseEntity<InputStreamResource> exportExcelFile() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  3. "Java Spring RestController download Excel file from server"

    • Description: This query targets methods to download an Excel file from a server using a Spring RestController in Java.
    @GetMapping("/downloadExcelFromServer") public ResponseEntity<InputStreamResource> downloadExcelFromServer() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Read your Excel file from server File excelFile = new File("path/to/your/excel.xlsx"); FileInputStream fileInputStream = new FileInputStream(excelFile); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(fileInputStream)); } 
  4. "Java Spring RestController serve Excel file"

    • Description: Users look for methods to serve an Excel file for download using a Spring RestController in Java.
    @GetMapping("/serveExcel") public ResponseEntity<InputStreamResource> serveExcelFile() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  5. "Java Spring RestController download Excel file with REST API"

    • Description: This query aims to download an Excel file using a REST API provided by a Spring RestController in Java.
    @GetMapping("/downloadExcelWithAPI") public ResponseEntity<InputStreamResource> downloadExcelWithAPI() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  6. "Java Spring RestController return Excel file"

    • Description: Users seek ways to return an Excel file for download from a Spring RestController in Java.
    @GetMapping("/returnExcel") public ResponseEntity<InputStreamResource> returnExcelFile() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  7. "Java Spring RestController serve Excel file with REST API"

    • Description: This query looks for methods to serve an Excel file for download using a REST API provided by a Spring RestController in Java.
    @GetMapping("/serveExcelWithAPI") public ResponseEntity<InputStreamResource> serveExcelWithAPI() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  8. "Java Spring RestController download Excel file from URL"

    • Description: Users seek methods to download an Excel file from a URL using a Spring RestController in Java.
    @GetMapping("/downloadExcelFromURL") public ResponseEntity<InputStreamResource> downloadExcelFromURL() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Fetch your Excel file from URL URL url = new URL("your_excel_file_url"); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(inputStream)); } 
  9. "Java Spring RestController download Excel file with custom name"

    • Description: This query targets methods to download an Excel file with a custom name using a Spring RestController in Java.
    @GetMapping("/downloadExcelWithCustomName") public ResponseEntity<InputStreamResource> downloadExcelWithCustomName() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "custom_filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 
  10. "Java Spring RestController export Excel file with REST API"

    • Description: Users seek methods to export or download an Excel file using a REST API provided by a Spring RestController in Java.
    @GetMapping("/exportExcelWithAPI") public ResponseEntity<InputStreamResource> exportExcelWithAPI() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel")); headers.setContentDispositionFormData("attachment", "filename.xlsx"); // Generate your Excel file ByteArrayInputStream excelStream = generateExcelFile(); return ResponseEntity .ok() .headers(headers) .body(new InputStreamResource(excelStream)); } 

More Tags

browser-tab libusb nsenumerator nullpointerexception entity-framework-4.1 dbscan azure-pipelines-build-task addition tab-completion pdfsharp

More Programming Questions

More Genetics Calculators

More Statistics Calculators

More Trees & Forestry Calculators

More Dog Calculators