翻譯|使用教程|編輯:李顯亮|2021-04-26 10:14:13.957|閱讀 326 次
概述:有時可能會遇到需要以編程方式操縱Excel文件時插入或刪除行和列的情況。有鑒于此,本文將介紹如何使用C ++在Excel工作表中插入和刪除行和列。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
MS Excel為您提供了許多處理Excel文件的功能。可以執行的基本操作之一是插入或刪除行和列。有時可能會遇到需要以編程方式操縱Excel文件時插入或刪除行和列的情況。有鑒于此,本文將教您如何使用C ++在Excel工作表中插入和刪除行和列。
Aspose.Cells for C++是本機C ++庫,使用它可以創建,讀取和更新Excel文件,而無需安裝Microsoft Excel。該API還支持在Excel工作表中插入和刪除行和列。
下面是我們將在本文中處理的示例文件的圖像。
以下是在Excel工作表中插入行的步驟。
下面的示例代碼顯示了如何使用C ++在Excel工作表中插入行。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the input Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Insert 2 rows into the worksheet at 3rd position worksheet->GetICells()->InsertRows(2, 2); // Path of output Excel file StringPtr outputInsertRows = outDir->StringAppend(new String("outputInsertRows.xlsx")); // Save the Excel file. workbook->Save(outputInsertRows);
以下是在Excel工作表中插入列的步驟。
下面的示例代碼顯示了如何使用C++在Excel工作表中插入列。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the input Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Insert 2 columns into the worksheet at 2nd position worksheet->GetICells()->InsertColumns(1, 2); // Path of output Excel file StringPtr outputInsertColumns = outDir->StringAppend(new String("outputInsertColumns.xlsx")); // Save the Excel file. workbook->Save(outputInsertColumns);
以下是從Excel工作表中刪除行的步驟。
下面的示例代碼顯示了如何使用C ++從Excel工作表中刪除行。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the input Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Delete 2 rows starting from the 3rd row worksheet->GetICells()->DeleteRows(2, 2, true); // Path of output Excel file StringPtr outputDeleteRows = outDir->StringAppend(new String("outputDeleteRows.xlsx")); // Save the Excel file. workbook->Save(outputDeleteRows);
以下是從Excel工作表中刪除列的步驟。
下面的示例代碼顯示了如何使用C++從Excel工作表中刪除列。
// Source directory path. StringPtr srcDir = new String("SourceDirectory\\"); // Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the input Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Delete 2 columns from the worksheet starting at the 2nd position worksheet->GetICells()->DeleteColumns(1, 2, true); // Path of output Excel file StringPtr outputDeleteColumns = outDir->StringAppend(new String("outputDeleteColumns.xlsx")); // Save the Excel file. workbook->Save(outputDeleteColumns);
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn