Click here to Skip to main content
14,424,243 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using a datagridview in my application and I have filled it with dataset which returns two columns ID and Name, but in gridview I'm displaying the name column alone hiding the ID column. Is it possible to export the rows alone from datagridview to excel. Eg If the follow. names appear as each row,
A
B
C
D

My excel should be in a format that each row must display as a column

A B C D

Code Which I have tried for export

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.CreatePrompt = true;
                saveFileDialog1.Title = "Export Excel File To";
                //saveFileDialog1.ShowDialog();
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {

                    worksheet = workbook.Sheets["Sheet1"];
                    worksheet = workbook.ActiveSheet;
                    worksheet.Name = "Exported from Data Template";
                    for (int i = 0; i < dgDtParameters.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < dgDtParameters.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, j + 1] = dgDtParameters.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                    fileName = saveFileDialog1.FileName;
                    workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    MessageBox.Show("Data template exported successfully","Data Template Export);
                }
                else
                    return;

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                app.Quit();
                workbook = null;
                app = null;
                GC.Collect();
            }
Posted
Comments
digimanus 26-Aug-11 6:07am
   
does your data come from an sqlserver 2005 or newer?
CyborgForever 26-Aug-11 6:08am
   
S it comes from SQL server 2008 express. I'm fillin the datagridview using dataset which in turn is returned through a stored procedure
CyborgForever 26-Aug-11 8:08am
   
I have got the concept of displaying the rows in horizontal manner in excel, but I dont know how to display only the column 2, but I need column 1 also in datagridview for validation purpose

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I was literally just reading an article about this 10 minutes ago I had similar issues(slightly different) have a read of this and see what you think:

Export Data to Excel, Word, PDF without Automation from DataBase[^]

also if you type it into google there are a host of sites that a really really good for db to excel and things like that try this one also:

http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]

It was really useful to me and a good resource
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100