C++ ile Excel dosyasına Satır ve Sütun Ekleme ve Silme
Giriş
Sıfırdan yeni bir çalışma sayfası oluştururken veya mevcut bir çalışma sayfası üzerinde çalışırken, daha fazla veri eklemek için ekstra satırlar veya sütunlar eklememiz gerekebilir. Tersine, çalışma sayfasının belirli pozisyonlarından satırları veya sütunları silebiliriz. Bu gereksinimleri karşılamak için, Aspose.Cells çok basit bir sınıf ve yöntemler seti sağlar, aşağıda açıklanmıştır.
Satırları ve Sütunları Yönetmek
Aspose.Cells, Microsoft Excel dosyasını temsil eden Workbook sınıfını sağlar. Workbook sınıfı, bir Excel dosyasındaki her çalışma sayfasına erişimi sağlayan Worksheets koleksiyonunu içerir. Bir çalışma sayfası Worksheet sınıfı ile temsil edilir. Worksheet sınıfı, çalışma sayfasındaki tüm hücreleri temsil eden GetCells() koleksiyonunu sağlar.
GetCells() koleksiyonu, çalışma sayfasındaki satır ve sütunları yönetmek için çeşitli yöntemler sağlar. Bunlardan bazıları aşağıda anlatılmaktadır.
Satırları ve Sütunları Eklemek
Bir Satır Nasıl Eklenir
Çalışma sayfasına herhangi bir konumda satır eklemek için, GetCells() koleksiyonunun InsertRow metodunu çağırın. InsertRow yöntemi, eklenecek satırın indeksini alır.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Instantiating a Workbook object
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Inserting a row into the worksheet at 3rd position
worksheet.GetCells().InsertRow(2);
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Birkaç Satır Nasıl Eklenir
Birden fazla satır eklemek için, GetCells() koleksiyonunun InsertRows metodunu çağırın. InsertRows yöntemi iki parametre alır:
- Satır indeksi, yeni satırların ekleneceği satırın indeksi.
- Satır sayısı, eklenmesi gereken toplam satır sayısı.
#include <iostream>
#include <fstream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Insert 10 rows into the worksheet starting from 3rd row
worksheet.GetCells().InsertRows(2, 10);
// Path of output excel file
U16String outputFilePath = srcDir + u"output.out.xls";
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Rows inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Biçimlendirme Seçenekleriyle Bir Satır Nasıl Eklenir
Biçimlendirme seçenekleriyle satır eklemek için, InsertOptions parametresi alan InsertRows aşırı yüklemesini kullanın. CopyFormatType özelliğini InsertOptions sınıfında, CopyFormatType Enumeration kullanarak ayarlayın. CopyFormatType Enumeration’un üç üyesi aşağıda listelenmiştir.
- SameAsAbove: Yukarıdaki satır gibi biçimlendirir.
- SameAsBelow: Aşağıdaki satır gibi biçimlendirir.
- Temizle: Biçimlendirmeyi temizler.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"InsertingARowWithFormatting_out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Setting Formatting options
InsertOptions insertOptions;
insertOptions.SetCopyFormatType(CopyFormatType::SameAsAbove);
// Inserting a row into the worksheet at 3rd position
worksheet.GetCells().InsertRows(2, 1, insertOptions);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Row inserted successfully with formatting!" << std::endl;
Aspose::Cells::Cleanup();
}
Bir Sütun Nasıl Eklenir
Geliştiriciler, herhangi bir konumda sütun eklemek için GetCells() koleksiyonunun InsertColumn metodunu çağırabilir. InsertColumn yöntemi, eklenecek sütunun indeksini alır.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Insert a column into the worksheet at 2nd position
worksheet.GetCells().InsertColumn(1);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Column inserted successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Satırları ve Sütunları Silmek
Birden Fazla Satır Nasıl Silinir
Bir çalışma sayfasındanden çoklu satır silmek için, GetCells() koleksiyonunun DeleteRows metodunu çağırın. DeleteRows yöntemi iki parametre alır:
- Satır endeksi, satırların silineceği başlangıç satırının endeksi.
- satır sayısı, silinmesi gereken toplam satır sayısı
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Delete 10 rows from the worksheet starting from 3rd row
worksheet.GetCells().DeleteRows(2, 10);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Rows deleted successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}
Bir Sütunu Nasıl Silebilirsiniz
Herhangi bir konumdaki sütunu silmek için, GetCells() koleksiyonunun DeleteColumn metodunu çağırın. DeleteColumn yöntemi, silinecek sütunun indeksini alır.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Create workbook from file
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Delete a column from the worksheet at 5th position (index 4)
worksheet.GetCells().DeleteColumn(4);
// Save the modified Excel file
U16String outputFilePath = srcDir + u"output.xlsx";
workbook.Save(outputFilePath);
std::cout << "Column deleted successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}