C# read open Excel file through OleDb

孤街醉人 提交于 2019-12-01 06:55:45

the question seems have no answer. and I cant delete it....

my solution was - run macro on timer to save the excel file in question and C# app was copying the file to another one and reading it using OleDb.

Kristoffer Lindvall

This seems like a similar problem: Writing into excel file with OLEDB

Does that work out for you?

What parameters are you passing in when you open the Excel document? Could you set the "ReadOnly" parameter in Workbook.Open() to true? See here.

Pavan

Refer to the code below how to get the information of Excel data into an array. Then you will perform any validations on that Excel sheet.

var fileName = @"D:\Pavan\WorkDiployed.xlsx";
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 12.0;", fileName);
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", con);

con.Open();
System.Data.DataSet excelDataSet = new DataSet();
cmd.Fill(excelDataSet);
DataTable data = excelDataSet.Tables[0];

DataRow[] arrdata = data.Select();

foreach (DataRow rw in arrdata)
{
    object[] cval = rw.ItemArray;
}           

con.Close();
MessageBox.Show (excelDataSet.Tables[0].ToString ()); 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!