Hi,

I am working on exporting data from a dataset to excel.  When I used save
file dialog to save the file in a desired path I came across the error
"Exception from HRESULT: 0x800A03EC - COM EXCEPTION UNHANDLED"

Please find below the code I have used

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace EXPORT_DATASET_EXCEL_II
{
 static class ExportingCellByCellMethod
 {

        public static void ExportToExcel(DataSet dataSet, string outputPath)
       // public static void ExportToExcel(DataSet dataSet)
        {

            // Create the Excel Application object
            ApplicationClass excelApp = new ApplicationClass();
            //excelApp.Interactive = false;
            // Create a new Excel Workbook
            Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);

            int sheetIndex = 0;

            // Copy each DataTable as a new Sheet
            foreach (System.Data.DataTable dt in dataSet.Tables)
            {

                // Create a new Sheet
                Worksheet excelSheet = (Worksheet)excelWorkbook.Sheets.Add(
                    excelWorkbook.Sheets.get_Item(++sheetIndex),
                    Type.Missing, 1, XlSheetType.xlWorksheet);

                excelSheet.Name = dt.TableName;


                // Copy the column names (cell-by-cell)
                for (int col = 0; col < dt.Columns.Count; col++)
                {
                    excelSheet.Cells[1, col + 1] =
dt.Columns[col].ColumnName;
                }



                    // Copy the values (cell-by-cell)
                    for (int col = 0; col < dt.Columns.Count; col++)
                    {
                        for (int row = 0; row < dt.Rows.Count; row++)
                        {
                            excelSheet.Cells[row + 2, col + 1] =
dt.Rows[row].ItemArray[col];
                            ((Range)excelSheet.Rows[1,
Type.Missing]).AutoFit();
                        }

                        ((Range)excelSheet.Columns[1,
Type.Missing]).AutoFit();
                        ((Range)excelSheet.Columns[2,
Type.Missing]).AutoFit();
                        ((Range)excelSheet.Columns[3,
Type.Missing]).AutoFit();

                    }

                }


                // Save and Close the Workbook

                excelWorkbook.SaveAs(outputPath,
XlFileFormat.xlWorkbookNormal, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing,
XlSaveAsAccessMode.xlExclusive,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
                excelWorkbook.Close(true, Type.Missing, Type.Missing);


                excelWorkbook = null;

                // Release the Application object
                excelApp.Quit();
                excelApp = null;

                // Collect the unreferenced objects
                GC.Collect();
                GC.WaitForPendingFinalizers();

            }

        }

 }


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace EXPORT_DATASET_EXCEL_II
{
    public partial class Form1 : Form
    {
        System.Data.OleDb.OleDbConnection conn;
        OleDbDataAdapter DA;
        string S;
        DataSet source;

        public Form1()
        {
            InitializeComponent();
            conn = new System.Data.OleDb.OleDbConnection();
            conn.ConnectionString =
ConfigurationManager.ConnectionStrings["MSAccess"].ToString();
        }

private DataSet getDemoDataSet()
        {
            S = "Select Emp_ID as [ID], Emp_Name as NAME,Reason as REASON,
Submitted_Date as [SUBMITTED DATE] from LEAVEMASTER_TABLE";
            source = new DataSet();
            DA = new OleDbDataAdapter(S, conn);
            DA.Fill(source);
            source.Tables[0].TableName = "sample";
            return source;
        }

private void button2_Click(object sender, EventArgs e)
        {
            DataSet demoDataSet = this.getDemoDataSet();
            string savepath;
            saveFileDialog1 = new SaveFileDialog();
            savepath = saveFileDialog1.FileName + ".xls";
            saveFileDialog1.Filter = "xls files (*.xls)|*.xls";

            if (saveFileDialog1.ShowDialog() ==
System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                ExportingCellByCellMethod.ExportToExcel(demoDataSet,
savepath);

            }


            MessageBox.Show("Data Exported
successfully");

        }
    }
}


Can anyone help me out in solving this? Replies are highly appreciated. TIA.
-- 
Regards,
Karthik

Reply via email to