Note that there are some explanatory texts on larger screens.

plurals
  1. POExporting data to excel file C#.NET
    primarykey
    data
    text
    <p>I want to export the data from a access database to excel file. But i got Exception from HRESULT: 0x800A03EC error.</p> <p>Here is a bit of code that i have written,</p> <pre><code>using System; using System.Data; using System.Data.OleDb; using System.Configuration; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; private void showBtn_Click(object sender, EventArgs e) { int cnt = -1; Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\AyumiDB1.mdb"); thisConnection.Open(); OleDbCommand thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText = "SELECT CodeNumber, Particular, LF, DebitCredit, Amount FROM JournalVoucher"; OleDbDataReader thisReader = thisCommand.ExecuteReader(); while (thisReader.Read()) { cnt++; MessageBox.Show(thisReader["CodeNumber"].GetType().ToString()); xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString(); xlWorkSheet.Cells[cnt, 2] = thisReader["Particular"].ToString(); xlWorkSheet.Cells[cnt, 3] = thisReader["LF"].ToString(); xlWorkSheet.Cells[cnt, 4] = thisReader["DebitCredit"].ToString(); xlWorkSheet.Cells[cnt, 5] = thisReader["Amount"].ToString(); } thisReader.Close(); thisConnection.Close(); xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } </code></pre> <p>My problem is the</p> <pre><code>MessageBox.Show(thisReader["CodeNumber"].GetType().ToString()); </code></pre> <p>runs perfectly, but when i tried to insert the same value to the cell of excel with</p> <pre><code>xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString(); </code></pre> <p>then the exception is thrown.</p> <p>Am i doing something wrong??</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload