Click here to Skip to main content
Sign Up to vote bad
good
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:37
Member613.4K

Comments
Sandeep Mewara - 12 May '12 - 3:46
Some class assinment given today, looks like a hot question of the day!

4 solutions

  Permalink  
Comments
Sandeep Mewara - 12 May '12 - 3:47
Some class assinment given today, looks like a hot question of the day! 5!
sravani.v - 12 May '12 - 3:53
Thank You Sandeep
P.Salini - 12 May '12 - 4:05
my 5!
sravani.v - 12 May '12 - 4:30
Thank you dear
losmac - 12 May '12 - 18:37
Good links! +5
sravani.v - 13 May '12 - 23:35
Thank you losmac
  Permalink  
Comments
Sandeep Mewara - 12 May '12 - 3:47
Some class assinment given today, looks like a hot question of the day! 5!
P.Salini - 12 May '12 - 3:53
I think you are right Sandeep,some assignment is going on. Thanks for voting.
losmac - 12 May '12 - 18:38
Good links! +5
P.Salini - 13 May '12 - 23:44
Thank you losmac
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 - 12 May '12 - 18:38
Good work! +5
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 425
1 OriginalGriff 315
2 Slacker007 240
3 Aarti Meswania 210
4 Maciej Los 200
0 Sergey Alexandrovich Kryukov 8,893
1 OriginalGriff 7,134
2 CPallini 3,678
3 Rohan Leuva 3,036
4 Maciej Los 2,428


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 May 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid