<p>Does anyone know how to write to an excel file (.xls) via OLEDB in C#?  I'm doing the following:</p>

<pre><code>   OleDbCommand dbCmd = new OleDbCommand("CREATE TABLE [test$] (...)", connection);
   dbCmd.CommandTimeout = mTimeout;
   results = dbCmd.ExecuteNonQuery();
</code></pre>

<p>But I get an OleDbException thrown with message:</p>

<blockquote>
  <p>"Cannot modify the design of table
  'test$'.  It is in a read-only
  database."</p>
</blockquote>

<p>My connection seems fine and I can select data fine but I can't seem to insert data into the excel file, does anyone know how I get read/write access to the excel file via OLEDB?</p>

[{'answer_id': 58162, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 1, 'selected': False, 'text': '<p>A couple questions: </p>\n\n<ul>\n<li>Does the user that executes your app (you?) have permission to write to the file? </li>\n<li>Is the file read-only?</li>\n<li>What is your connection string?</li>\n</ul>\n\n<p>If you\'re using ASP, you\'ll need to add the IUSER_* user as in <a href="http://support.microsoft.com/kb/195951/" rel="nofollow noreferrer">this example</a>.</p>\n'}, {'answer_id': 59411, 'author': 'Danielb', 'author_id': 39040, 'author_profile': 'https://Stackoverflow.com/users/39040', 'pm_score': 0, 'selected': False, 'text': '<ul>\n<li>How do I check the permissions for writing to an excel file for my application (I\'m using excel 2007)?</li>\n<li>The file is not read only, or protected (to my knowledge).</li>\n<li>My connection String is:        </li>\n</ul>\n\n<blockquote>\n  <p>"Provider=Microsoft.Jet.OLEDB.4.0;Data\n  Source=fifa_ng_db.xls;Mode=ReadWrite;Extended\n  Properties=\\"Excel\n  8.0;HDR=Yes;IMEX=1\\""</p>\n</blockquote>\n'}, {'answer_id': 184213, 'author': 'Zorantula', 'author_id': 18108, 'author_profile': 'https://Stackoverflow.com/users/18108', 'pm_score': 4, 'selected': True, 'text': '<p>You need to add <code>ReadOnly=False;</code> to your connection string</p>\n\n<pre><code>Provider=Microsoft.Jet.OLEDB.4.0;Data Source=fifa_ng_db.xls;Mode=ReadWrite;ReadOnly=false;Extended Properties=\\"Excel 8.0;HDR=Yes;IMEX=1\\";\n</code></pre>\n'}, {'answer_id': 184271, 'author': 'Duncan Smart', 'author_id': 1278, 'author_profile': 'https://Stackoverflow.com/users/1278', 'pm_score': 0, 'selected': False, 'text': '<p>Further to Michael Haren\'s answer. The account you will need to grant Modify permissions to the XLS file will likely be NETWORK SERVICE if this code is running in an ASP.NET application (it\'s specified in the IIS Application Pool). To find out exactly what account your code is running as, you can do a simple: </p>\n\n<pre><code>Response.Write(Environment.UserDomainName + "\\\\" + Environment.UserName);\n</code></pre>\n'}, {'answer_id': 781193, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>I was also looking for and answer but Zorantula\'s solution didn\'t work for me.\nI found the solution on <a href="http://www.cnblogs.com/zwwon/archive/2009/01/09/1372262.html" rel="noreferrer">http://www.cnblogs.com/zwwon/archive/2009/01/09/1372262.html</a></p>\n\n<p>I removed the <code>ReadOnly=false</code> parameter and the <code>IMEX=1</code> extended property.</p>\n\n<p>The <code>IMEX=1</code> property opens the workbook in import mode, so structure-modifying commands (like <code>CREATE TABLE</code> or <code>DROP TABLE</code>) don\'t work.</p>\n\n<p>My working connection string is:</p>\n\n<pre><code>"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=workbook.xls;Mode=ReadWrite;Extended Properties=\\"Excel 8.0;HDR=Yes;\\";"\n</code></pre>\n'}, {'answer_id': 976623, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I was running under ASP.NET, and encountered both "Cannot modify the design..." and "Cannot locate ISAM..." error messages.</p>\n\n<p>I found that I needed to:</p>\n\n<p><strong>a</strong>) Use the following connection string:</p>\n\n<p><code>Provider=Microsoft.Jet.OLEDB.4.0;Mode=ReadWrite;Extended Properties=\'Excel 8.0;HDR=Yes;\';Data Source=" + {path to file};</code></p>\n\n<p>Note I too had issues with <code>IMEX=1</code> and with the <code>ReadOnly=false</code> attributes in the connection string.</p>\n\n<p><strong>b</strong>) Grant EVERYONE full permissions to the folder in which the file was being written.  Normally, ASP.NET runs under the NETWORK SERVICE account, and that already had permissions. However, the OleDb code is unmanaged, so it must run under some other security context.  (I am currently too lazy to figure out which account, so I just used EVERYONE.)</p>\n'}, {'answer_id': 6947980, 'author': 'Erwin Aarnoudse', 'author_id': 879400, 'author_profile': 'https://Stackoverflow.com/users/879400', 'pm_score': 2, 'selected': False, 'text': '<p>I also had the same problem. Only remove the extended property <code>IMEX=1</code>. That will solve your problem. Your table will be created in your Excel file...</p>\n'}]
['https://Stackoverflow.com/questions/57987', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/39040/']