HTML
JPG
PDF
XML
XLSX
DOCX
在C++中创建DOCX文件
使用 C++ 库以编程方式创建本机高性能 DOCX 文件,无需 Microsoft Office。
使用 C++ 创建 DOCX 文件
如何创建DOCX文件?使用 Aspose.Cells for C++ 库,您可以通过几行代码以编程方式轻松创建 DOCX 文件。 Aspose.Cells for C++ 能够构建跨平台应用程序,能够生成、修改、转换、渲染和打印所有 Excel 文件。 C++ Excel API不仅可以在电子表格格式之间进行转换,还可以将Excel文件渲染为图像,PDF、HTML、ODS、CSV、SVG、JSON、WORD、PPT等,从而使其成为行业交换文档的完美选择-标准格式。您可以直接下载最新版本,打开即可 NuGet 包管理器,搜索Aspose.Cells.cpp并安装。您还可以从包管理器控制台使用以下命令。
命令
PM> Install-Package Aspose.Cells.Cpp
如何在C++中创建DOCX
开发人员只需几行代码即可轻松在运行的报告应用程序中创建、加载、修改和转换 DOCX 文件以进行数据处理。
- 创建 IWorkbook 类的对象。
- 将第一个工作表放入 IWorksheet 对象中。
- 使用 IWorksheet->GetICells() 方法将工作表的单元格获取到 ICells 对象中。
- 使用 ICells->GetObjectByIndex() 方法将工作表中所需的单元格访问到 ICell 对象中。
- 使用 ICell->PutValue() 方法将值输入到单元格中。
- 使用 Save() 方法将工作簿保存为 .docx 文件。
示例代码展示了如何在 C++ 中创建 DOCX 文件。
// Create an object of the IWorkbook class.
intrusive_ptr<IWorkbook> wkb = Factory::CreateIWorkbook();
// Get the first sheet into an IWorksheet object.
intrusive_ptr<IWorksheetCollection> wsc = wkb->GetIWorksheets();
intrusive_ptr<IWorksheet> ws = wsc->GetObjectByIndex(0);
// Use IWorksheet->GetICells() method to get the cells of the worksheet into an ICells object.
intrusive_ptr<ICells> cells = ws->GetICells();
// Use ICells->GetObjectByIndex() method to access the desired cell of the worksheet into an ICell object.
intrusive_ptr<ICell> cell00 = cells->GetObjectByIndex(0, 0);
intrusive_ptr<ICell> cell01 = cells->GetObjectByIndex(0, 1);
intrusive_ptr<ICell> cell10 = cells->GetObjectByIndex(1, 0);
intrusive_ptr<ICell> cell11 = cells->GetObjectByIndex(1, 1);
// Use ICell->PutValue() method to input value into the cell.
cell00->PutValue(new String("ColumnA"));
cell01->PutValue(new String("ColumnB"));
cell10->PutValue(new String("ValueA"));
cell11->PutValue(new String("ValueB"));
// Save workbook to resultFile folder
wkb->Save(new String("created_one.docx"));
系统要求
在运行 C++ 转换示例源代码之前,请确保满足以下先决条件。
- Microsoft Windows 或具有 C++ 运行时环境的兼容操作系统,适用于 Windows 32 位、Windows 64 位和 Linux 64 位。
- 在项目中添加对 Aspose.Cells for C++ DLL 的引用。
DOCX 什么是DOCX文件格式
DOCX 是 Microsoft Word 文档的众所周知的格式。自 2007 年发布 Microsoft Office 2007 以来,这种新文档格式的结构已从纯二进制更改为 XML 和二进制文件的组合。 Docx 文件可以使用 Word 2007 和横向版本打开,但不能使用支持 DOC 文件扩展名的早期版本的 MS Word 打开。
阅读更多