2

I'm creating Excel like following:

     class Program
{
    private Excel.Application app = null;
    private Excel.Application worksheet = null;
    private Excel.Application workbook = null;
    private Excel.Application worksheet_range = null;

    //public Program()
    //{
    //    CreateExcelDoc();
    //}
    static void Main(string[] args)
    {
        Program obj = new Program();
        obj.CreateExcelDoc();
    }

    public void CreateExcelDoc()
    {
        try
        {
            app = new Excel.Application();
            if (app == null)
                return;
            app.Visible = true;
            workbook = app.Workbooks.Add(1);////error in this,saying "can not convert from Excel.Workbook to Excel.Application"

        }
        catch (Exception ex)
        {
            throw new Exception("CreateExcelDoc: Error" + ex.Message);
        }

    }
}

Could anyone pl tell where I'm going wrong? Note: I'm following this: http://msdn.microsoft.com/en-us/library/ms173186%28v=vs.80%29.aspx

Please send me any nice link/solution. I'm using Windows 7 64-bit OS.

Thanks.

1
  • Where do you declare the variable workbook and what type do you declare it as? Commented May 27, 2013 at 11:01

1 Answer 1

3

Change this declaration:

    private Excel.Application app = null;
    private Excel.Application worksheet = null;
    private Excel.Application workbook = null;
    private Excel.Application worksheet_range = null;

to:

    private Excel.Application app = null;
    private Excel.Worksheet worksheet = null;
    private Excel.Workbook workbook = null;
    private Excel.Range worksheet_range = null;

You may also follow exactly the code:

    app.Visible = true; // or false if you don't want to show app.
    workbook  = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
    worksheet = (Worksheet)workbook.Worksheets[1];

    // Select the Excel cells, in the range c1 to c7 in the worksheet.
    worksheet_range  = worksheet.get_Range("C1", "C7");

Edit

You can save your workbook with the following code:

    workbook.SaveAs(@"C:\XMLCopy.xls",
    Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
    false, false, Excel.XlSaveAsAccessMode.xlNoChange,
    missing, missing, missing, missing, missing);

but look for resources release methods after Workbook save. You need to close your file, your Application and all the involved objects.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! Now, no error and I can proceed.I hope with this I can create Excel Dynamically?
I do not want to make it visible, but rather to save dynamically it in particular location in disk drive.How can I do this? Also, next time when I'm going to create it application should check if the file exist or not.
These are simple task you can achieve with a little bit of MSDN and Google search. To hide your app just set app.Visible = false. For the other tasks check the link msdn.microsoft.com/en-us/library/vstudio/h1e33e36.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.