Click here to Skip to main content
6,635,160 members and growing! (19,644 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

Automating MS Excel Documents

By Govardhana Reddy

This article will help and demonstrate how to automate and get content from a MS Excel Documents.
C# 1.0, C# 2.0, Windows, Office, .NET 1.0, .NET 1.1, .NET 2.0, ASP.NET, WinForms, WebForms, VS.NET2003, VS2005, Dev
Posted:25 Oct 2007
Views:5,342
Bookmarked:6 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.30 Rating: 1.00 out of 5
2 votes, 100.0%
1

2

3

4

5

Introduction

I had been working on automating MS Office application since some time. I have seen that its very easy to find information on automation of MS Word but its difficult to find the information on automation of others like Power Point, Excel.

So I decided to write one by myself and share the information with others.

Using the code

Automation of the Excel is same as Word. Create a new project, go ahead and right click on References in the Solution Explorer, and select Add Reference� When the Add Reference window comes up select the COM tab. This will list all Component Names which are available on your machine, since we are going to use MS Excel, we will scroll down until we find: Microsoft Excel Object Library.

The following code will help u understand the rest of the automation:

public string excelExtract(string path)
    {
        object ex_missing = System.Reflection.Missing.Value;

        object ex_visible = true;
        object ex_false = false;
        object ex_true = true;

        object ex_update_links = 0;
        object ex_read_only = ex_true;
        object ex_format = 1;
        object ex_password = "Govardhan";
        object ex_write_res_password = ex_missing;
        object ex_ignore_read_only_recommend = ex_true;
        object ex_origin = ex_missing;
        object ex_delimiter = ex_missing;
        object ex_editable = ex_false;
        object ex_notify = ex_false;
        object ex_converter = 0;
        object ex_add_to_mru = ex_false;
        object ex_local = ex_false;
        object ex_corrupt_load = ex_false;

        object ex_save_changes = ex_false;
        object ex_route_workbook = ex_false;

        bool errorFlag = false;


        Excel.Application excelApp = null;
        Excel.Workbook excelWorkbook = null;
        Excel.Sheets excelSheets = null;
        Excel.Worksheet excelWorksheet = null;

        excelApp = new Excel.ApplicationClass();
        excelApp.Visible = false;

        object missing = System.Reflection.Missing.Value;

        string fileName = path;

        string heading = null;

        string content = null;

        try
        {

            excelWorkbook = excelApp.Workbooks.Open(
                fileName, ex_update_links, ex_read_only, ex_format, ex_password,
                ex_write_res_password, ex_ignore_read_only_recommend, ex_origin,
                ex_delimiter, ex_editable, ex_notify, ex_converter, ex_add_to_mru,
                ex_local, ex_corrupt_load);



            string startUpPath = path.Substring(0, path.Length - 4); 

            excelSheets = excelWorkbook.Worksheets;

            int count = excelSheets.Count;

            if (excelSheets != null)
            {

                for (int i = 1; i <= count; i++)
                {

                    string sheetName = startUpPath + "exc_text_" + i + ".txt";

                    excelWorksheet = (Excel.Worksheet)excelSheets.get_Item((object)i);

                    excelWorksheet.Activate();

                    excelWorksheet.SaveAs(sheetName, Excel.XlFileFormat.xlTextWindows, missing, missing, missing, missing, missing, missing, missing, missing);
                }

                excelWorkbook.Close(false, missing, missing);


                for (int i = 1; i <= count; i++)
                {

                    heading = "\r\n\r\n" + "                EXCEL WORKSHEET " + i + "                " + "\r\n\r\n";

                    content += heading;

                    string sheetName = startUpPath + "exc_text_" + i + ".txt";

                    FileStream fs = new FileStream(sheetName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                    StreamReader sr = new StreamReader(sheetName);

                    content += sr.ReadToEnd().ToString();

                    sr.Close();

                    fs.Close();

                    File.Delete(sheetName);

                }
            }
        }
        catch (System.Exception error)
        {
            string temp = error.Message.ToString();
            errorFlag = true;
          
            if (excelApp != null)
                excelApp.Quit();


        }
        finally
        {

            if (excelApp != null)
                excelApp.Quit();

        }

        if (!errorFlag)
        {
            return (content);
        }
        else
            return ("");
    }
 

Points of Interest

I want to mention one thing i have used Office XP COM object and this can be used with MS Office 2003 and 2007

Good Luck guys...

License

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

About the Author

Govardhana Reddy


Member
This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.

I want to be one of the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.)

My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".

Recently came up with a software firm Gyaan Solutions which provide a user-centric, web-based learning management system (LMS) for the delivery of online learning and teaching

Anyways long road ahead...

You can find more articles on http://www.apondu.50megs.com
Occupation: Founder
Company: Gyaan Solutions
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Oct 2007
Editor:
Copyright 2007 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project