Question : Exporting Sql Tabel to Excel

I am trying to Export a SQl Table to Excel in VS.net 2008,  C# asp.net.

I got info off the internet. But for some reason just can't get the access to the Excel library quite right. (At least I think that is my main error.

I've added the reference for Microsoft Excel 12.0 Object Library in.
I've checked in properties I have "Microsoft.Office.Interlop.Excel in there.

I have tried a using statement as

using Microsoft.Office.Interop.Excel;
and
using Microsoft.Office.Interop;

I still get the following errors..

1) The type or namespace name 'Excel' could not be found (are you missing a using directive or an assembly reference?

2) Ambiguity between method 'Microsoft.Office.Interop.Excel._Worksheet.Activate()' and non-method 'Microsoft.Office.Interop.Excel.DocEvents_Event.Activate'.

3)  The type or namespace name 'XmlException' could not be found (are you missing a using directive or an assembly reference?

Attached is the code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;// needed for database
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel; //Need for Excel connection

/// <summary>
/// Summary description for ExcelOutput
/// </summary>
public class ExcelOutput
{
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["WannalancitConnectionString"];
        //SqlDataReader rdr = null;
        SqlConnection con = null;
        SqlCommand cmd = null;
        // Open connection to the database
        string CommandText = "";

	public ExcelOutput()
	{
	}

    public void CreateExcelOutput()
    {

	try			
    {
        string connectionInfo = conSettings.ConnectionString;
        string ConnectionString = connectionInfo;
        con = new SqlConnection(ConnectionString);

        con.Open();
        cmd = new SqlCommand("ExcelSetup");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Process", "set");
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        con.Close();

        con.Open();
        cmd = new SqlCommand("select * from tmpAdultTrainedExcel");
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet dataset = new DataSet();
        adapter.Fill(dataset);
        Excel.ApplicationClass excel = new ApplicationClass();
        excel.Application.Workbooks.Add(true);
        System.Data.DataTable table = dataset.Tables[0];
        int ColumnIndex=0;
        foreach( System.Data.DataColumn col in table.Columns)
        {
            ColumnIndex++;
            excel.Cells[1,ColumnIndex]=col.ColumnName;
        }
        int rowIndex=0;
        foreach(DataRow row in table.Rows)
        {
            rowIndex++;
            ColumnIndex=0;
            foreach(DataColumn col in table.Columns)
            {
                ColumnIndex++;
                excel.Cells[rowIndex+1,ColumnIndex]=row[col.ColumnName];
            }
        }
        excel.Visible = true;
        Worksheet worksheet = (Worksheet)excel.ActiveSheet;
        worksheet.Activate();
    }
    catch (XmlException exml)
    {
        throw;
    }

    }    
}

Answer : Exporting Sql Tabel to Excel

Problem by problem:

1) and 2)
When you use:
using Microsoft.Office.Interop.Excel

You only need to
ApplicationClass xls = new ApplicationClass();
xls.Application.Workbooks.Add(true);

Also, note that I used xls, instead of excel, as it may give you some reference error (although it should be case sensitive)...

3)
add the following line to avoid error for the XmlException:
using System.Xml;
Random Solutions  
 
programming4us programming4us