 |
|
 |
I wrote a windows form application in C# that uses ADO.Net to read from an Excel file. I receive xls files but have to manually save them as xlsx files because I can't get the program to read xls files (I get "External table is not in the expected format"). As an aside, for broadest .net compatibility, I set each of the Projects (which create assemblies/dlls) for the application to .net framework 2.0.
I have 2 questions: 1. How can I get my program to read xls files when Excel 2007 is installed? 2. How can I support environments where Excel 2002 and Excel 2003 are installed - I assume different connection strings and have the users ONLY open xls files?
Code Snippet: DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); DbConnection connection = factory.CreateConnection(); connection.ConnectionString = "whatever"; connection.Open(); Connection strings I've tried:
// The first one worked for xlsx files w/ Excel 2007 installed: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFile.xls;Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myFile.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;"" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myFile.xls;Extended Properties=""Excel 5.0;HDR=Yes;IMEX=1;""
|
| Sign In·View Thread·PermaLink | 2.67/5 (5 votes) |
|
|
|
 |
|
 |
If u purely download and try to run the project it will give you a debugging error saying that there is no such file exists ( a runtime error) so what i basically did was change this line of default code to this THIS string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Members.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
To string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\FYP\examplefollow\examplefollow\Members.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
as u can see that i have specified the whole file path i wonder if this is a bug that the program cnnot directly find it in a directory For newbies like me take note that examplefollow is my solution name and the whole path is only to my computer and not to yours so change it to the path on your computer
and dun forget to thank to MIDAH for giving this wonderful tutorial
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
In C# the connectionString would be like
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\FYP\examplefollow\examplefollow\Members.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;\"";
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am using Excel 2007 and currently using OLEDB driver to read excel sheet. The problem I am having is - the resultant record set only reads data untill 255 columns. Although Excel 2007 allows far greater column limit, I am unable to utilize this facility. Any suggestion regarding this will be highly appreciated!
Following is the connection string I am using for OLEDB "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';";
And then use the olddb dataadabter: da = new OleDbDataAdapter("SELECT * FROM [MyWork1$]", con); da.Fill(ds);
Thanks in advance, Fad
|
| Sign In·View Thread·PermaLink | 4.50/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Can you elaborate more on this?
I am new reading Excel files and I dont quite understand the From part on you query.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Greetings,
I also have an issue with the 255 columns limitations on Excel 2007 files. My file contains over 2700 columns. When I query the file using "Microsoft.ACE.OLEDB.12.0" I only get 255 columns. I searched the Web and nothing comes up.
Any ideas?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
I tried reading excel file using Timer_Tick for every 5 seconds (used OLEDB and VB.NET 2005) so that I can update the data in DataGridView with the latest data from Excel. It is continuous process that I need to look at the excel and update the changes in Form. But my program having problem with memory keep going up and after long time, it crashes by throwing some exception.
Can u tell me is there anyway to do this task without memory related issue? Excel file should be open and it has data of 300 rows and 10 columns.
Thanks very much for your time. Expecting for some guidance.
Ramesh
Code is below: -----------------------------------------------
Imports System.Data Imports System.Data.OleDb
Public Class Form1
Public theOleDbCommand As OleDbCommand Public theOleDbDataAdapter As OleDbDataAdapter Public theDataSet As DataSet
Public SelectString As String = "SELECT * FROM `SimpleRange` " + _ "WHERE (AlarmStatus = 1)"
Public ConnectionString As String = " Provider=Microsoft.Jet.OLEDB.4.0; " & _ " Data Source=C:\testminimum.xls; " & _ " Extended Properties=Excel 8.0;"
Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load
Timer1.Interval = 5000 Timer1.Enabled = True
Me.theOleDbCommand = New OleDbCommand(Me.SelectString, New OleDbConnection(Me.ConnectionString)) Me.theOleDbDataAdapter = New OleDbDataAdapter(Me.theOleDbCommand) Me.theDataSet = New DataSet()
Try Me.theOleDbCommand.Connection.Open() Catch ex As Exception End Try
End Sub
Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
FetchExcelData() ' Here data is fetched from Excel continuously using timer1_tick event
End Sub
Public Sub FetchExcelData()
Try Me.theDataSet.Clear() Me.theOleDbDataAdapter.Fill(theDataSet) Me.theDataGridView.DataSource = theDataSet.Tables(0).DefaultView Me.theOleDbDataAdapter.Dispose() Catch ex As Exception End Try
GC.Collect() GC.WaitForPendingFinalizers()
End Sub
End Class ----------------------------------------------
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
I finally realize that my problem was due to some bug in Excel and ADO memory Leak from this article below:
http://support.microsoft.com/kb/319998
Title: BUG: Memory leak occurs when you query an open Excel worksheet by using ActiveX Data Objects (ADO)
Thanks Ramesh
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Using the Microsoft.ACE.OLEDB have the same issue as using the Jet Driver with Mixed Fields?
I was writing a program that was reading a .xls (2003) file using the Jet Driver. The issue I found was if the a field (column) had both Numbers and Text. It looked at the 1st 8 rows by default to determine the field type. If the column had mostly numbers it set the field type as Number and returned NULL for the Text fields.
|
| Sign In·View Thread·PermaLink | 4.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
 |
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\@TypeGuessRows=0
A value of 0 will result in a scan of all the rows, whereas any other number n will result in a scan of the first n rows.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Read Excel using Jet. you may configure update command also.
Dim m_sConn19 As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\temp\umr0001.xls;" & _ "Extended Properties=""Excel 8.0;HDR=YES"""
Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn19) conn1.Open() Dim cmd1 As New System.Data.OleDb.OleDbCommand("Select [f1],[f2] From [New$] where [F2] is not null", conn1)
Dim myAdapter As New OleDbDataAdapter Dim myTable As New DataTable("test") With myAdapter .SelectCommand = cmd1 .Fill(myTable) End With
I started out with nothing and I still have most of it
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I know its a while since the last post but in case anyone comes across this and needs to know the answer, I have just had the same issue but solved it by adding IMEX=1 in to the connection string as follows:
myConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=" + strPath + "; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");
Note the quotes around the extended properties values as without those I got an error.
Also I cannot take the credit for this. I got the answer here: http://forums.asp.net/p/1039391/1443853.aspx - it worked for me so thought I would post a solution.
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
 |
Hi, I tried the above solution but it is not working for me. In my case almost first 200 rows in the excel are numeric & then a combination of text & numeric. Can any body plzzzz help.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi,
if you are using the Microsoft.ACE.OLEDB.12.0 provider, you need to change the following registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel\@TypeGuessRows
Regards
--- Paco ---
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |