Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
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 11-May-12 21:37pm
Rockstar_5.7K
Comments
Sandeep Mewara at 12-May-12 3:46am
   
Some class assinment given today, looks like a hot question of the day!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Sandeep Mewara at 12-May-12 3:47am
   
Some class assinment given today, looks like a hot question of the day!
5!
sravani.v at 12-May-12 3:53am
   
Thank You Sandeep
P.Salini at 12-May-12 4:05am
   
my 5!
sravani.v at 12-May-12 4:30am
   
Thank you dear
losmac at 12-May-12 18:37pm
   
Good links! +5
sravani.v at 13-May-12 23:35pm
   
Thank you losmac
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
v3
Comments
Sandeep Mewara at 12-May-12 3:47am
   
Some class assinment given today, looks like a hot question of the day!
5!
P.Salini at 12-May-12 3:53am
   
I think you are right Sandeep,some assignment is going on.
Thanks for voting.
losmac at 12-May-12 18:38pm
   
Good links! +5
P.Salini at 13-May-12 23:44pm
   
Thank you losmac
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Here is some code I have used to export to excel:
 
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;
    }
}
  Permalink  
Comments
losmac at 12-May-12 18:38pm
   
Good work! +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

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...");
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 489
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 PIEBALDconsult 150
0 OriginalGriff 7,755
1 Sergey Alexandrovich Kryukov 6,951
2 Maciej Los 3,994
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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