IronXL 支援使用 .NET Core 的 AWS Lambda 函數
IronXL完全支援AWS Lambda Function for .NET Standard 2.0程式庫、Core應用程式、.NET 8和.NET 10專案。
要新增AWS Toolkit for Visual Studio,請點擊此連結:使用AWS Toolkit for Visual Studio中的AWS Lambda範本。
在 Visual Studio 中安裝 AWS Toolkit 後,即可建立 AWS Lambda 函數專案。 您可以通過這個連結學習如何使用Visual Studio創建AWS Lambda Function專案。
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());
}
}
}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(input As String, 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
Next
' Convert the entire workbook to a byte array and then to a Base64 string
Return Convert.ToBase64String(workBook.ToByteArray())
End Function
End Class
End NamespaceImports 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(input As String, 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
Next
' 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安裝指南中。
常見問題
IronXL支援哪些.NET版本用於AWS Lambda函數?
IronXL支援AWS Lambda伺服器用於.NET Standard 2.0程式庫、Core應用程式、.NET 8和.NET 10專案。
如何使用IronXL開始AWS Lambda專案?
要使用IronXL開始AWS Lambda專案,首先安裝AWS Toolkit到Visual Studio以啟用專案建立。然後,按照Visual Studio的AWS Toolkit中AWS Lambda範本進行操作。
有使用IronXL與AWS Lambda的程式碼範例嗎?
是的,提供的程式碼範例展示了如何創建一個新的Excel工作簿、填入數據,並使用IronXL在AWS Lambda函數中返回一個Base64字串。
IronXL可以在AWS Lambda函數內創建Excel文件嗎?
可以,IronXL可以通過其功能在AWS Lambda函數內以程式方式構建Excel工作簿和工作表來創建Excel文件。
如何使用IronXL將Excel工作簿轉換為Base64字串?
您可以在IronXL中將Excel工作簿轉換為Base64字串,首先將工作簿轉換為位元組陣列,然後使用Convert.ToBase64String方法。
IronXL的AWS Lambda範例中'FunctionHandler'方法的作用是什麼?
IronXL的AWS Lambda範例中的'FunctionHandler'方法處理字串輸入,通過填入標籤值生成Excel工作簿,並將工作簿以Base64字串形式返回。
我可以使用什麼AWS資源來取得在AWS Lambda中部署IronXL專案的詳細說明?
可以在AWS的文件中找到名為AWS Toolkit for Visual Studio的AWS Lambda範本中IronXL專案作為AWS Lambda函數部署的詳細說明。
我可以去哪裡找到IronXL NuGet安裝指南?
IronXL NuGet安裝指南可在Iron Software網站的文檔部分找到,具體為IronXL NuGet安裝指南。
如何使用IronXL在Excel工作簿中創建新的工作表?
在IronXL中,您可以使用CreateWorkSheet方法在Excel工作簿中建立新的工作表。例如,var newSheet = workBook.CreateWorkSheet("new_sheet");將建立一個名為'new_sheet'的工作表。
使用IronXL與AWS Lambda的好處是什麼?
在AWS Lambda中使用IronXL可以無縫整合.NET應用程式與AWS雲端服務,允許在無伺服器環境中以程式方式創建和操作Excel文件。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。