PNG
JPG
BMP
TIFF
DOCX
編輯 XLSX C# 中的格式
使用伺服器端 Aspose.Cells for .NET API 進行本機高效能 XLSX 文件編輯,無需使用 Microsoft 或 Adobe PDF 等任何軟體。
如何使用 C# 編輯 XLSX 文件
為了編輯 XLSX 文件,我們將使用Aspose.Cells for .NETAPI這是一個功能豐富、功能強大且易於使用的API,適用於C#平台的任何編輯器。打開NuGet套件管理器,搜尋Aspose.Cells並安裝。您也可以從套件管理器控制台使用以下命令。
命令
PM> Install-Package Aspose.Cells
編輯 C# 中的 XLSX 文件的步驟
基本的文檔編輯 Aspose.Cells for .NET 只需幾行程式碼即可完成 API。
- 在類別檔案中包含命名空間
- 加載您的範例文件。
- 新增並存取工作簿的新工作表。
- 取得工作表的所需儲存格並將值放入儲存格中。
- 插入資料透視表並設定樣式
- 使用 Save 方法將工作簿另存為 XLSX 檔案。
系統需求
我們的 API 在所有主要平台和作業系統上均受支援。在執行下面的程式碼之前,請確保您的系統符合以下先決條件。
- Microsoft Windows 或具有 .NET Framework、.NET Core、Windows Azure、Mono 或 Xamarin 平台的相容作業系統
- 開發環境如Microsoft Visual Studio
- 在專案中新增對 Aspose.Cells for .NET DLL 的引用 - 使用上面的下載按鈕從 NuGet 安裝
編輯 XLSX 文件 - C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//load your sample file | |
Workbook book = new Workbook("sample.xlsx"); | |
//add new worksheet | |
Worksheet sheet = book.Worksheets.Add("NewSheet"); | |
Cells cells = sheet.Cells; | |
//add some data | |
cells[0, 0].Value = "fruit"; | |
cells[1, 0].Value = "grape"; | |
cells[2, 0].Value = "blueberry"; | |
cells[3, 0].Value = "kiwi"; | |
cells[4, 0].Value = "cherry"; | |
cells[5, 0].Value = "grape"; | |
cells[6, 0].Value = "blueberry"; | |
cells[7, 0].Value = "kiwi"; | |
cells[8, 0].Value = "cherry"; | |
cells[0, 1].Value = "year"; | |
cells[1, 1].Value = 2020; | |
cells[2, 1].Value = 2020; | |
cells[3, 1].Value = 2020; | |
cells[4, 1].Value = 2020; | |
cells[5, 1].Value = 2021; | |
cells[6, 1].Value = 2021; | |
cells[7, 1].Value = 2021; | |
cells[8, 1].Value = 2021; | |
cells[0, 2].Value = "amount"; | |
cells[1, 2].Value = 50; | |
cells[2, 2].Value = 60; | |
cells[3, 2].Value = 70; | |
cells[4, 2].Value = 80; | |
cells[5, 2].Value = 90; | |
cells[6, 2].Value = 100; | |
cells[7, 2].Value = 110; | |
cells[8, 2].Value = 120; | |
PivotTableCollection pivots = sheet.PivotTables; | |
//Add a PivotTable | |
int pivotIndex = pivots.Add("=NewSheet!A1:C9", "A12", "TestPivotTable"); | |
PivotTable pivot = pivots[pivotIndex]; | |
//Add PivotField for Rows area | |
pivot.AddFieldToArea(PivotFieldType.Row, "fruit"); | |
//Add PivotField for Columns area | |
pivot.AddFieldToArea(PivotFieldType.Column, "year"); | |
//Add PivotField for Values area | |
pivot.AddFieldToArea(PivotFieldType.Data, "amount"); | |
//Set the style of PivotTable | |
pivot.PivotTableStyleType = PivotTableStyleType.PivotTableStyleMedium9; | |
//Refresh and calculate data of PivotTable | |
pivot.RefreshData(); | |
pivot.CalculateData(); | |
book.Save("out.xlsx"); |
Aspose.Cells API 可用於建立、編輯、轉換和渲染 Microsoft Excel 格式為不同的格式。此外,它還可用於軟體應用程式中的全面圖表、可擴展報告和可靠計算。 Aspose.Cells 是一個獨立的 API,它不需要任何像 Microsoft 或 OpenOffice 這樣的軟體。