IronXL 支援使用 .NET Core 的 AWS Lambda 函數

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronXL 完全支援 .NET 標準函式庫、核心應用程式、.NET 5 和 .NET 6 專案的 AWS Lambda 函數。

若要為 Visual Studio 新增 AWS Toolkit,請點擊此連結: 在 Visual Studio 的 AWS Toolkit 中使用 AWS Lambda 範本

在 Visual Studio 中安裝 AWS Toolkit 後,即可建立 AWS Lambda 函數專案。 您可以透過此連結學習如何使用 Visual Studio 建立 AWS Lambda 函數專案。

AWS Lambda 函數程式碼範例

建立新的 AWS Lambda 函數專案後,您可以嘗試以下程式碼片段:

using System;
using Amazon.Lambda.Core;
using IronXL;

// Ensure this attribute targets your Lambda function
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AWSLambdaIronXL
{
    public class Function
    {
        /// <summary>
        /// A simple function that takes a string input and processes it using IronXL.
        /// This specific example creates a new Excel workbook and fills cells with labeled values.
        /// </summary>
        /// <param name="input">The string input for the function</param>
        /// <param name="context">The Lambda context</param>
        /// <returns>A Base64 string representation of the Excel file</returns>
        public string FunctionHandler(string input, ILambdaContext context)
        {
            // Create a new workbook with ExcelFileFormat.XLS
            WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLS);

            // Create a new worksheet named "new_sheet"
            var newSheet = workBook.CreateWorkSheet("new_sheet");

            string columnNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            foreach (char col in columnNames)
            {
                for (int row = 1; row <= 50; row++)
                {
                    // Construct cell name and fill it with data
                    var cellName = $"{col}{row}";
                    newSheet[cellName].Value = $"Cell: {cellName}";
                }
            }

            // Convert the entire workbook to a byte array and then to a Base64 string
            return Convert.ToBase64String(workBook.ToByteArray());
        }
    }
}
using System;
using Amazon.Lambda.Core;
using IronXL;

// Ensure this attribute targets your Lambda function
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AWSLambdaIronXL
{
    public class Function
    {
        /// <summary>
        /// A simple function that takes a string input and processes it using IronXL.
        /// This specific example creates a new Excel workbook and fills cells with labeled values.
        /// </summary>
        /// <param name="input">The string input for the function</param>
        /// <param name="context">The Lambda context</param>
        /// <returns>A Base64 string representation of the Excel file</returns>
        public string FunctionHandler(string input, ILambdaContext context)
        {
            // Create a new workbook with ExcelFileFormat.XLS
            WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLS);

            // Create a new worksheet named "new_sheet"
            var newSheet = workBook.CreateWorkSheet("new_sheet");

            string columnNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            foreach (char col in columnNames)
            {
                for (int row = 1; row <= 50; row++)
                {
                    // Construct cell name and fill it with data
                    var cellName = $"{col}{row}";
                    newSheet[cellName].Value = $"Cell: {cellName}";
                }
            }

            // Convert the entire workbook to a byte array and then to a Base64 string
            return Convert.ToBase64String(workBook.ToByteArray());
        }
    }
}
Imports System
Imports Amazon.Lambda.Core
Imports IronXL

' Ensure this attribute targets your Lambda function
<Assembly: LambdaSerializer(GetType(Amazon.Lambda.Serialization.Json.JsonSerializer))>

Namespace AWSLambdaIronXL
	Public Class [Function]
		''' <summary>
		''' A simple function that takes a string input and processes it using IronXL.
		''' This specific example creates a new Excel workbook and fills cells with labeled values.
		''' </summary>
		''' <param name="input">The string input for the function</param>
		''' <param name="context">The Lambda context</param>
		''' <returns>A Base64 string representation of the Excel file</returns>
		Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As String
			' Create a new workbook with ExcelFileFormat.XLS
			Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)

			' Create a new worksheet named "new_sheet"
			Dim newSheet = workBook.CreateWorkSheet("new_sheet")

			Dim columnNames As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			For Each col As Char In columnNames
				For row As Integer = 1 To 50
					' Construct cell name and fill it with data
					Dim cellName = $"{col}{row}"
					newSheet(cellName).Value = $"Cell: {cellName}"
				Next row
			Next col

			' Convert the entire workbook to a byte array and then to a Base64 string
			Return Convert.ToBase64String(workBook.ToByteArray())
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

可用於部署的 IronXL NuGet 套件已記錄在我們的IronXL NuGet 安裝指南中。

常見問題解答

如何在 Visual Studio 中建立 AWS Lambda 函式專案?

若要在 Visual Studio 中建立 AWS Lambda 函式專案,請安裝 AWS Toolkit for Visual Studio。這可讓您在專案中使用 AWS Lambda 範本。您可以在 AWS 文件中找到詳細說明。

IronXL 在 AWS Lambda 中的應用範例是什麼?

IronXL 在 AWS Lambda 中的一個應用範例是建立一個新的 Excel 工作簿,將標示的資料填入其中,並傳回該 Excel 檔案的 Base64 字串。這展示了如何在 AWS Lambda 函式中使用 IronXL 來操作 Excel 檔案。

如何在 AWS Lambda 中使用 IronXL.Excel 將 Excel 工作簿轉換為 Base64?

在 AWS Lambda 函式中使用 IronXL,您可以建立工作簿,並使用 workBook.ToByteArray() 將其轉換為位元組陣列。然後,使用 Convert.ToBase64String() 取得工作簿的 Base64 字串表示。

IronXL for .NET 5 可以在 AWS Lambda 函式中使用嗎?

是的,IronXL 完全支援 AWS Lambda Functions for .NET 5,以及 .NET Standard Libraries、Core 應用程式和 .NET 6 專案。

哪裡可以找到使用 AWS Lambda 部署 IronXL 的資源?

使用 AWS Lambda 部署 IronXL 的資源可在 IronXL NuGet 安裝指南中找到,該指南提供了使用 IronXL NuGet 套件進行部署的說明文件。

在 AWS Lambda 中使用 IronXL 時,如何排除故障?

確保您已安裝正確版本的 IronXL 和 AWS Toolkit。檢閱程式碼是否有任何語法錯誤,並參閱 IronXL 的說明文件,以瞭解與 AWS Lambda 的相容性細節。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,738,553 | Version: 2025.11 剛發表