Preview:
package com.example.codelearning.api;

import com.aspose.cells.*;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@RestController
@RequestMapping("excel")
public class ExcelApi {

    @GetMapping("/download")
    public ResponseEntity<Resource> downloadExcel() throws Exception {

        Workbook workbook = new Workbook();
        Worksheet worksheet = workbook.getWorksheets().get(0);

        // Tạo dữ liệu và biểu đồ
        createChartData(worksheet);

        // Lưu workbook vào ByteArrayOutputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.save(outputStream, SaveFormat.XLSX);

        InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        org.apache.poi.ss.usermodel.Workbook workbookApache = WorkbookFactory.create(inputStream);

        workbookApache.removeSheetAt(1);
        outputStream.reset();
        workbookApache.write(outputStream);

        // Chuẩn bị tệp Excel để tải xuống
        ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=chart_example.xlsx")
                .body(resource);
    }

    private void createChartData(Worksheet worksheet) {
        // Tạo dữ liệu mẫu
        Cells cells = worksheet.getCells();
        cells.get("A1").setValue("Month test test test");
        cells.get("A2").setValue("Oct");
        cells.get("A3").setValue("Nov");

        cells.get("B1").setValue("Toyota");
        cells.get("B2").setValue(32);
        cells.get("B3").setValue(42);

        cells.get("C1").setValue("VinFast");
        cells.get("C2").setValue(100);
        cells.get("C3").setValue(125);

        Range range = worksheet.getCells().createRange("A1:C3");
        Style style = worksheet.getWorkbook().createStyle();
        style.setBorder(BorderType.TOP_BORDER, CellBorderType.THIN, Color.getBlack());
        style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THIN, Color.getBlack());
        style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THIN, Color.getBlack());
        style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THIN, Color.getBlack());
        range.setStyle(style);

        // Đặt chiều rộng cho cột A
        Column columnA = worksheet.getCells().getColumns().get(0);
        columnA.setWidth(25);

        // Tạo biểu đồ
        int chartIndex = worksheet.getCharts().add(ChartType.LINE_WITH_DATA_MARKERS, 5, 0, 15, 5);
        Chart chart = worksheet.getCharts().get(chartIndex);

        chart.getNSeries().add("B2:B3", true);
        chart.getNSeries().get(0).setName("Toyota");
        chart.getNSeries().add("C2:C3", true);
        chart.getNSeries().get(1).setName("VinFast");

        chart.getNSeries().setCategoryData("A2:A3");
        PlotArea plotArea = chart.getPlotArea();
        plotArea.getArea().setFormatting(FormattingType.NONE);
        plotArea.getBorder().setTransparency(1.0);

        Title title = chart.getTitle();
        title.setText("Biểu đồ phân tích");
        Font font = title.getFont();
        font.setSize(16);
        font.setColor(Color.getLightGray());

        chart.getChartArea().setWidth(400);
        chart.getChartArea().setHeight(300);
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter