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 Function 專案?

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

IronXL 在 AWS Lambda 的一個應用程式示例是建立新的 Excel 活頁簿,填充其標記資料,並返回 Excel 檔案的 Base64 字串。這樣做展示了如何在 AWS Lambda 函數中使用 IronXL 來操作 Excel 檔案。

IronXL 在 AWS Lambda 中的一個示例應用是創建一個新的 Excel 工作簿,填充標籤數據,並返回 Excel 文件的 Base64 字符串。這演示了 IronXL 如何在 AWS Lambda 函數中用於處理 Excel 文件。

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

在 AWS Lambda 函數中使用 IronXL,您可以創建一個工作簿並使用 workBook.ToByteArray() 將其轉換為字節數組。然後,使用 Convert.ToBase64String() 獲取工作簿的 Base64 字符串表示。

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

是的,IronXL 完全支持 .NET 5 的 AWS Lambda 函數,還有 .NET 標準庫、核心應用程序和 .NET 6 項目。

使用 AWS Lambda 部署 IronXL 的資源可以在 IronXL NuGet 安裝指南中找到,該指南提供了有關如何使用 IronXL NuGet Packages 進行部署的文檔。

有關在 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 bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 1,846,091 | 版本: 2026.2 剛剛發布