IronXL 支持使用 .NET Core 的 AWS Lambda 函数
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可用于部署的 IronXL NuGet 包已记录在我们的IronXL NuGet 安装指南中。
常见问题解答
如何在 Visual Studio 中创建 AWS Lambda 功能项目?
要在 Visual Studio 中创建 AWS Lambda 功能项目,请安装 Visual Studio 的 AWS 工具包。这使您能够为您的项目使用AWS Lambda模板。您可以在AWS文档中找到详细的说明。
IronXL在AWS Lambda中的应用示例是什么?
IronXL在AWS Lambda中的一个应用示例是创建一个新的 Excel 工作簿,用标签数据填充它,并返回 Excel 文件的 Base64 字符串。这展示了如何在AWS Lambda函数中使用IronXL来操作Excel文件。
如何使用 IronXL 在 AWS Lambda 中将 Excel 工作簿转换为 Base64?
在 AWS Lambda 函数中使用 IronXL,您可以创建一个工作簿并使用 workBook.ToByteArray() 将其转换为字节数组。然后使用 Convert.ToBase64String() 获取工作簿的 Base64 字符串表示。
IronXL 可以用于 AWS Lambda 函数与 .NET 5 吗?
是的,IronXL 完全支持用于 .NET 5、.NET Standard 库、Core 应用程序和 .NET 6 项目的 AWS Lambda 函数。
我在哪里可以找到有关使用 IronXL 部署 AWS Lambda 的资源?
有关使用 IronXL 部署 AWS Lambda 的资源可以在 IronXL NuGet 安装指南中找到,该指南提供了有关如何使用您的部署的 IronXL NuGet 包的文档。
当在 AWS Lambda 中使用 IronXL 时,我如何解决问题?
确保您安装了正确版本的 IronXL 和 AWS 工具包。检查代码是否有任何语法错误,并参考 IronXL 的文档以了解与 AWS Lambda 的兼容性细节。






