Click here to Skip to main content
15,888,322 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi friends,

As i am new to C# .net, I have a DataGridView and i want to export all the columns of DataGridView To Excel file...
Posted
Comments
Sandeep Mewara 12-May-12 3:46am    
Some class assinment given today, looks like a hot question of the day!
EaveTina 24-Nov-15 2:06am    
While searching for a solution to this I found a direct way of exporting a DataGridView control into an excel file.
All you need to do is use this method, it is provided with this C# library for excel files.

Hi,
Import referecence namespace
microsoft.Office.Interop.Excel namespace and Microsoft.Office


C#
Microsoft.Office.Interop.Excel.ApplicationClass excApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            excApp.Application.Workbooks.Add(Type.Missing);
            excApp.Columns.ColumnWidth = 20;
            for (int i = 1; i < dgComplaints.Columns.Count+1; i++)
            {
                excApp.Cells[1, i] = dgComplaints.Columns[i - 1].HeaderText;
            }
            for (int i = 0; i < dgComplaints.Rows.Count ; i++)
            {
                for (int j = 0; j < dgComplaints.Columns.Count ; j++)
                {
                    excApp.Cells[i + 2, j + 1] = dgComplaints.Rows[i].Cells[j].Value.ToString();
                }
            }
            excApp.ActiveWorkbook.SaveCopyAs("C:\\"+DateTime.Now.ToString("ddMMyyyy")+".xls");
            excApp.ActiveWorkbook.Saved = true;
            excApp.Quit();
            MessageBox.Show("Report Saved...");
 
Share this answer
 
Here is some code I have used to export to excel:

C#
private static void Excel(string fileName, List<IDirectoryInventoryDataCollector> list)
{
    try
    {
        var xlApp = new Excel.Application();
        var xlWorkBook = xlApp.Workbooks.Add();
        var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        ExcelTitleRow(list[0], 1, xlWorkSheet);

        int row = 2;
        foreach (var item in list)
        {
            ExcelFillRow(item, row++, xlWorkSheet);
        }

        for (int i = 1; i < list[0].MaxLevel - 1; i++)
        {
            ((Range)xlWorkSheet.Columns[i]).ColumnWidth = 2;
        }
        ((Range)xlWorkSheet.Columns[list[0].MaxLevel - 1]).ColumnWidth = 30;
        ((Range)xlWorkSheet.Rows[1]).WrapText = true;
        ((Range)xlWorkSheet.Rows[1]).HorizontalAlignment = HorizontalAlignment.Center;
        ((Range)xlWorkSheet.Cells[1, 1]).WrapText = false;

        xlWorkBook.SaveAs(fileName);
        xlWorkBook.Close();
        xlApp.Quit();
    }
    catch (AccessViolationException)
    {
        System.Windows.Forms.MessageBox.Show(
             "Have encountered access violation. This could be issue with Excel 2000 if that is only version installed on computer",
             "Access Violation");
    }
    catch (Exception)
    {
        System.Windows.Forms.MessageBox.Show("Unknown error",
             "Unknown error");
    }
}

private static void ExcelFillRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, item.Level] = item.Name;
    int column = item.MaxLevel;
    foreach (var property in item.GetProperties())
    {
        sheet.Cells[row, column++] = property;
    }
}

private static void ExcelTitleRow(IDirectoryInventoryDataCollector item, int row, Excel.Worksheet sheet)
{
    sheet.Cells[row, 1] = "Name";
    int column = item.MaxLevel;
    foreach (var property in item.GetPropertyNames())
    {
        sheet.Cells[row, column++] = property;
    }
}
 
Share this answer
 
Comments
Maciej Los 12-May-12 18:38pm    
Good work! +5
 
Share this answer
 
v3
Comments
Sandeep Mewara 12-May-12 3:47am    
Some class assinment given today, looks like a hot question of the day!
5!
P.Salini 12-May-12 3:53am    
I think you are right Sandeep,some assignment is going on.
Thanks for voting.
Maciej Los 12-May-12 18:38pm    
Good links! +5
P.Salini 13-May-12 23:44pm    
Thank you losmac
 
Share this answer
 
Comments
Sandeep Mewara 12-May-12 3:47am    
Some class assinment given today, looks like a hot question of the day!
5!
sravani.v 12-May-12 3:53am    
Thank You Sandeep
P.Salini 12-May-12 4:05am    
my 5!
sravani.v 12-May-12 4:30am    
Thank you dear
Maciej Los 12-May-12 18:37pm    
Good links! +5

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