本文介绍了使用 NET REST API 在 Word 中创建表格的过程。您将使用 Aspose.Words for .NET cloud SDK 通过调用 API 调用并将输出保存在本地系统上来使用 C# Low Code API 在 Word 中插入表格。将介绍设置环境和在任何支持 SDK 的平台上开发应用程序所需的所有步骤。
先决条件
- 创建账户并获取 API 凭证
- 下载 Aspose.Words Cloud SDK for Dotnet to add a bookmark in a Word file
- 使用上述 SDK 设置 C# 解决方案项目
使用 C# REST API 在 Word 文档中创建表格的步骤
- 使用客户端 ID 和密钥创建 WordsApi 类对象
- 将输入的 Word 文件读入内存流
- 创建 TableInsert 对象并设置行数和列数
- 使用输入文档、请求的表和输出文件创建 InsertTableOnlineRequest
- 调用 InsertTableOnline 使用上面的表格请求创建表格
- 获取API调用结果并保存在本地文件中
上述步骤描述了使用 NET REST API 在 Word 中制作表格的过程。通过读取输入文件并使用 InsertTableOnlineRequest 类创建向 Word 文件添加表格的请求来开始该过程。生成请求后,将调用 InsertTableOnline 方法来插入表格,并以目标文件名作为参数。
使用 C# Low Code API 在 Word 文档中创建表格的代码
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using Aspose.Words.Cloud.Sdk; | |
using Aspose.Words.Cloud.Sdk.Model; | |
using Aspose.Words.Cloud.Sdk.Model.Requests; | |
namespace WordsSample.Words | |
{ | |
public class WordFileOperations | |
{ | |
public void CreateTable() | |
{ | |
try | |
{ | |
var wordsApi = new WordsApi("", ""); | |
using var requestDocument = File.OpenRead("Sample.docx"); | |
var requestTable = new TableInsert() | |
{ | |
ColumnsCount = 5, | |
RowsCount = 4 | |
}; | |
string output = "OutputWithTable.docx"; | |
var insertRequest = new InsertTableOnlineRequest(requestDocument, requestTable, destFileName: output); | |
var tableTask = wordsApi.InsertTableOnline(insertRequest); | |
tableTask.Wait(); | |
var result = tableTask.Result; | |
if (result.Document.TryGetValue(output, out var stream)) | |
{ | |
stream.Position = 0; | |
using (var fileStream = File.Create(output)) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
此示例代码展示了如何使用 C# REST API 在 MS Word 中创建表格。当您使用 InsertTableOnline 方法时,它会在 Word 文件的末尾创建表格,并将修改后的文件保存在云中,并以指定的名称作为目标文件。您需要使用此任务的结果将修改后的文件下载到本地系统。
在本主题中,我们学习了如何使用 C# Low Code API 在 Word 文档中插入表格。要合并文档,请参阅以下文章:使用 NET REST API 合并 Word 文档。