您的第一个 Aspose.Cells 应用程序 Hello World
Contents
[
Hide
]
本教程演示了如何使用 Aspose.Cells 的简单 API 创建一个非常基础的应用程序(Hello World)。该简单应用程序将在指定的工作表单元格中创建一个包含文本“Hello World”的 Microsoft Excel 文件。
如何创建 Hello World 应用程序
以下步骤使用 Aspose.Cells API 创建了 Hello World 应用程序:
- 创建 Workbook 类的实例。
- 如果您有许可证,则应用它。 如果您使用的是评估版,则跳过与许可证相关的代码行。
- 创建一个新的Excel文件,或者打开一个已经存在的Excel文件。
- 访问Excel文件的工作表中的任意单元格。
- 在访问的单元格中插入单词Hello World!。
- 生成修改后的Microsoft Excel文件。
上述步骤的实现在下面的示例中进行了演示。
如何创建新工作簿
下面的示例从头开始创建一个新的工作簿,在第一个工作表的A1单元格中写入Hello World!,并保存Excel文件。
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 | |
// 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模板文件,在第一个工作表的A1单元格中输入"Hello World!“文本,并保存工作簿。
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 | |
// 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(); |