翻譯|使用教程|編輯:李顯亮|2021-05-26 10:04:10.147|閱讀 349 次
概述:合并和取消合并單元格是Microsoft Excel的一項簡單且常用功能,有時可能需要在C ++應用程序中執行這些任務。為此,本文將教您如何使用C ++以編程方式合并和取消合并Excel工作表中的單元格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
合并和取消合并單元格是Microsoft Excel的一項簡單且常用功能。合并單元格可能會在某些情況下很有用,例如,當工作表中有多個列共享相同的標題時,可以合并列上方的單元格以使其具有共同的標題。如果不再需要合并的單元格,則可以輕松地取消合并它們。為此,本文將教您如何使用C ++以編程方式合并和取消合并Excel工作表中的單元格。
Aspose.Cells for C++是本機C ++庫,使用它可以創建,讀取和修改Excel文件,而無需安裝Microsoft Excel。該API還支持合并和取消合并Excel工作表中的單元格。
在此示例中,我們將創建一個空的Excel工作表,并按照以下步驟合并幾個單元格。
下面的示例代碼顯示了如何使用C ++合并Excel工作表中的單元格。
// Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the input Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Create a Cells object ot fetch all the cells. intrusive_ptrcells = worksheet->GetICells(); // Merge cells cells->Merge(5, 2, 2, 3); // Put data into the cell cells->GetObjectByIndex(5, 2)->PutValue((StringPtr)new String("This is a test value")); // Create a Style object intrusive_ptrstyle = cells->GetICell(5, 2)->GetIStyle(); // Create a Font object intrusive_ptrfont = style->GetIFont(); // Set the name font->SetName(new String("Times New Roman")); // Set the font size font->SetSize(18); // Set the font color font->SetColor(Systems::Drawing::Color::GetCyan()); // Make the text bold font->SetBold(true); // Make the text italic font->SetItalic(true); // Set the Foreground color style->SetForegroundColor(Systems::Drawing::Color::GetRed()); // Set the Pattern style->SetPattern(BackgroundType_Solid); // Apply the Style cells->GetICell(5, 2)->SetIStyle(style); // Save the Excel file workbook->Save(outDir->StringAppend(new String("MergeCells_out.xlsx")));
以下是取消合并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_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedCells.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Create a Cells object ot fetch all the cells. intrusive_ptr<ICells> cells = worksheet->GetICells(); // Unmerge cells. cells->UnMerge(5, 2, 2, 3); // Save the Excel file workbook->Save(outDir->StringAppend(new String("UnmergeCells_out.xlsx")));
以下是合并Excel工作表中的一系列單元格的步驟。
下面的示例代碼顯示了如何使用C ++合并Excel工作表中的一系列單元格。
// Output directory path. StringPtr outDir = new String("OutputDirectory\\"); // Load the input Excel file intrusive_ptrworkbook = Factory::CreateIWorkbook(); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Input data into A1 Cell. worksheet->GetICells()->GetObjectByIndex(0, 0)->PutValue((StringPtr)new String("This is a test value")); // Create a range intrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4")); // Merge range into a single cell range->Merge(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("MergeRangeOfCells_out.xlsx")));
以下是在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("SampleMergedRangeOfCells.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Create a range intrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4")); // UnMerge range range->UnMerge(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("UnmergeRangeOfCells_out.xlsx")));
Aspose.Cells for C++還提供了合并指定區域的單元格的能力。為了實現這一功能,請按照下面的步驟進行操作。
下面的示例代碼演示了如何使用C++合并一個命名區域的單元格。
// 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("SampleMergedRangeOfCells.xlsx"))); // Access the first worksheet in the Excel file intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Create a range intrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4")); // Set Range name range->SetName(new String("Named_Range")); // Define style object intrusive_ptrstyle = workbook->CreateIStyle(); // Set horizontal alignment style->SetHorizontalAlignment(TextAlignmentType::TextAlignmentType_Center); // Create a StyleFlag object intrusive_ptrstyleFlag = Factory::CreateIStyleFlag(); // Set horizontal alignment to true styleFlag->SetHorizontalAlignment(true); // Apply the style to the range range->ApplyIStyle(style, styleFlag); // Put data in the range range->GetObjectByIndex(0, 0)->PutValue((StringPtr)new String("This is a test value")); // Merge Range range->Merge(); // Save the Excel file workbook->Save(outDir->StringAppend(new String("MergeNamedRange_out.xlsx")));
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn