翻譯|使用教程|編輯:李顯亮|2021-05-26 10:04:10.147|閱讀 349 次
概述:合并和取消合并單元格是Microsoft Excel的一項(xiàng)簡(jiǎn)單且常用功能,有時(shí)可能需要在C ++應(yīng)用程序中執(zhí)行這些任務(wù)。為此,本文將教您如何使用C ++以編程方式合并和取消合并Excel工作表中的單元格。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
合并和取消合并單元格是Microsoft Excel的一項(xiàng)簡(jiǎn)單且常用功能。合并單元格可能會(huì)在某些情況下很有用,例如,當(dāng)工作表中有多個(gè)列共享相同的標(biāo)題時(shí),可以合并列上方的單元格以使其具有共同的標(biāo)題。如果不再需要合并的單元格,則可以輕松地取消合并它們。為此,本文將教您如何使用C ++以編程方式合并和取消合并Excel工作表中的單元格。
Aspose.Cells for C++是本機(jī)C ++庫(kù),使用它可以創(chuàng)建,讀取和修改Excel文件,而無(wú)需安裝Microsoft Excel。該API還支持合并和取消合并Excel工作表中的單元格。
在此示例中,我們將創(chuàng)建一個(gè)空的Excel工作表,并按照以下步驟合并幾個(gè)單元格。
下面的示例代碼顯示了如何使用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工作表中取消合并單元格區(qū)域的步驟。
下面的示例代碼顯示了如何使用C++在Excel工作表中取消合并一個(gè)單元格的范圍。
// 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++還提供了合并指定區(qū)域的單元格的能力。為了實(shí)現(xiàn)這一功能,請(qǐng)按照下面的步驟進(jìn)行操作。
下面的示例代碼演示了如何使用C++合并一個(gè)命名區(qū)域的單元格。
// 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的全部完整功能,可聯(lián)系在線客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn