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

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

IronXL 全面支援 AWS Lambda Function,適用於 .NET Standard 函式庫、Core 應用程式、.NET 5 及 .NET 6 專案。

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

將 AWS Toolkit 安裝至 Visual Studio 後,您即可建立 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 字串。這展示了如何利用 IronXL 在 AWS Lambda 函式中操作 Excel 檔案。

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

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

IronXL 能否在 AWS Lambda 函式中與 .NET 5 搭配使用?

是的,IronXL 完全支援 .NET 5 的 AWS Lambda 函式,以及 .NET Standard 函式庫、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 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 2,052,917 | 版本: 2026.6 just released
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronXL.Excel
執行範例 觀看您的資料變成試算表。