Hi

C# Example with ClosedXML and S3 Upload

 using System;

using System.Data;

using System.IO;

using ClosedXML.Excel;

using Amazon;

using Amazon.S3;

using Amazon.S3.Transfer;

using System.Threading.Tasks;


class Program

{

    static async Task Main()

    {

        // 1. Create a sample DataTable

        var dt = new DataTable("SampleData");

        dt.Columns.Add("ID", typeof(int));

        dt.Columns.Add("Name", typeof(string));

        dt.Rows.Add(1, "Alice");

        dt.Rows.Add(2, "Bob");


        // 2. Save to Excel using ClosedXML

        string excelFilePath = "output.xlsx";

        using (var workbook = new XLWorkbook())

        {

            var worksheet = workbook.Worksheets.Add(dt, "Sheet1");

            workbook.SaveAs(excelFilePath);

        }


        Console.WriteLine("Excel file created.");


        // 3. Upload to AWS S3

        string bucketName = "your-s3-bucket-name";

        string keyName = "folder/output.xlsx"; // path in S3


        var s3Client = new AmazonS3Client(

            "your-access-key",

            "your-secret-key",

            RegionEndpoint.APSouth1 // Use your AWS region

        );


        var fileTransferUtility = new TransferUtility(s3Client);


        await fileTransferUtility.UploadAsync(excelFilePath, bucketName, keyName);


        Console.WriteLine("✅ File uploaded to AWS S3.");

    }

}


Previous
Next Post »