ExcelInterop.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Excel = Microsoft.Office.Interop.Excel;

using System.Reflection;    //Missing.Value

using System.Windows.Forms; //Messagebox

 

namespace MesUtilities

{

    class ExcelInterop

    {

 

        #region member fields

 

        Excel.Application objApp;

        Excel._Workbook objBook;

        Excel.Workbooks objBooks;

        Excel.Sheets objSheets;

        Excel._Worksheet objSheet;

 

        #endregion

 

        #region helper methods

 

 

        /// <summary>

        /// Using the pre-existing class object objSheet,

        /// write a row of data to the specified row number.

        /// </summary>

        /// <param name=”args”></param>

        /// <param name=”rowNumber”></param>

        public void ExcelWriteToWorksheet(string[] args, int rowNumber)

        {

            Excel.Range range;

 

            rowNumber += 2; //Convert from 0-based counting to 1-based counting for Excel, and also skip the header row.

            int rowCount = 1;

            int colCount = args.Count();

 

            try

            {

                //Get the range where the starting cell has the address

                //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.

                range = objSheet.get_Range(String.Format(“A{0}”, rowNumber), Missing.Value);

                range = range.get_Resize(rowCount, colCount);

 

                //Set the range value to the array.

                range.set_Value(Missing.Value, args);

 

            }

            catch (Exception theException)

            {

                String errorMessage;

                errorMessage = “Error: “;

                errorMessage = String.Concat(errorMessage, theException.Message);

                errorMessage = String.Concat(errorMessage, ” Line: “);

                errorMessage = String.Concat(errorMessage, theException.Source);

 

                MessageBox.Show(errorMessage, “Error”);

            }

        }

 

        /// <summary>

        /// Close the Excel Book object (and its Sheets).

        /// Quit Excel.

        /// </summary>

        public void ExcelTerminate()

        {

            objBook.Close(true, Type.Missing, Type.Missing);

            objBooks.Close();

            objApp.Quit();

            objApp = null;

        }

 

        /// Overloaded version to default file format to XLSX.

        public void ExcelCreateFile(string filename, string[] aryHeaders )

        {

            ExcelCreateFile( filename, aryHeaders, System.Type.Missing);

        }

 

        /// <summary>

        /// Create a new Excel file,

        /// and the associated class objects for use by the other ExcelInterop methods.

        /// File formats: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat(office.11).aspx

        /// </summary>

        /// <param name=”filename”></param>

        /// <param name=”aryHeaders”></param>

        /// <param name=”fileFormat”></param>

        public void ExcelCreateFile(string filename, string[] aryHeaders, object fileFormat)

        {

 

            try

            {

 

                // Instantiate Excel and start a new workbook.

                objApp = new Excel.Application();

                objBooks = objApp.Workbooks;

 

                /*

                 * Create / Open the workbooks.

                 * Must create it, save it as XSLX, close it, then re-open it.

                 * If that is not done, it opens in COMPATIBILITY MODE and

                 * and restricts the row count to 65,536.

                 *

                 * This close/reopen trick also works for other file types such as CSV and HTML.

                 * */

                objBook = objBooks.Add(Missing.Value);

                ExcelSaveAs(objBook, filename, fileFormat);

                objBook.Close(true, Type.Missing, Type.Missing);

                ExcelOpenWorkbook(objBooks, filename);

                objBook = objBooks[1];

                objSheets = objBook.Worksheets;

                objSheet = (Excel._Worksheet)objSheets.get_Item(1);

 

                /*                

                                string[] aryHeaders = {

                                    “Count”

                                    ,”Serial No”

                                    ,”Part No”

                                    ,”Shop Order No”

                                    ,”IFS MfgRev”

                                    ,”IFS EngRev”

                                    ,”MES MfgRev”

                                    ,”MES EngRev”

                                    ,”S/N Discrepancy Flag”

                                    ,”Is Product in MES Flag”

                                    ,”Action”

                                    };

                */

                // Write the header record

                ExcelWriteToWorksheet(aryHeaders, -1);

 

                //objBook.Save();

            }

            catch (Exception e)

            {

                throw new Exception(“ExcelCreateFile failed to create the file.\nDetails:\n” + e.ToString());

            }

        }

Perricone on the Oprah show, and dubbed “The worlds viagra 100 mg opacc.cv #1 superfood”. Startingwithin our twenties, the pituitary gland generatesmuch less and less HGH that contributes to somelack ofgood qualitysleep as opacc.cv on line levitra we age. sildenafil 25mg The meetings can be scheduled without any drastic changes in production time. But this thought is purely wrong viagra for sale and this medicine is helpful to remove your problem of early ejaculation.  

        /// <summary>

        /// Overloaded version to default file format to XLSX.

        /// </summary>

        /// <param name=”workbook”></param>

        /// <param name=”filename”></param>

        private void ExcelSaveAs(Excel._Workbook workbook, string filename)

        {

            ExcelSaveAs(workbook, filename, System.Type.Missing);

        }

        /// <summary>

        /// Given a workbook,

        /// a filename,

        /// and a file format,

        /// Save the workbook as the filename, using the fileformat.

        /// </summary>

        /// <param name=”workbook”></param>

        /// <param name=”filename”></param>

        /// <param name=”fileFormat”></param>

        private void ExcelSaveAs(Excel._Workbook workbook, string filename, object fileFormat)

        {

 

            #region ForFutureReference

 

            /*

            //from http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.saveas(office.11).aspx

            // see also http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.saveas(v=office.14).aspx

            // see also http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlfileformat(office.11).aspx

            //Microsoft.Office.Interop.Excel.XlFileFormat fileFormat = Excel.XlFileFormat.xlExcel12;

            object fileFormat = Excel.XlFileFormat.xlOpenXMLWorkbook;

            object password = “”;

            object writeResPassword = “”;

            object readOnlyRecommended = false;

            object createBackup = false;

            Excel.XlSaveAsAccessMode accessMode = Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive;

            object conflictResolution = Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution;

            object addToMru = false;

            object textCodePage = new object();

            object textVisualLayout = new object();

            object local = new object();

            */

 

            #endregion

 

 

            // Object fileFormat = System.Type.Missing; // Refactored to be a Parameter.

            Object password = System.Type.Missing;

            Object writeResPassword = System.Type.Missing;

            Object readOnlyRecommended = System.Type.Missing;

            Object createBackup = System.Type.Missing;

            Excel.XlSaveAsAccessMode accessMode = Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive;

            Object conflictResolution = System.Type.Missing;

            Object addToMru = System.Type.Missing;

            Object textCodePage = System.Type.Missing;

            Object textVisualLayout = System.Type.Missing;

            Object local = System.Type.Missing;

 

            workbook.SaveAs(

                filename,

                fileFormat,

                password,

                writeResPassword,

                readOnlyRecommended,

                createBackup,

                accessMode,

                conflictResolution,

                addToMru,

                textCodePage,

                textVisualLayout,

                local);

        }

 

        private void ExcelOpenWorkbook(Excel.Workbooks workbooks, string filename)

        {

 

            #region ForFutureReference

            /*           

            // from http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open.aspx

            Object updateLinks = 0; //If Microsoft Excel is opening a file in the WKS, WK1, or WK3 format and the UpdateLinks argument is 2, Microsoft Excel generates charts from the graphs attached to the file. If the argument is 0, no charts are created.

            Object readOnly = false;

            Object format = Excel.XlFileFormat.xlOpenXMLWorkbook;

            Object password = “”;

            Object writeResPassword = “”;

            Object ignoreReadOnlyRecommended = false;

            Object origin = Missing.Value;

            Object delimiter = Missing.Value;

            Object editable = true;

            Object notify = false;

            Object converter = 1;

            Object addToMru = false;

            Object local = true;

            Object corruptLoad = false;

*/

            #endregion

 

            Object updateLinks = System.Type.Missing;

            Object readOnly = System.Type.Missing;

            Object format = System.Type.Missing;

            Object password = System.Type.Missing;

            Object writeResPassword = System.Type.Missing;

            Object ignoreReadOnlyRecommended = System.Type.Missing;

            Object origin = System.Type.Missing;

            Object delimiter = System.Type.Missing;

            Object editable = System.Type.Missing;

            Object notify = System.Type.Missing;

            Object converter = System.Type.Missing;

            Object addToMru = System.Type.Missing;

            Object local = System.Type.Missing;

            Object corruptLoad = System.Type.Missing;

 

            try

            {

                workbooks.Open(

                    filename,

                    updateLinks,

                    readOnly,

                    format,

                    password,

                    writeResPassword,

                    ignoreReadOnlyRecommended,

                    origin,

                    delimiter,

                    editable,

                    notify,

                    converter,

                    addToMru,

                    local,

                    corruptLoad);

 

            }

            catch (Exception e)

            {

                MessageBox.Show(“Exception ” + e.Message + ” Stack Trace: ” + e.StackTrace.ToString());

            }

        }

 

        #endregion

 

    }

}

 

This entry was posted in Parenting and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.