Prise en charge d'IronXL pour les fonctions AWS Lambda avec .NET Core
IronXL prend entièrement en charge les fonctions AWS Lambda pour les bibliothèques .NET Standard, les applications Core, les projets .NET 5 et .NET 6. Utilisez IronXL dans AWS Lambda pour créer des fichiers Excel, lire des données de feuilles de calcul et même convertir Excel en PDF sans dépendances externes.
Pour ajouter AWS Toolkit à Visual Studio, suivez ce lien : Utilisation des modèles AWS Lambda dans AWS Toolkit pour Visual Studio .
L'installation d'AWS Toolkit dans Visual Studio vous permet de créer un projet de fonction AWS Lambda. Vous pouvez apprendre à créer un projet de fonction AWS Lambda à l'aide de Visual Studio en suivant ce lien .
Exemple de code fonctionnel pour une fonction AWS Lambda
Après avoir créé un nouveau projet de fonction AWS Lambda, vous pouvez essayer cet extrait de code :
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
Les packages NuGet IronXL disponibles pour les déploiements sont documentés dans notre guide d'installation NuGet IronXL .
Questions Fréquemment Posées
Comment puis-je créer un projet de fonction AWS Lambda dans Visual Studio?
Pour créer un projet de fonction AWS Lambda dans Visual Studio, installez AWS Toolkit pour Visual Studio. Cela vous permet d'utiliser les modèles AWS Lambda pour vos projets. Vous pouvez trouver des instructions détaillées dans la documentation AWS.
Quel est un exemple d'application d'IronXL dans AWS Lambda?
Un exemple d'application d'IronXL dans AWS Lambda est la création d'un nouveau classeur Excel, son remplissage avec des données étiquetées et le retour d'une chaîne Base64 du fichier Excel. Cela démontre comment IronXL peut être utilisé pour manipuler des fichiers Excel au sein des fonctions AWS Lambda.
Comment puis-je convertir un classeur Excel en Base64 dans AWS Lambda à l'aide d'IronXL?
En utilisant IronXL au sein d'une fonction AWS Lambda, vous pouvez créer un classeur et le convertir en un tableau d'octets avec workBook.ToByteArray(). Ensuite, utilisez Convert.ToBase64String() pour obtenir une représentation de chaîne Base64 du classeur.
IronXL peut-il être utilisé avec .NET 5 dans les fonctions AWS Lambda?
Oui, IronXL prend entièrement en charge les fonctions AWS Lambda for .NET 5, ainsi que les bibliothèques .NET Standard, les applications Core et les projets .NET 6.
Où puis-je trouver des ressources pour déployer IronXL avec AWS Lambda?
Des ressources pour déployer IronXL avec AWS Lambda se trouvent dans le guide d'installation IronXL NuGet, qui fournit une documentation sur l'utilisation des IronXL NuGet Packages pour vos déploiements.
Comment puis-je dépanner les problèmes lors de l'utilisation d'IronXL dans AWS Lambda?
Assurez-vous d'avoir la version correcte d'IronXL et AWS Toolkit installée. Vérifiez le code pour toute erreur de syntaxe et consultez la documentation d'IronXL pour des détails de compatibilité avec AWS Lambda.

