您的第一个 Aspose.Cells 申请 - Hello World
Contents
[
Hide
]
本教程介绍如何使用 Aspose.Cells 简单的 API 创建第一个应用程序 (Hello World)。这个简单的应用程序创建一个 Microsoft Excel 文件,在指定的工作表单元格中包含文本“Hello World”。
创建 Hello World 应用程序
以下步骤使用 Aspose.Cells API 创建 Hello World 应用程序:
- 创建一个实例工作簿班级。
- 如果你有驾照,那么应用它. 如果您使用的是评估版,请跳过与许可证相关的代码行。
- 创建一个新的 Excel 文件,或打开一个现有的 Excel 文件。
- 访问 Excel 文件中工作表的任何所需单元格。
- 插入单词**Hello World!**进入访问的单元格。
- 生成修改后的 Microsoft Excel 文件。
下面的示例演示了上述步骤的实现。
代码示例:创建新工作簿
以下示例从头开始创建一个新工作簿,写入 Hello World!到第一个工作表的单元格 A1 中并保存 Excel 文件。
This file contains hidden or 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 | |
// 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"); |
代码示例:打开现有文件
下面的示例打开一个名为“Sample.xlsx”的现有 Microsoft Excel 模板文件,输入“Hello World!”文本到第一个工作表的 A1 单元格中并保存工作簿。
This file contains hidden or 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 | |
// 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(); |