Hücrelerin Hizalamasını Değiştir ve Mevcut Biçimlendirmeyi Koru ile C++
Olası Kullanım Senaryoları
Bazen, birden çok hücrenin hizalamasını değiştirmek istersiniz ama mevcut biçimlendirmeyi de korumak istersiniz. Aspose.Cells, bu işlemi GetAlignments() özelliği ile yapmanızı sağlar. Eğer true olarak ayarlarsanız, hizalamadaki değişiklikler gerçekleşir, aksi takdirde gerçekleşmez. Lütfen unutmayın, StyleFlag nesnesi ApplyStyle(const Style& style, const StyleFlag& flag) metoduna parametre olarak geçirilir ve bu metod aslında hücre aralığına biçimlendirme uygular.
Hücre Düzenini Değiştirme ve Mevcut Biçimlendirmeyi Koruma
Aşağıdaki örnek kod, örnek Excel dosyasını yükler, aralık oluşturur ve yatay ve dikey olarak ortalayıp mevcut biçimlendirmeyi korur. Aşağıdaki ekran görüntüsü, örnek Excel dosyasını ve çıktı Excel dosyasını karşılaştırır ve hücrelerin mevcut biçimlendirmesinin aynı olduğunu ancak hücrelerin şimdi yatay ve dikey olarak merkezlenmiş olduğunu gösterir.
Örnek Kod
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Load sample Excel file containing cells with formatting.
U16String sourceDir(u"..\\Data\\01_SourceDirectory\\");
U16String outputDir(u"..\\Data\\02_OutputDirectory\\");
Workbook wb(sourceDir + u"sampleChangeCellsAlignmentAndKeepExistingFormatting.xlsx");
// Access first worksheet.
Worksheet ws = wb.GetWorksheets().Get(0);
// Create cells range.
Range rng = ws.GetCells().CreateRange(u"B2:D7");
// Create style object.
Style st = wb.CreateStyle();
// Set the horizontal and vertical alignment to center.
st.SetHorizontalAlignment(TextAlignmentType::Center);
st.SetVerticalAlignment(TextAlignmentType::Center);
// Create style flag object.
StyleFlag flag;
// Set style flag alignments true. It is the most crucial statement.
// Because if it is false, no changes will take place.
flag.SetAlignments(true);
// Apply style to range of cells.
rng.ApplyStyle(st, flag);
// Save the workbook in XLSX format.
wb.Save(outputDir + u"outputChangeCellsAlignmentAndKeepExistingFormatting.xlsx", SaveFormat::Xlsx);
Aspose::Cells::Cleanup();
}