无需MS Office创建Excel!在C ++中以编程方是在Excel中插入和删除行和列

有时可能会遇到需要以编程方式操纵Excel文件时插入或删除行和列的情况。有鉴于此,本文将介绍如何使用C ++在Excel工作表中插入和删除行和列。

MS Excel为您提供了许多处理Excel文件的功能。可以执行的基本操作之一是插入或删除行和列。有时可能会遇到需要以编程方式操纵Excel文件时插入或删除行和列的情况。有鉴于此,本文将教您如何使用C ++在Excel工作表中插入和删除行和列。

  • 使用C ++在Excel工作表中插入行
  • 使用C ++在Excel工作表中插入列
  • 使用C ++从Excel工作表中删除行
  • 使用C ++从Excel工作表中删除列

整合所有格式的Aspose.Total永久授权正在火热促销中,联系客服立马1分钟了解全部咨询!


使用C ++在Excel工作表中插入行

下面是我们将在本文中处理的示例文件的图像。

无需MS Office创建Excel!在C ++中以编程方是在Excel中插入和删除行和列

以下是在Excel工作表中插入行的步骤。

  • 首先,使用 IWorkbook 类加载 Excel 文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法获得你想插入行的工作表实例。
  • 使用IWorksheet->GetICells()->InsertRows ( Aspose::Cells::Systems::Int32 rowIndex, Aspose::Cells::Systems::Int32 totalRows) 方法插入行。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存Excel文件。

下面的示例代码显示了如何使用C ++在Excel工作表中插入行。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the input Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Insert 2 rows into the worksheet at 3rd positionworksheet->GetICells()->InsertRows(2, 2);// Path of output Excel fileStringPtr outputInsertRows = outDir->StringAppend(new String("outputInsertRows.xlsx"));// Save the Excel file.workbook->Save(outputInsertRows);

无需MS Office创建Excel!在C ++中以编程方是在Excel中插入和删除行和列

使用C++在Excel工作表中插入列

以下是在Excel工作表中插入列的步骤。

  • 首先,使用 IWorkbook 类加载 Excel 文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法获得你想插入列的工作表实例。
  • 使用IWorksheet->GetICells()->InsertColumns ( Aspose::Cells::Systems::Int32 columnIndex, Aspose::Cells::Systems::Int32 totalColumns) 方法插入列。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存Excel文件。

下面的示例代码显示了如何使用C++在Excel工作表中插入列。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the input Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Insert 2 columns into the worksheet at 2nd positionworksheet->GetICells()->InsertColumns(1, 2);// Path of output Excel fileStringPtr outputInsertColumns = outDir->StringAppend(new String("outputInsertColumns.xlsx"));// Save the Excel file.workbook->Save(outputInsertColumns);

无需MS Office创建Excel!在C ++中以编程方是在Excel中插入和删除行和列

使用C ++从Excel工作表中删除行

以下是从Excel工作表中删除行的步骤。

  • 首先,使用 IWorkbook 类加载 Excel 文件。
  • 使用IWorkbook-> GetIWorksheets()-> GetObjectByIndex(Aspose :: Cells :: Systems :: Int32索引)方法获取要从中删除行的工作表实例。
  • 使用IWorksheet-> GetICells()-> DeleteRows(Aspose :: Cells :: Systems :: Int32 rowIndex,Aspose :: Cells :: Systems :: Int32 totalRows,bool updateReference)方法删除行。
  • 最后,使用IWorkbook-> Save(intrusive_ptrfileName)方法保存Excel文件。

下面的示例代码显示了如何使用C ++从Excel工作表中删除行。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the input Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Delete 2 rows starting from the 3rd rowworksheet->GetICells()->DeleteRows(2, 2, true);// Path of output Excel fileStringPtr outputDeleteRows = outDir->StringAppend(new String("outputDeleteRows.xlsx"));// Save the Excel file.workbook->Save(outputDeleteRows);

无需MS Office创建Excel!在C ++中以编程方是在Excel中插入和删除行和列

使用C ++从Excel工作表中删除列

以下是从Excel工作表中删除列的步骤。

  • 首先,使用 IWorkbook 类加载 Excel 文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法获取你要删除的列的工作表的实例。
  • 使用IWorksheet->GetICells()->DeleteColumns ( Aspose::Cells::Systems::Int32 columnIndex, Aspose::Systems::Int32 totalColumns, bool updateReference) 方法删除列。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存Excel文件。

下面的示例代码显示了如何使用C++从Excel工作表中删除列。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the input Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Delete 2 columns from the worksheet starting at the 2nd positionworksheet->GetICells()->DeleteColumns(1, 2, true);// Path of output Excel fileStringPtr outputDeleteColumns = outDir->StringAppend(new String("outputDeleteColumns.xlsx"));// Save the Excel file.workbook->Save(outputDeleteColumns);

无需MS Office创建Excel!在C ++中以编程方是在Excel中插入和删除行和列

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年3月23日
下一篇 2021年3月23日

相关推荐

发表回复

登录后才能评论