Alıntı: public void gridden_excele(Control grid_adi, string excel_cikti_adi, string sayfa_mesaji)
{
//Dil Farkını Önlemek İçin
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = false;
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sayfa1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = sayfa_mesaji;
for (int i = 1; i < (grid_adi as DataGridView).Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = (grid_adi as DataGridView).Columns[i - 1].HeaderText;
}
for (int i = 0; i < (grid_adi as DataGridView).Rows.Count - 1; i++)
{
for (int j = 0; j < (grid_adi as DataGridView).Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = (grid_adi as DataGridView).Rows.Cells[j].Value.ToString();
}
}
string masaustu = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
workbook.SaveAs(masaustu + "\\" + excel_cikti_adi + ".xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();
MessageBox.Show("Masaüstünde istemiş olduğunuz Excel dosyası oluşturuldu.", "İşlem Tamam", MessageBoxButtons.OK,MessageBoxIcon.Information);
} // gridden excele bitti
Ben c# ile gridten excele bu şekilde veriyorum. İsterseniz bir inceleyin fikir verir. Diğer yandan Vb.Net için aşağıdaki kaynağı bir inceleyin isterseniz:
Kod:
http://www.codeproject.com/KB/vb/Senthil_S__Software_Eng_.aspx