C# Struggling to read excel data into arrays (interop) -


first off apologize have asked before never got answer due posting @ quiet time of day.

i'm trying read data xlsx document columns array lists on form based program can call in various bits of data manipulate or show onscren can't code work. searched forums , found according poster works i'm having problems. i'm literally stuck on matter i've never had write code involved excel.

the errors throws words "server, missing & session" not exist in current context. i'm aware session isn't part of winforms applications dont know it.

using microsoft.office.interop.excel; 

this main code

string filepath = "sampledata.xlsx";  try {    excel.application appexl;    excel.workbook workbook;    excel.worksheet nwsheet;    excel.range shtrange;    appexl = new excel.application();    workbook = appexl.workbooks.open(filepath);    workbook = appexl.workbooks.open(server.mappath(filepath), missing.value,         missing.value,    missing.value, missing.value, missing.value, missing.value, missing.value, missing.value,    missing.value, missing.value, missing.value, missing.value, missing.value, missing.value);    nwsheet = (excel.worksheet)workbook.sheets.get_item(1);     int cnum = 0;    int rnum = 0;     shtrange = nwsheet.usedrange;    datatable dt = new datatable();     (int row = 1; row <= shtrange.rows.count; row++)    {       string str = convert.tostring((shtrange.cells[row, 1] excel.range).value2);       dt.columns.add(str);    }     workbook.close(true);    appexl.quit();     session["data"] = dt;     return true; }  catch (exception ex) {    return false; } 

is better representation of should like?

 using excel = microsoft.office.interop.excel;  using system.reflection;   string filepath = "sampledata.xlsx";      excel.application appexl;     excel.workbook workbook;     excel.worksheet nwsheet;     excel.range shtrange;     appexl = new excel.application();     workbook = appexl.workbooks.open(filepath);      nwsheet = (excel.worksheet)workbook.sheets.get_item(1);      int cnum = 0;     int rnum = 0;      shtrange = nwsheet.usedrange;     datatable dt = new datatable();      (int row = 1; row <= shtrange.rows.count; row++)     {        string str = convert.tostring((shtrange.cells[row, 1] excel.range).value2);        dt.columns.add(str);     }      workbook.close(true);     appexl.quit(); 

i believe need add following references project:

system.web system.reflection 

you may need add following code:

using system.web using system.reflection 

if winforms application, won't need system.web reference , you'll need replace 'server.mappath(filepath)' filepath instead of 'session["data"]' want use datagrid.

also looks trying open file twice in succession. not sure why.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -