Sunday, February 1, 2015

Missing Header Row When Exporting Data to Excel and Program Runs Slow

Hi Dave,


For question 1: Please try the following code, using HeaderText property to add header to Excel.



// storing header part in Excel

for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
if (dataGridView1.Columns[i - 1].Visible)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
}

For question 2: Yes, I have also met the same issue before. after some search, please refer to


Export Sql data to Excel very slow


Option 1:


See this answer . Use a library called ClosedXML to write the data to Excel.


Option 2:


Get a range big enough for all of the data and set the value equal to a 2 dimensional range. This works very fast without another referencing another library.


And note op read the data from DataTable not DataGridView.



string conString = "Data Source=DELL\\SQLSERVER1*******";

SqlConnection sqlCon = new SqlConnection(conString);
sqlCon.Open();


SqlDataAdapter da = new SqlDataAdapter("Select *****from*****", sqlCon);

System.Data.DataTable dtMainSQLData = new System.Data.DataTable();

da.Fill(dtMainSQLData);

Best regards,


Kristin






We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

Click HERE to participate the survey.





No comments:

Post a Comment