Link to home
Start Free TrialLog in
Avatar of JAaron Anderson
JAaron AndersonFlag for United States of America

asked on

HowTo code C# to read an Excel File

I am running local VS Express 2013 on Win7:64bit leveraging MSExcel 14.0 Object Library Interop Library added to Solution Reference.

I am only finding conflicting code for RAZOR shorthand for my .aspx.cs file
I am trying to code the following procedural loop to step through reading an adjacent excel file.

please help me identify why my worksheet object hints at missing some form of casting (Im too n00b)

thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;

public partial class CMS_Application : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e){

       [i] // \/ Here is my code Im trying to create an Excel application here in C# for a web.ui .page:[/i]
        Excel.Application xlApp = new Excel.Application();
        [i]// \/ create the workbook [ container ][/i]
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\\ASP_OLEDB_NET\\cms.xls");
        [i]// \/ create the worksheet [/i]
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];

      [i]  // \/ Selected the range that was used [/i]
        Excel.Range xlRange = xlWorksheet.UsedRange;

        [i]// \/ Create two integers to contain the count of Columns and Rows. [/i]
        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;

        // RAZOR simple programming syntax embedding server code in web pages 
        // statement blocks for RAZOR seem to not work at all . confused by its implementation
        //@{ var myMessage = "Hello World"; } // ERRORS out 'String expected after verbatim specifier'

       // \/ C# Forloop over the entire data set get values
        for (int i=1; i <=rowCount; i++) {
            // testing w/ concole write line
            Console.WriteLine(i);
                for(int j=1; j <= colCount; j++){
                    Response.Write(xlRange.Cells[i,j].Value2.ToString());
                    // /\ Not sure how to write syntax for above line
                }
        }
    }// end Page_Load
}// end class

Open in new window

SOLUTION
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JAaron Anderson

ASKER

Im getting too many debugging errors #overwhelmed .

perhaps forget any of my feeble code approach agh

I rethought it out perhaps its simpler if I read a csv ? but I need to still control the cells of data and have a way to know which one is which like a key/value pair so I can nest data nuggets into appropriate areas of the html display format .

make sense ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I learned that Excel in Office  is required to be running localhost.
If this is true (makes sense then you could leverage calls to the framework) Ill have to abandon entirely and move to reading in .csv its simpler...  best, jaa
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
would handling the XLS or XLSX file be the same essentially as handling a CSV ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial