Home » excel » C# Open new Excel Workbook and Add Data

C# Open new Excel Workbook and Add Data

Posted by: admin May 14, 2020 Leave a comment


From a console application using C# I am trying to open a new Excel workbook and add data to it. I can open a new workbook fine, but I am running into issues adding data to the workbook because my range object is null, and I can’t seem to hook into the excel workbook that was just opened. I tried a variation of ActiveWorkbook, Sheets[1] and a couple of others and I can’t seem to figure it out

using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            Application xl = null;
            _Workbook wb = null;

            // Option 1
            xl = new Application();
            xl.Visible = true;
            wb = (_Workbook)(xl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet));

            Worksheet sheet = xl.ActiveWorkbook.ActiveSheet;
            Range cell = sheet.Cells[1, 1];
            //ERROR on cell.Value("Test");
How to&Answers:

This code may be help you.

using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xls" })
                if (sfd.ShowDialog() == DialogResult.OK)
                    // creating Excel Application  
                    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                    // creating new WorkBook within Excel application  
                    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                    // creating new Excelsheet in workbook  
                    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                    // see the excel sheet behind the program  
                    app.Visible = true;
                    // get the reference of first sheet. By default its name is Sheet1.  
                    // store its reference to worksheet  
                    worksheet = workbook.Sheets["Sheet1"];
                    worksheet = workbook.ActiveSheet;
                    // changing the name of active sheet  
                    worksheet.Name = "Exported from gridview";
                    // storing header part in Excel  
                    for (int i = 1; i < DGView.Columns.Count + 1; i++)
                        worksheet.Cells[1, i] = DGView.Columns[i - 1].HeaderText;
                    // storing Each row and column value to excel sheet  
                    for (int i = 0; i < DGView.Rows.Count - 1; i++)
                        for (int j = 0; j < DGView.Columns.Count; j++)
                            worksheet.Cells[i + 2, j + 1] = DGView.Rows[i].Cells[j].Value.ToString();
                    // save the application  
                    workbook.SaveAs(sfd.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);
                    // Exit from the application  

I used this code in my work for saving datagridview as excel sheet and it has been worked well.