Hot File

Automating MS Excel Documents

View: 1847    Dowload: 0   Comment: 0   Post by: naruto  
Author: none   Category: MS Office   Fields: Other

0 point/3 review File has been tested

I have been working on automating MS Office applications since some time. I have seen that it is very easy to find information on automation of MS Word but it is difficult to find information on automation of PowerPoint or Excel. So I decided to write one and share the information with others.

Introduction

Using the code

Automation of Excel is the same as for Word. Create a new project, go ahead and right click on References in 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 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 ("");
}

Automating MS Excel Documents

Automating MS Excel Documents Posted on 12-05-2014  I have been working on automating MS Office applications since some time. I have seen that it is very easy to find information on automation of MS Word but it is difficult to find information on automation of PowerPoint or Excel. So I decided to write one and share the information with others. 5/10 1847

Comment:

To comment you must be logged in members.

Files with category

  • Document Library - Enable New Document, Edit Document Features

    View: 1872    Download: 0   Comment: 0   Author: none  

    Document Library - Enable New Document, Edit Document Features

    Category: MS Office
    Fields: Other

    0/5 review
    In this article we can explore the underlying aspects involved in enabling the New Document feature in a Document Library.

  • Generating Excel (XML Spreadsheet) in C#

    View: 2426    Download: 2   Comment: 0   Author: none  

    Generating Excel (XML Spreadsheet) in C#

    Category: MS Office
    Fields: Other

    0.5625/8 review
    Recently, I was working on a web based report in ASP.NET. The report had to be generated in MS Excel format and the data was too much. To give you an idea, I had to generate more than 300 worksheets and the file size was more than 5 MB. I tried the...

  • Searching - Human Way and Index Way

    View: 1942    Download: 0   Comment: 0   Author: none  

    Searching - Human Way and Index Way

    Category: MS Office
    Fields: Other

    0.093023255813953/86 review
    It was really an art to optimize the data retrieval from database and data retrieval optimization is part of every developer's life. Indexes are one of the best ways of Optimization that SQL Servers provide. Understanding indexes some time may be...

  • Creating Pivot Tables and Charts Using ActiveX

    View: 2507    Download: 0   Comment: 0   Author: none  

    Creating Pivot Tables and Charts Using ActiveX

    Category: MS Office
    Fields: none

    1.125/4 review
    Most of us (developers) use the “Generate Excel” feature in our applications. This allows us to generate an Excel copy on any of our reports. Most of us are also familiar with “Pivot Table” and “Pivot Charts”. This feature from MS Excel allows the...

  • Automating MS Excel Documents

    View: 1847    Download: 0   Comment: 0   Author: none  

    Automating MS Excel Documents

    Category: MS Office
    Fields: Other

    0/3 review
    I have been working on automating MS Office applications since some time. I have seen that it is very easy to find information on automation of MS Word but it is difficult to find information on automation of PowerPoint or Excel. So I decided to...

  • Embedding PowerPoint presentation player into a WPF application

    View: 952    Download: 0   Comment: 0   Author: none  

    Embedding PowerPoint presentation player into a WPF application

    Category: MS Office
    Fields: Other

    0/2 review
    On one occasion I came across an interesting and, I can even say, challenging task of building a customized player of PowerPoint presentations. This task emerged as part of a project which my teammates and I developed at Reliable Systems. Our...

  • Parallel Processing MS Access VBA Application

    View: 921    Download: 0   Comment: 0   Author: none  

    Parallel Processing MS Access VBA Application

    Category: MS Office
    Fields: Other

    0/1 review
    I got a challenging assignment to provide alarm capability on top of an existing application, written in VBA Access. This required a few issues to be dealt with and resolved. I will, in this article, describe the steps I took to provide parallel...

  • Microsoft Office XML formats, defective by design?

    View: 3477    Download: 0   Comment: 0   Author: none  

    Microsoft Office XML formats, defective by design?

    Category: MS Office
    Fields: Other

    0/549 review
    Microsoft is trying to push new file formats that are using Zip and XML. Are those new file formats any good for Office developers? In other words, should anyone feel safe to make direct access to file parts, and start getting free of running...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd