以下C#程式碼可用於生成Excel類型的報告：
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using GemBox.Spreadsheet;
using SharpCompress.Compressors.ADC;

namespace ProSmart.ReportingServices
{
    internal class ExcelReportGenerator 
    {
        public SaveOptions _saveOptions { get; set; } = SaveOptions.XlsxDefault;
        private ExcelFile _workbook;
        public ExcelReportGenerator(Stream template)
        {
            _workbook = ExcelFile.Load(template, LoadOptions.XlsxDefault);
            FontSettings.FontsBaseDirectory = "Fonts";
            FontSettings.FontsBaseResourceLocation = "/Fonts/";
            _workbook.Styles[BuiltInCellStyleName.Normal].Font.Name = "NotoSansHK";
        }

        public ExcelReportGenerator()
        {
            _workbook = new ExcelFile();
            FontSettings.FontsBaseDirectory = "Fonts";
            FontSettings.FontsBaseResourceLocation = "/Fonts/";
            var workSheet = _workbook.Worksheets.Add("Raw Data");
            workSheet.Parent.Styles.Normal.Font.Name = "NotoSansHK";
        }

        public ExcelReportGenerator(string fileType)
        {
            _workbook = new ExcelFile();
            FontSettings.FontsBaseDirectory = "Fonts";
            FontSettings.FontsBaseResourceLocation = "/Fonts/";
            var workSheet = _workbook.Worksheets.Add("Raw Data");
            workSheet.Parent.Styles.Normal.Font.Name = "NotoSansHK";

            if (fileType == "pdf")
                _saveOptions = SaveOptions.PdfDefault;
            else if(fileType == "xlsx")
                _saveOptions = SaveOptions.XlsxDefault;
        }

        public void Save(MemoryStream outputStream)
        {
            _workbook.Save(outputStream, _saveOptions);
            outputStream.Position = 0;
        }

        public void CreateHeader(List<string> headers, string sheetName = "Raw Data")
        {
            var rawDataSheet = _workbook.Worksheets[sheetName];

            // Write the headers to the first row
            for (int i = 0; i < headers.Count; i++)
            {
                rawDataSheet.Cells[0, i].Value = headers[i]; // Row 0, Column i
            }

            // Auto fit requires Drawing dll. This dll requires gdi+ which does not support in non windows after .net 6.
            //for (int j = 0; j < headers.Count; j++)
            //{
            //    rawDataSheet.Columns[j].AutoFit();
            //}
        }


        public void FillTable(List<List<string>> data, List<Type> colTypes, string sheetName = "Raw Data")
        {
            var rawDataSheet = _workbook.Worksheets[sheetName];
            rawDataSheet.PrintOptions.Portrait = false;
            rawDataSheet.PrintOptions.FitWorksheetWidthToPages = 1;
           
            var lastRow = rawDataSheet.Rows.Count;
            if (lastRow > 2)
            {
                rawDataSheet.Cells.GetSubrange("A2", $"Z{lastRow}").Clear(ClearOptions.Content);
            }
            for (int rowIndex = 0; rowIndex < data.Count; rowIndex++)
            {
                var row = data[rowIndex];
                for (int colIndex = 0; colIndex < row.Count; colIndex++)
                {
                    Type targetType = colTypes[colIndex];
                    if (row[colIndex] != null)
                    {
                        if (!string.IsNullOrEmpty(row[colIndex]))
                        {
                            var valObj = Convert.ChangeType(row[colIndex], targetType);
                            rawDataSheet.Cells[rowIndex + 1, colIndex].Value = valObj;
                        }
                    }
                }
            }
        }
    }
}


我希望將以下程式碼改寫成分批將數據寫入至同一Excel中以減少記憶體的資源佔用，而非將所有數據預載於RAM中：
var collection = mongoClient.GetDatabase("SupplierServices")
                .GetCollection<BsonDocument>("ProSupplierMasterInfo");
_helper = new ReportingServicesDbTransactionHelper(_dbContext);
using (var cursor = await collection.Find(filter).SortByDescending(x => x["DateModified"]).ToCursorAsync())
{
    var entities = new List<ProSupplierMasterInfoEntityDto>();

    while (await cursor.MoveNextAsync())
    {
        var batch = cursor.Current;
        foreach (var doc in batch)
        {
            entities.Add(BsonSerializer.Deserialize<ProSupplierMasterInfoEntityDto>(doc));
        }
    }
    var xlsgen = new ExcelReportGenerator();
    if (entities != null)
    {
        FillExcel(xlsgen, entities);
    }
    using (MemoryStream outputStream = new MemoryStream())
    {
        xlsgen.Save(outputStream);
        var proFileDto = await SendToMinio(outputStream, "SupplierMaster.xlsx");
        return proFileDto;
    }
}