Wsparcie IronXL dla funkcji AWS Lambda z .NET Core
IronXL w pełni wspiera funkcję AWS Lambda dla bibliotek .NET Standard, aplikacji Core oraz projektów .NET 5 i .NET 6.
Aby dodać AWS ToolKit do Visual Studio, odwiedź ten link: Using the AWS Lambda Templates in the AWS Toolkit for Visual Studio.
Zainstalowanie AWS Toolkit w Visual Studio pozwala na stworzenie projektu funkcji AWS Lambda. Możesz nauczyć się, jak stworzyć projekt funkcji AWS Lambda używając Visual Studio przez ten link.
Przykład działającego kodu funkcji AWS Lambda
Po utworzeniu nowego projektu funkcji AWS Lambda możesz wypróbować ten fragment kodu:
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
Pakiety NuGet IronXL dostępne do wdrożeń są opisane w naszym przewodniku instalacji IronXL NuGet.
Często Zadawane Pytania
Jak utworzyć projekt funkcji AWS Lambda w Visual Studio?
Aby utworzyć projekt funkcji AWS Lambda w Visual Studio, zainstaluj zestaw narzędzi AWS Toolkit for Visual Studio. Umożliwi to korzystanie z szablonów AWS Lambda w Twoich projektach. Szczegółowe instrukcje znajdziesz w dokumentacji AWS.
Jaki jest przykład zastosowania IronXL w AWS Lambda?
Przykładem zastosowania IronXL w AWS Lambda jest utworzenie nowego skoroszytu Excel, wypełnienie go oznaczonymi danymi i zwrócenie ciągu Base64 pliku Excel. Pokazuje to, w jaki sposób IronXL może być wykorzystywany do manipulowania plikami Excel w ramach funkcji AWS Lambda.
Jak przekonwertować skoroszyt Excel na Base64 w AWS Lambda przy użyciu IronXL?
Korzystając z IronXL w ramach funkcji AWS Lambda, można utworzyć skoroszyt i przekonwertować go na tablicę bajtów za pomocą metody workBook.ToByteArray(). Następnie należy użyć metody Convert.ToBase64String(), aby uzyskać reprezentację skoroszytu w postaci ciągu znaków Base64.
Czy IronXL może być używany z .NET 5 w funkcjach AWS Lambda?
Tak, IronXL for .NET w pełni obsługuje funkcje AWS Lambda dla .NET 5, a także biblioteki .NET Standard, aplikacje Core i projekty .NET 6.
Gdzie mogę znaleźć zasoby dotyczące wdrażania IronXL z AWS Lambda?
Materiały dotyczące wdrażania IronXL z AWS Lambda można znaleźć w przewodniku instalacji IronXL NuGet, który zawiera dokumentację dotyczącą korzystania z pakietów IronXL NuGet do wdrożeń.
Jak rozwiązywać problemy podczas korzystania z IronXL w AWS Lambda?
Upewnij się, że masz zainstalowaną właściwą wersję IronXL i AWS Toolkit. Sprawdź kod pod kątem błędów składniowych i zapoznaj się z dokumentacją IronXL, aby uzyskać szczegółowe informacje na temat zgodności z AWS Lambda.

