入门
安装
通过 NuGet 安装 Aspose.Cells
NuGet 是下载和安装 Aspose.Cells for .NET 最简单的方法。
- 打开 Microsoft Visual Studio 和 NuGet 包管理器。
- 搜索“aspose.cells”找到所需的 Aspose.Cells for .NET。
- 点击“安装”,Aspose.Cells for .NET会被下载并引用到你的项目中。
也可以从aspose.cells的nuget网页下载: Aspose.Cells for .NET NuGet 包
在 Windows 上安装 Aspose.Cells
- 从以下页面下载 Aspose.Cells.msi: 下载 Aspose.Cells.msi
- 双击 Aspose Cells msi 并按照说明进行安装:
在 linux 上安装 Aspose.Cells
在这个例子中,我使用 Ubuntu 来展示如何在 linux 上开始使用 Aspose.Cells。
- 创建一个名为“AsposeCellsTest”的 .netcore 应用程序。
- 打开文件“AsposeCellsTest.csproj”,为 Aspose.Cells 包引用添加以下行:
3.在Ubuntu上用VSCode打开项目:
<ItemGroup> <PackageReference Include="Aspose.Cells" Version="22.12" /> </ItemGroup>
- 使用以下代码运行测试:
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。
- 创建一个名为“AsposeCellsTest”的 .netcore 应用程序。
2.用Visual Studio for Mac打开应用程序,然后安装Aspose Cells到NuGet:
- 使用以下代码运行测试:
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");
注意:Aspose.Cells For .NetStandard 可以支持您在 MAC OS 上的要求。
适用于:NetStandard2.0、NetCore2.1、NetCore3.1、Net5.0、Net6.0及高级版本。
在 Docker 中运行 Aspose Cells
如何在非windows平台上使用Net6图形库
Aspose.Cells for Net6 现在使用 SkiaSharp 作为图形库,如中所推荐Microsoft官方声明.有关将 Aspose.Cells 与 NET6 一起使用的更多详细信息,请参阅如何为 .Net6 运行 Aspose.Cells.
创建 Hello World 应用程序
以下步骤使用 Aspose.Cells API 创建 Hello World 应用程序:
- 如果你有驾照,那么应用它. 如果您使用的是评估版,请跳过与许可证相关的代码行。
- 创建一个实例工作簿类以创建新的 Excel 文件,或打开现有的 Excel 文件。
- 访问 Excel 文件中工作表的任何所需单元格。
- 插入单词**Hello World!**进入访问的单元格。
- 生成修改后的 Microsoft Excel 文件。
下面的示例演示了上述步骤的实现。
代码示例:创建新工作簿
下面的示例从头开始创建一个新工作簿,插入“Hello World!”到第一个工作表的单元格 A1 中并另存为 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”,插入“Hello World!”到第一个工作表的单元格 A1 中并另存为 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(); |