From C# to Excel 2007

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rshin2020
    New Member
    • Dec 2009
    • 26

    From C# to Excel 2007

    Hi. Here's what i have done:

    Code:
    using System;
    using System.Collections;
    using System.Reflection; // For Missing.Value and BindingFlags
    using System.Runtime.InteropServices; // For COMException
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace MSExcelApp
    {
        /// <summary>
        /// Summary description for ExcelAuto.
        /// </summary>
        public class ExcelAuto
        {
            private Excel.Application ExcelApp;
            private Excel.Workbook objBook;
            private Excel.Worksheet objSheet;
            private Excel.Range range;
            string strAttendeeList, strAbsenteeList, strCopiesToList;
            int totActionItems;
            object oMissing, oTemplate;
            private string strTitle;
    
            public ExcelAuto()
            {
                //
                // TODO: Add constructor logic here
                //
            }
            public void CreateFile(ArrayList array)
    		{
    			object missing = System.Reflection.Missing.Value;
    			object fileName = "normal.dot";
    			object newTemplate = false;
    			object docType = 0;
    			object isVisible = true;
    			
    			ExcelApp = new Excel.ApplicationClass();
    			ExcelApp.Visible = true;
    			objBook = ExcelApp.Workbooks.Add(missing);
    			objSheet = (Excel.Worksheet)objBook.Sheets["Sheet1"];
    			objSheet.Name = "It's Me";
    
               objSheet.Cells[1, 1] = "Details";
    			objSheet.Cells[2, 1] = "Voltage : "+ array[0].ToString();
    			objSheet.Cells[3, 1] = "EmployeeID : "+ array[1].ToString();
    
                objSheet.get_Range("A1", "A1").Font.Bold = true;
    			objSheet.get_Range("A1", "A6").EntireColumn.AutoFit();
    			objSheet.get_Range("A1","A7").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlMedium,
    							Excel.XlColorIndex.xlColorIndexAutomatic,Excel.XlColorIndex.xlColorIndexAutomatic);
            }
    
                static void Main() 
    		{
                Application.Run(new Form1());
    		}
    
    		private void btnCreate_Click(object sender, System.EventArgs e)
    		{
    			ExcelAuto excel = new ExcelAuto();
    			ArrayList array = new ArrayList();
    			bool filled = true;
    			
    			if ( ( 1.016).ToString().Length.Equals(0) )
    			{
    				errorProvider.SetError(1.016,"cannot be empty" );
    				filled = false;
    			}
    			if( ( 00180000 ).ToString().Length.Equals(0) )
    			{
    				errorProvider.SetError( 00180000,"cannot be empty" );
    				filled = false;
    			}
    			
    			
    			if ( filled == true )
    			{
    				array.Insert(0,1.016);
    				array.Insert(1,00180000);
    				
    	
    				excel.CreateFile(array);
    			}
    		}
    
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			Application.Exit();
    		}
    	}
    }
    I have to convert the data of voltage and employeeeID, 1.016 and 00180000 respectively to be displayed in excel. It has to be in console. Can you please check the code. There's some errors but i don't know how to mend them. Thanks.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    There's some errors but i don't know how to mend them. Thanks.
    Help the volunteers to help you: What are the errors and on what line #'s?

    Comment

    • rshin2020
      New Member
      • Dec 2009
      • 26

      #3
      Line 65: Class, struct, or interface method must have a return type
      Line 133: Type or namespace definition, or end-of-file expected

      All I need is to display the data in excel. It has to be in console not windows application. Thanks.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Line 65: Class, struct, or interface method must have a return type
        Line 133: Type or namespace definition, or end-of-file expected
        You don't have 133 lines of code here. There are only 93 if you count the last to blank lines.

        Comment

        • rshin2020
          New Member
          • Dec 2009
          • 26

          #5
          the last one refers to the curly braces at the end. I dont know whats wrong there.

          Comment

          Working...