Microsoft Excel Dosyası Oluştur - CSharp


C# ile kod kullanarak xlsx uzantılı excel dosyası oluşturmak için kullanılabilecek alternatif bir koddur.

Kod


//--------------------------------------------------
//Referans ekle.
//Add Reference > Microsoft.Office.Interop.Excel > OK
//--------------------------------------------------
using Excel = Microsoft.Office.Interop.Excel;

void MicrosoftExcelDosyasiOlustur(string tamYol)
{
	if (tamYol == "")
	{
		MessageBox.Show("Dosya yolu boş bırakılamaz.",
						"HATA",
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
	}
	else if (tamYol != "" && !File.Exists(tamYol))
	{
		Microsoft.Office.Interop.Excel._Application uygulama = 
			new Microsoft.Office.Interop.Excel.Application();

		Microsoft.Office.Interop.Excel._Workbook calisma_kitabi = 
			uygulama.Workbooks.Add(Type.Missing);

		Microsoft.Office.Interop.Excel._Worksheet calisma_sayfasi = 
			null;

		uygulama.Visible = true;

		calisma_sayfasi = calisma_kitabi.Sheets["Sayfa1"];
		calisma_sayfasi = calisma_kitabi.ActiveSheet;
		calisma_sayfasi.Name = "excel";

		calisma_sayfasi.SaveAs(
			tamYol,
			Type.Missing,
			Type.Missing,
			Type.Missing,
			Type.Missing,
			Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
			Type.Missing,
			Type.Missing,
			Type.Missing,
			Type.Missing);

		uygulama.Quit();
	}
	else
	{
		MessageBox.Show(
			"Bir hata oluştu.",
			"HATA",
			MessageBoxButtons.OK,
			MessageBoxIcon.Error);
	}
}

Kullanım


void Kullan()
{
	string yol = @"C:\arsiv\ornek.xlsx";
	MicrosoftExcelDosyasiOlustur(yol);
}

Etiketler
csharp csharp dosya işlemleri csharp metot örnekleri csharp microsoft excel işlemleri