I am using the Microsoft.ACE.OLEDB.12.0 driver in my C # to read and write to excel (XLS) files. Advanced properties for my reader look like this: Excel 8.0; HDR = NO; IMEX = 1; and for a screenwriter it looks like this: Excel 8.0; HDR = NO; IMEX = 0;
This is the scenario: I read from the excel file, say input.xls and create a new output.xls file and write it using my author. Now I open the output.xls file in MS Excel and add a few more lines to it.
Then I pass my output.xls as input to my program, and when I debug it, I see that it only reads lines originally written using OleDb. It does not read any of the new lines that I have added, and the writer spills out the lines that have been read.
How does OleDb work? that is, treat the database as locked by it and not evaluate external inserts. Or may there be a problem with the way I create and save files?
private void Initialize(string fileName, FileType fileType) { string connectionString = GetConnectionString(fileName, fileType); string sheet; using (OleDbConnection connection = OpenConnection(connectionString)) { DataTable sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); } tableName = "[ListingDetails]"; conn = new OleDbConnection(); conn.ConnectionString = connectionString; conn.Open(); cmd1 = new OleDbCommand(); cmd1.Connection = conn; cmd1.CommandText = string.Format(CultureInfo.InvariantCulture, @"CREATE TABLE {0} {1}", tableName, fieldstring); int x = cmd1.ExecuteNonQuery(); } public void InsertRow(string[] data) { StringBuilder fieldString = new StringBuilder(); fieldString.Append("("); foreach (var h in headers) { fieldString.Append(" ["+h+"], "); } fieldString.Remove(fieldString.Length - 2, 2); fieldString.Append(")"); StringBuilder dataString = new StringBuilder(); dataString.Append("('"); foreach (var d in data) { if(d!=null) dataString.Append(d.Replace("'", "''") + "', '"); else dataString.Append("', '"); } dataString.Remove(dataString.Length - 4, 4); dataString.Append("')"); cmd1.CommandText = string.Format(CultureInfo.InvariantCulture, @"INSERT INTO {0} {1} values {2}", tableName, fieldString, dataString); int x = cmd1.ExecuteNonQuery(); }
To close the file, I just do conn.Close();
I somehow suspect how I create / use a sheet here in the Initialize () method.
PS I saw a similar question, but the problem there seemed to be due to the fact that the Data and IMEX flag is not set to 1. Let me tell you in advance that this is not a duplicate question.
thanks