Belge, Excel dosyasının C# ile nasıl oluşturulacağını gösterir. Aşağıdaki adımlar ve kod parçacığı, MS Office birlikte çalışmasını kullanmadan C#‘da Excel dosyasının nasıl oluşturulacağını gösterir.
C# ile Excel Dosyası Oluşturma Adımları
- Aspose.Cells for .NET paketini NuGet.org’dan indirin veya yükleyin
- Aspose.Cells ad alanını VS.NET projesine aktarın
- Lisans nesnesini tanımlayın ve SetLicense yöntemini kullanarak ayarlayın
- Boş bir Workbook oluşturun
- İlk Worksheet hücrelerine rapor verilerini girin
- Excel XLSX dosyasını verilerle kaydedin
Yıllar geçtikçe, MS Excel dosya biçimlerinin veri analizi ve veri raporlaması için kullanıldığı bilinmektedir. Yukarıdaki adımlar, C# kullanarak Excel dosyasına yazma prosedürünü açıklar. Sıfırdan yeni bir Excel dosyası oluşturmak için C# kodu yazıyoruz. Excel dosyası, örnek bir müşteri raporu içerecektir; Çalışma sayfasındaki ilgili hücreleri verilerle doldurarak istediğiniz raporu oluşturabilirsiniz.
C# ile Excel Dosyası Oluşturmak için Örnek Kod
//Add reference to the namespace of Aspose.Cells for .NET | |
using Aspose.Cells; | |
namespace GenerateExcelFile | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//At the start, set the license before using other APIs | |
Aspose.Cells.License Aspose_Cells_license = new Aspose.Cells.License(); | |
Aspose_Cells_license.SetLicense("Aspose.Cells.lic"); | |
//Create an Excel workbook from the scratch | |
Workbook ExcelFileWorkbook = new Workbook(); | |
//Get the first worksheet (0 indexed position) in the workbook, the default worksheet | |
Worksheet ExcelFileSheet = ExcelFileWorkbook.Worksheets[0]; | |
//Get the cells collection in the default worksheet | |
Cells SheetCells = ExcelFileSheet.Cells; | |
//Insert data into the cells of the sheet | |
SheetCells["A1"].PutValue("Customers Report"); | |
SheetCells["A2"].PutValue("C_ID"); | |
SheetCells["B2"].PutValue("C_Name"); | |
SheetCells["A3"].PutValue("C001"); | |
SheetCells["B3"].PutValue("Customer1"); | |
SheetCells["A4"].PutValue("C002"); | |
SheetCells["B4"].PutValue("Customer2"); | |
SheetCells["A5"].PutValue("C003"); | |
SheetCells["B5"].PutValue("Customer3"); | |
SheetCells["A6"].PutValue("C004"); | |
SheetCells["B6"].PutValue("Customer4"); | |
//Save to Excel file (XLSX) | |
ExcelFileWorkbook.Save("ExcelFile.xlsx"); | |
} | |
} | |
} |
Yukarıdaki örnekte, C# ile dinamik olarak Excel dosyasının nasıl oluşturulacağını öğreneceksiniz. C#.NET kullanarak kolayca Excel dosyası oluşturabilirsiniz. Kod parçacığında, basit bir rapor oluşturmak için çalışma kitabındaki ilk çalışma sayfasının birkaç hücresine veri ekliyoruz. Son olarak Excel XLSX dosyasını diske kaydediyoruz. Ayrıca büyük Excel dosyasını C# ile CSV’ye nasıl dışa aktarırım‘i de beğenebilirsiniz.