DataGridview Verileri Excel'e Aktarma

Bu konuyu okuyanlar

mustafabilir

Öğrenci
Katılım
23 Ağustos 2019
Mesajlar
12
Reaksiyon puanı
0
Puanları
1
Yaş
39
Bu kodlarla DataGridview excele aktarabiliyorum fakat tablo çizgilerini nasıl çizebiliriz?

Ayrıca excele aktardığım veriler tam olarak excelde tamamlanmadan exceli kapatıttıgım zaman ekde göndermiş olduğum ekran görüntüsündeki hatayı veriyor bunu vermemesi için ne yapmalıyım?

Kod:
using Excel = Microsoft.Office.Interop.Excel;

Kod:
private void button3_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application uyg = new Microsoft.Office.Interop.Excel.Application();
            uyg.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook kitap = uyg.Workbooks.Add(System.Reflection.Missing.Value);
            Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)kitap.Sheets[1];
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[1, i + 1];
                myRange.Value2 = dataGridView1.Columns[i].HeaderText;
            }

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[j + 2, i + 1];
                    myRange.Value2 = dataGridView1[i, j].Value;
                }
            }
        }
 

Ekli dosyalar

  • Ekran Alıntısı.PNG
    Ekran Alıntısı.PNG
    14.7 KB · Görüntüleme: 307

sametekinci

Öğrenci
Katılım
14 Ağustos 2019
Mesajlar
11
Reaksiyon puanı
1
Puanları
1
Yaş
36
Aşağıdaki kodlarla değiştirerek sorunu çözebilirsin.
Kolay Gelsin.

Kod:
try
            {   

DialogResult dialog = new DialogResult();
            dialog = MessageBox.Show("Bu işlem, veri yoğunluğuna göre uzun sürebilir. Devam etmek istiyor musunuz?", "EXCEL'E AKTARMA", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dialog == DialogResult.Yes)
            {
                Microsoft.Office.Interop.Excel.Application uyg = new Microsoft.Office.Interop.Excel.Application();
                uyg.Visible = true;
                Microsoft.Office.Interop.Excel.Workbook kitap = uyg.Workbooks.Add(System.Reflection.Missing.Value);
                Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)kitap.Sheets[1];
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[1, i + 1];
                    myRange.Value2 = dataGridView1.Columns[i].HeaderText;
                }

                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {
                        Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[j + 2, i + 1];
                        myRange.Value2 = dataGridView1[i, j].Value;
                    }
                }
            }
            else
            {
                MessageBox.Show("İŞLEM İPTAL EDİLDİ.", "İşlem Sonucu", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
}
            catch (Exception)
            {

         MessageBox.Show("İŞLEM TAMAMLANMADAN EXCEL PENCERESİNİ KAPATTINIZ.", "HATA", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
 
Üst