Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send ....data set data to the msexcel.....how can i send it.
i have made a connection string . how can i do that. help me out in this .....
Posted

Since you are working with asp.net you should avoid using excel to perform your work.

Use a library like EPPlus[^] to create your excel files. It's an open source project hosted on CodePlex.

Using excel to perform your work tends to work beutifully during development, and cause all sorts of grief during deployment. Is Excel installed on the server? - and if it is - do you have the required privileges to execute it?

Regards
Espen Harlinn
 
Share this answer
 
Comments
JF2015 6-Feb-11 7:58am    
Good link.
Espen Harlinn 6-Feb-11 8:02am    
Thank you JF2015!
Are you looking for something like this[^]?
 
Share this answer
 
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.CboSection.SelectedIndex = -1 Then Exit Sub
Windows.Forms.Cursor.Current = Cursors.WaitCursor
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel File|*.xls"
saveFileDialog1.Title = "Save an Excel File"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
FillExcel(saveFileDialog1.FileName)
End If
Windows.Forms.Cursor.Current = Cursors.Default
End Sub


VB
Private Sub FillExcel(ByVal FileName As String)
        'Extracting from database
        Dim str, filename1, sql1 As String
        Dim col, row As Integer
        Dim j, m As Integer
        Dim cnt As Integer
        Dim ad1 As SqlClient.SqlDataAdapter
        Dim tb2 As System.Data.DataTable
        str = "SELECT * from Table1"
        Dim Tb As System.Data.DataTable

        sql="write sql "
       
      


        tb2 = New DataTable
        tb2 = GetDataset(sql)



        Dim columncount As Integer = tb2.Columns.Count

        Dim Excel As Object = CreateObject("Excel.Application")
        If Excel Is Nothing Then
            MsgBox("It appears that Excel is not installed on this machine. This operation requires MS Excel to be installed on this machine.", MsgBoxStyle.Critical)
            Return
        End If


        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()

                Dim i As Integer = 1
                For col = 0 To tb2.Columns.Count - 1
                    .cells(1, i).value = tb2.Columns(col).ColumnName
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To tb2.Columns.Count - 1
                    i = 2
                    For row = 0 To tb2.Rows.Count - 1
                        Dim OI
                        .Cells(i, k).Value = tb2.Rows(row).ItemArray(col)
                        i += 1
                    Next
                    k += 1
                Next
                'filename = "c:\File_Exported" & Format(Now(), "dd-MM-yyyy_hh-mm-ss") & ".xls"
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MsgBox("Data's are exported to Excel Succesfully in '" & FileName & "'", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub
 
Share this answer
 
Comments
Aksh@169 6-Feb-11 11:36am    
i need in asp.net C# Please help me out!!!...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900