T
thejamie
Coming from a VB platform, I am trying to use code to populate an Excel
spreadsheet with a late bound Excel object: (It hangs on the Interaction
object which is found in VB but obviously not in ASP.NET). It requires a
valid SQL string be passed in. How can I make this work in ASP.NET? I
placed both VB and C# code below:
string strToExcel = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data
Source=MyServer";
object objExcel;
try
{
objExcel = Interaction.CreateObject("Excel.Application");
//ExcelApplication
objExcel.Workbooks.Add();
objExcel.Cells.Activate();
objExcel.ActiveSheet.QueryTables.Add("OLEDB;" + strToExcel,
objExcel.Cells(1, 1), sql);
objExcel.ActiveSheet.QueryTables(1).Refresh();
objExcel.Range("A1").Select();
objExcel.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unable to export the dataset to
Excel");
}
-- ----
VB
Imports System.Data.SqlClient
Public Class Form1
Private Sub ExportToExcel2(ByVal sql As String)
Dim strToExcel As String = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial Catalog=ZPHCore;Data
Source=HCUSGR699\int"
Dim objExcel As Object
Try
objExcel = CreateObject("Excel.Application") 'ExcelApplication
objExcel.Workbooks.Add()
objExcel.Cells.Activate()
objExcel.ActiveSheet.QueryTables.Add("OLEDB;" & strToExcel,
objExcel.Cells(1, 1), sql)
objExcel.ActiveSheet.QueryTables(1).Refresh()
objExcel.Range("A1").Select()
objExcel.Visible = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Unable to export the dataset to
Excel")
End Try
End Sub
spreadsheet with a late bound Excel object: (It hangs on the Interaction
object which is found in VB but obviously not in ASP.NET). It requires a
valid SQL string be passed in. How can I make this work in ASP.NET? I
placed both VB and C# code below:
string strToExcel = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial Catalog=MyDB;Data
Source=MyServer";
object objExcel;
try
{
objExcel = Interaction.CreateObject("Excel.Application");
//ExcelApplication
objExcel.Workbooks.Add();
objExcel.Cells.Activate();
objExcel.ActiveSheet.QueryTables.Add("OLEDB;" + strToExcel,
objExcel.Cells(1, 1), sql);
objExcel.ActiveSheet.QueryTables(1).Refresh();
objExcel.Range("A1").Select();
objExcel.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unable to export the dataset to
Excel");
}
-- ----
VB
Imports System.Data.SqlClient
Public Class Form1
Private Sub ExportToExcel2(ByVal sql As String)
Dim strToExcel As String = "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial Catalog=ZPHCore;Data
Source=HCUSGR699\int"
Dim objExcel As Object
Try
objExcel = CreateObject("Excel.Application") 'ExcelApplication
objExcel.Workbooks.Add()
objExcel.Cells.Activate()
objExcel.ActiveSheet.QueryTables.Add("OLEDB;" & strToExcel,
objExcel.Cells(1, 1), sql)
objExcel.ActiveSheet.QueryTables(1).Refresh()
objExcel.Range("A1").Select()
objExcel.Visible = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Unable to export the dataset to
Excel")
End Try
End Sub