Exporting data to excel file C#.NET

狂风中的少年 提交于 2019-12-12 20:27:15

问题


I want to export the data from a access database to excel file. But i got Exception from HRESULT: 0x800A03EC error.

Here is a bit of code that i have written,

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();
        }
    }

My problem is the

MessageBox.Show(thisReader["CodeNumber"].GetType().ToString()); 

runs perfectly, but when i tried to insert the same value to the cell of excel with

xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString();

then the exception is thrown.

Am i doing something wrong??

Thanks


回答1:


xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString();

In this line avoid the exception you change

int cnt = 0;



回答2:


Instead of

xlWorkSheet.Cells[cnt, 1] = thisReader["CodeNumber"].ToString(); 

try this

((Excel.Range)xlWorkSheet.Cells[cnt, 1]).Value2= thisReader["CodeNumber"].ToString(); 

Might work



来源:https://stackoverflow.com/questions/4772272/exporting-data-to-excel-file-c-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!