lancerxe
asked on
Reading an Excel file in C#
Hello experts:
I'm trying to load the load test.xls contents thru c# code so that I can update some data base fields:
My code is below:
public class BankParserClass
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlW BATemplate .xlWBATWor ksheet);
( this line of code gets an error here on excelApp : denotes a fileld where a class is expected)
string workbookPath = "c:/test.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(wo rkbookPath ,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows , "",
true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelShee ts.get_Ite m(currentS heet);
Excel.Range excelCell =(Excel.Range)excelWorkshe et.get_Ran ge("A1", "A1");
I got other errors but I think they are related to the 1st error above.
Thanks
I'm trying to load the load test.xls contents thru c# code so that I can update some data base fields:
My code is below:
public class BankParserClass
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook newWorkbook = excelApp.Workbooks.Add(XlW
( this line of code gets an error here on excelApp : denotes a fileld where a class is expected)
string workbookPath = "c:/test.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(wo
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows
true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelShee
Excel.Range excelCell =(Excel.Range)excelWorkshe
I got other errors but I think they are related to the 1st error above.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There is one easy of reading excel file from .Net application.
string strFilePath = "C:\A.xls";
string excelConnectionString = "Dsn=Excel Files;dbq=" + strFilePath + ";defaultdir=" + ";driverid=790;maxbuffersi ze=2048;pa getimeout= 5";
//Load Open detals
using (OdbcConnection connection = new OdbcConnection(excelConnec tionString ))
{
try
{
connection.Open();
OdbcCommand command = new OdbcCommand("Select [ID], [Status],[Created] FROM [Open$]", connection);
OdbcDataAdapter da = new OdbcDataAdapter(command);
da.Fill(dsSummary, "Open");
connection.Close();
}
catch
{
connection.Close();
}
string strFilePath = "C:\A.xls";
string excelConnectionString = "Dsn=Excel Files;dbq=" + strFilePath + ";defaultdir=" + ";driverid=790;maxbuffersi
//Load Open detals
using (OdbcConnection connection = new OdbcConnection(excelConnec
{
try
{
connection.Open();
OdbcCommand command = new OdbcCommand("Select [ID], [Status],[Created] FROM [Open$]", connection);
OdbcDataAdapter da = new OdbcDataAdapter(command);
da.Fill(dsSummary, "Open");
connection.Close();
}
catch
{
connection.Close();
}
Calling Com services is very slow process becaue it is remote procedure call .
opening OBdc connection or OLedb is an effeicent way you can easily filter data as want but in
excel object it iterate row by row and cause increase in condition or check you apply for filter.
In calling Excel object cause some problem while closing its object , if program breaks without closing excel
object it remain open until you by force kill them from task manager.
opening OBdc connection or OLedb is an effeicent way you can easily filter data as want but in
excel object it iterate row by row and cause increase in condition or check you apply for filter.
In calling Excel object cause some problem while closing its object , if program breaks without closing excel
object it remain open until you by force kill them from task manager.
regs,
yurich