Merhaba arkadaşlar aşağıdaki kod ile c# uygulamalarınızdaki datagrid nesnelerinizde bulunan verilerinizi excel’ e kolayca aktarabilirsiniz.
Bunun için daha önce yaptığımız gibi “using Excel = Microsoft.Office.Interop.Excel;” kütüphanesini eklemeyi unutmayın.
Farklı bir kod ile daha önceki yazımızda bunu yapmıştık.
private void copyAlltoClipboard() { DataGridView1.SelectAll(); DataObject dataObj = DataGridView1.GetClipboardContent(); if (dataObj != null) Clipboard.SetDataObject(dataObj); } private void BtnExcel_Click(object sender, EventArgs e) { try { copyAlltoClipboard(); Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlexcel = new Excel.Application(); xlexcel.Visible = true; xlWorkBook = xlexcel.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); } catch (Exception hata) { MessageBox.Show(hata.StackTrace); } }
Umarım fayalı olmuştur.
0 yorum