C# - How to Export DataGridView Data to Ms Excel File in Visual C#

C# - How to Export DataGridView Data to Ms Excel File in Visual C#

C# Ui Academy

5 лет назад

74,511 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Ankita
Ankita - 28.05.2023 13:38

Someone explain me that for loop😭

Ответить
AAJ30
AAJ30 - 21.12.2022 07:11

The best thanks!

Ответить
Coding with Syrym
Coding with Syrym - 02.07.2022 20:35

if you get an error do not forget to type the Value as Value?.

the whole code looks like this:
if(dataGridView1.Rows.Count > 0)
{
Excel.Application excelApp = new Excel.Application();

excelApp.Application.Workbooks.Add(Type.Missing);
for(int i=1; i < dataGridView1.Columns.Count + 1; i++)
{
excelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}

for(int i=0; i < dataGridView1.Rows.Count; i++)
{
for(int j=0; j < dataGridView1.Columns.Count; j++)
{
excelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value?.ToString();
}
}

excelApp.Columns.AutoFit();
excelApp.Visible = true;
}

Ответить
abdulilah elnagme
abdulilah elnagme - 31.03.2022 14:41

thank you very much

Ответить
Zoz Moz
Zoz Moz - 26.12.2021 15:34

hello I did all what you do , but i get message said "Object reference not set to an instance of an object. " in the line code : xcelApp.Cells[i + 2 , j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
could any one helps me please

Ответить
Bruno Oliveira
Bruno Oliveira - 15.11.2021 18:39

Thank you so much

Ответить
Hoàng Nguyễn
Hoàng Nguyễn - 31.10.2021 14:52

Hello, can you help to write data to excel file template?

Ответить
DDM SpeedHeat
DDM SpeedHeat - 25.10.2021 17:32

thank you so much

Ответить
Rafli Iskandar Kavarera
Rafli Iskandar Kavarera - 10.10.2021 11:00

what happen if client doesnt have excel?

Ответить
farapp Xtrem
farapp Xtrem - 08.10.2021 23:31

I'm not able to download DLL file

Ответить
farapp Xtrem
farapp Xtrem - 08.10.2021 23:18

Kindy tell how to save pictures in database and how to publish winforms applications

Ответить
Trung Trương
Trung Trương - 07.09.2021 18:41

great

Ответить
George Wadie
George Wadie - 30.07.2021 13:07

dear sir do you iam doing the same code but the out is out of order

Ответить
Bekind Havepassion
Bekind Havepassion - 25.07.2021 08:14

Hello! Thank you, it's cool! Can You be so kind to make a tutorial on how to export data from a form to a googlesheet? It'd be great!

Ответить
Katry Konig
Katry Konig - 03.06.2021 00:52

It wouldn't work if there is a combox inside the DG

Ответить
Phát Lê Ngọc
Phát Lê Ngọc - 30.05.2021 19:43

Thank you so much

Ответить
Marc Stephen Corpuz
Marc Stephen Corpuz - 07.04.2021 06:48

sir help. i have followed every code you did but im still getting error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

the error im getting is at the

Excelapp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

Ответить
Prosaa
Prosaa - 29.03.2021 17:08

i have a System.NullReferenceException: 'Object reference not set to an instance of an object.'
can you fix it for me ? please, thank you

Ответить
Black Shealth
Black Shealth - 14.02.2021 12:56

Sir, I'm stuck. Please help. I didn't find the Rows. Count in my case. It's showing the wavy red line. So how do I traverse through each row?

Ответить
حسين ابراهيم عبدالرسول
حسين ابراهيم عبدالرسول - 30.01.2021 18:00

thank you very very very very very very very very very very very very very very very very much

Ответить