Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help me: I can't format Date (dd/mm/yyyy), Text, Number of Columns in Datagridview C# when Export Excel.

private void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (dgvGF1111.Rows.Count > 0)

            {
                Microsoft.Office.Interop.Excel.Application xcelApp = new Microsoft.Office.Interop.Excel.Application();
                xcelApp.Application.Workbooks.Add(Type.Missing);

                for (int i = 1; i < dgvGF1111.Columns.Count + 1; i++)
                {
                    xcelApp.Cells[1, i] = dgvGF1111.Columns[i - 1].HeaderText;
                }

                for (int i = 0; i < dgvGF1111.Rows.Count; i++)

                {
                    for (int j = 0; j < dgvGF1111.Columns.Count; j++)

                    {
                        if (i < dgvGF1111.Rows.Count)
                        {
                            xcelApp.Cells[i + 2, j + 1] = dgvGF1111.Rows[i].Cells[j].Value.ToString();
                            //xcelApp.Cells[i+2, 3] = Convert.ToDateTime(dgvGF1111.Rows[2].Cells[3].Value);
                        }
                    }
                }
                xcelApp.Columns.AutoFit();
                xcelApp.Visible = true;
            }
        }

What I have tried:

Format Date, Text, number,.... when Export Excel in Datagridview C#
Posted
Updated 23-Oct-20 22:40pm
v3

You need to add the format settings to the Excel cell so it displays the date in the format that you want. If there is no format set, then Excel will use the system defaults.
 
Share this answer
 
Richard MacCutchan: I don't know Format settings to the excel cells. Please send me your codes. Thanks!
 
Share this answer
 
Comments
Richard MacCutchan 28-Sep-20 4:46am    
I don't know them either. Check the documentation, or Google for samples.
dos cosas se necesitan :
1- usar un hilo con el cultureinfo
2 - castear a date el valor a poner en la celda del excel


Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");

xcelApp.Cells[i + 2, j + 1] = CDATE(dgvGF1111.Rows[i].Cells[j].Value.ToString());


Bendiciones. Espero te sirva mi hermano
 
Share this answer
 
Comments
Richard MacCutchan 24-Oct-20 4:52am    
Please note this is an English language forum.
CHill60 24-Oct-20 7:42am    
CDate in C#?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900