Sunday, August 7, 2011

Writing in Excel through C#


Follow the following steps to writing data into Excel through C#:-

1) Add references Interop.Excel.dll in the project (first you need to download this dll)
2) Add this function in the form

using Excel;
using Microsoft;

void excel()
        {
            Excel._Application app;
            Excel.Workbook Wb;
            Excel.Worksheet Osheet;
            Range oRng;
            try
            {
                app = new Excel.Application();
                app.Visible = false;
                //Get a new workbook.
                Wb = (Excel.Workbook)(app.Workbooks.Add(1));
                Osheet = (Excel.Worksheet)Wb.ActiveSheet;
               
                //First row: Header
                for (int i = 1; i <= dataGridView1.Columns.Count; i++)
                    Osheet.Cells[1, i] = dataGridView1.Columns[i - 1].Name;
               
                //Filling Sheet with desired value except Header
                for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                        Osheet.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                                      
                        //    oRng.Font.Italic = true;
                        //    oRng.Interior.ColorIndex = 15; //     //http://dmcritchie.mvps.org/excel/colors.htm : color indexes list
                                               
                //Setting Font of each Column
                String[] columns = ExcelFont.Split('^');
                for (int i = 1; i <= columns.Length; i++)
                    if (columns[i - 1] == "h")
                    {
                        ((Range)Osheet.Cells[2, i]).EntireColumn.Font.Name = "Kruti Dev 010";
                        ((Range)Osheet.Cells[2, i]).EntireColumn.Font.Size = "12";
                    }
                        
                //First Row: Header
                oRng = Osheet.get_Range(Osheet.Cells[1, 1], Osheet.Cells[1, dataGridView1.ColumnCount]);
                oRng.EntireRow.Font.Name = "Arial";
                oRng.EntireRow.Font.Size = "10";
                oRng.Font.Bold = true;
                //Entire Sheet
                oRng.EntireColumn.AutoFit();

                //Border
                oRng = Osheet.get_Range(Osheet.Cells[1, 1], Osheet.Cells[dataGridView1.RowCount, dataGridView1.ColumnCount]);
                oRng.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                          
                // app.Visible = false;
                app.UserControl = false;
                // Need all following code to clean up and remove all references!!!
                Wb.Close(null, null, null);
                app.Workbooks.Close();
                app.Quit();
            }

            catch (Exception theException)
            {
                //MessageBox.Show(theException.Message);
            }
        }

No comments:

Post a Comment