Monday, September 19, 2011

c# and excel: editing a worksheet programatically

This tiny piece of code will generate an excel sheet, edit it and finally save it. This code contains a fix for non-English languages (which generates an exception unless you have it).



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // creating Excel Application
         
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();

            //workaround for non-english culture exceptions
            app.Visible = false;
            app.UserControl = false ;
            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
         

            // 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;
            app.DefaultSheetDirection =(int) Excel.Constants.xlLTR;


            // get the reference of first sheet. By default its name is Sheet1.
            // store its reference to worksheet
            worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets["Sheet1"];
            worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet;
         
            //edit the sheet:

         // Excel object references.
         Excel.Application m_objExcel =  null;
         Excel.Workbooks m_objBooks = null;
         Excel._Workbook m_objBook = null;
         Excel.Sheets m_objSheets = null;
         Excel._Worksheet m_objSheet = (Microsoft.Office.Interop.Excel._Worksheet)worksheet;
         Excel.Range m_objRange =  null;
         Excel.Font m_objFont = null;
         Excel.QueryTables m_objQryTables = null;
         Excel._QueryTable m_objQryTable = null;
         // Frequenty-used variable for optional arguments.
         object m_objOpt = System.Reflection.Missing.Value;



            //set col names
         m_objRange = m_objSheet.get_Range("A1", "J1");

         m_objRange.VerticalAlignment = -4108; // center

         m_objRange = m_objSheet.get_Range("A1", m_objOpt);

         m_objRange.set_Value(m_objOpt, "ID");

         m_objRange = m_objSheet.get_Range("B1", m_objOpt);

        m_objRange.set_Value(m_objOpt, "Account Type");

        m_objRange = m_objSheet.get_Range("C1", m_objOpt);

        m_objRange.set_Value(m_objOpt, "Account Number");



         
         // add data - + 2 for header and blank row

        for (int i = 0; i < 10; i++)
        {

            int intRow = i + 2;

            m_objRange = m_objSheet.get_Range("A" + intRow, m_objOpt);

            m_objRange.set_Value(m_objOpt, i.ToString());

        }
         
            // changing the name of active sheet
            worksheet.Name = "my name";
         
            /*          
            //Send email - this is problematic as outlook blocks this.
            string recipients = "amib@radvision.com";
            string subject = "Proposal for Review";
            bool returnReceipt = false;          
            workbook.SendMail(recipients, subject, returnReceipt);
             */
         
            // save the application

            workbook.SaveAs("c:\\output1.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing);

            // Exit from the application
            app.Quit();
         
        }
    }
}

1 comment:

  1. additional details @ MS:
    http://support.microsoft.com/kb/306023

    ReplyDelete