入门
安装
通过NuGet安装Aspose.Cells
NuGet是下载和安装Aspose.Cells for .NET最简单的方式。
- 打开Microsoft Visual Studio和NuGet软件包管理器。
- 搜索"aspose.cells"来找到所需的Aspose.Cells for .NET。
- 点击"安装",Aspose.Cells for .NET将被下载并引用到您的项目中。
您也可以从nuget网页上为aspose.cells下载它: Aspose.Cells for .NET NuGet包
在Windows上安装Aspose.Cells
- 从以下页面下载Aspose.Cells.msi: 下载Aspose.Cells.msi
- 双击Aspose Cells msi并按照指示进行安装:
在 Linux 安装 Aspose.Cells
在这个示例中,我使用 Ubuntu 操作系统来展示如何在 Linux 上开始使用 Aspose.Cells。
- 创建一个 .netcore 应用程序,名为 “AsposeCellsTest”。
- 打开文件 “AsposeCellsTest.csproj” ,并添加以下行用于引用 Aspose.Cells 包:
<ItemGroup> <PackageReference Include="Aspose.Cells" Version="24.7" /> </ItemGroup>
- 使用 VSCode 在 Ubuntu 上打开项目:
- 使用以下代码运行测试:
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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET Aspose.Cells.Workbook wb1 = new Aspose.Cells.Workbook(); wb1.Worksheets.Add("linux"); wb1.Worksheets["linux"].Cells[0, 0].Value = "Using Aspose Cells on linux with VS Code."; wb1.Save("linux.xlsx");
注意:Aspose.Cells For .NetStandard 能够支持您在 Linux 上的需求。
适用于: NetStandard2.0, NetCore2.1, NetCore3.1, Net5.0, Net6.0 及更高版本。
在 MAC OS 上安装 Aspose.Cells
在这个示例中,我使用 macOS High Sierra 操作系统来展示如何在 MAC OS 上开始使用 Aspose.Cells。
- 创建一个 .netcore 应用程序,名为 “AsposeCellsTest”。
- 用 Visual Studio for Mac 打开应用程序,然后通过 NuGet 安装 Aspose Cells:
- 使用以下代码运行测试:
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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET Aspose.Cells.Workbook wb1 = new Aspose.Cells.Workbook(); wb1.Worksheets.Add("macOS"); wb1.Worksheets["macOS"].Cells[0, 0].Value = "Using Aspose Cells on macOS with Visual Studio For MAC."; wb1.Save("macOS.xlsx"); - 如果您需要使用与绘图相关的功能,请在 macOS 中安装 libgdiplus,请参阅: 如何在 macOS 上安装 libgdiplus
注意:Aspose.Cells For .NetStandard 能够支持您在 MAC OS 上的需求。
适用于: NetStandard2.0, NetCore2.1, NetCore3.1, Net5.0, Net6.0 及更高版本。
Run Aspose Cells in Docker
如何在使用Net6的非Windows平台上使用图形库
Aspose.Cells for Net6现在使用SkiaSharp作为图形库,如微软官方声明中所推荐的。有关在NET6中使用Aspose.Cells的更多详情,请参阅如何运行Aspose.Cells for .Net6。
创建Hello World应用程序
以下步骤使用 Aspose.Cells API 创建了 Hello World 应用程序:
- 如果您有许可证,则应用它。 如果您使用的是评估版,则跳过与许可证相关的代码行。
- 创建Workbook类的实例以创建新的Excel文件,或者打开现有的Excel文件。
- 访问Excel文件的工作表中的任意单元格。
- 在访问的单元格中插入单词Hello World!。
- 生成修改后的Microsoft Excel文件。
上述步骤的实现在下面的示例中进行了演示。
代码示例:创建新工作簿
以下示例从头开始创建一个新工作簿,在第一个工作表的A1单元格中插入"Hello World!",并另存为Excel文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
try | |
{ | |
// Create a License object | |
License license = new License(); | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
license.SetLicense(dataDir + "Aspose.Cells.lic"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
// Instantiate a Workbook object that represents Excel file. | |
Workbook wb = new Workbook(); | |
// When you create a new workbook, a default "Sheet1" is added to the workbook. | |
Worksheet sheet = wb.Worksheets[0]; | |
// Access the "A1" cell in the sheet. | |
Cell cell = sheet.Cells["A1"]; | |
// Input the "Hello World!" text into the "A1" cell | |
cell.PutValue("Hello World!"); | |
// Save the Excel file. | |
wb.Save(dataDir + "MyBook_out.xlsx"); |
代码示例:打开现有文件
以下示例打开现有的Microsoft Excel模板文件"Sample.xlsx",在第一个工作表的A1单元格中插入"Hello World!",并另存为Excel文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
try | |
{ | |
// Create a License object | |
License license = new License(); | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
license.SetLicense("Aspose.Cells.lic"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Sample.xlsx", FileMode.Open); | |
// Instantiate a Workbook object that represents the existing Excel file | |
Workbook workbook = new Workbook(fstream); | |
// Get the reference of "A1" cell from the cells collection of a worksheet | |
Cell cell = workbook.Worksheets[0].Cells["A1"]; | |
// Put the "Hello World!" text into the "A1" cell | |
cell.PutValue("Hello World!"); | |
// Save the Excel file | |
workbook.Save(dataDir + "HelloWorld_out.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |