?
Solved

VB.Net DataSet to Excel

Posted on 2009-03-31
3
Medium Priority
?
1,379 Views
Last Modified: 2012-05-06
I am using the following VB to export a DataSet to excel. It works great in IE, but in Firefox 3.07 I get the following Microsoft Office Excel dialog:

The file you are trying to open, 'ExcelReport.xls', is in a different format that specified by the extension.  Verify that the file is not corrupted and is from a trusted source before opening the file.  Do you want to open the file now?

When I select Yes, it opens fine.

Any ideas?


'Class to convert a dataset to an html stream which can be used to display the dataset
'in MS Excel
'The Convert method is overloaded three times as follows
' 1) Default to first table in dataset
' 2) Pass an index to tell us which table in the dataset to use
' 3) Pass a table name to tell us which table in the dataset to use
Imports System.Data
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
 
Public Class DataSetToExcel
 
    Public Shared Sub Convert(ByVal ds As DataSet, ByVal response As HttpResponse)
        'first let's clean up the response.object
        response.Clear()
        response.AddHeader("content-disposition", "attachment;filename=ExcelReport.xls")
        response.Charset = ""
        'set the response mime type for excel
        response.ContentType = "application/vnd.xls"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New System.Web.UI.WebControls.DataGrid
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(0)
        'bind the datagrid
        dg.DataBind()
        dg.HeaderStyle.Font.Bold = True
        dg.HeaderStyle.Font.Size() = System.Web.UI.WebControls.FontUnit.Point(8)
        dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
        dg.ItemStyle.Font.Size() = System.Web.UI.WebControls.FontUnit.Point(8)
        dg.ItemStyle.HorizontalAlign = HorizontalAlign.Center
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        response.Write(stringWrite.ToString)
        response.End()
    End Sub
 
    Public Shared Sub Convert(ByVal ds As DataSet, ByVal TableIndex As Integer, ByVal response As HttpResponse)
        'lets make sure a table actually exists at the passed in value
        'if it is not call the base method
        If TableIndex > ds.Tables.Count - 1 Then
            Convert(ds, response)
        End If
        'we've got a good table so
        'let's clean up the response.object
        response.Clear()
        response.AddHeader("content-disposition", "attachment;filename=ExcelReport.xls")
        response.Charset = ""
        'set the response mime type for excel
        response.ContentType = "application/vnd.xls"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New System.Web.UI.WebControls.DataGrid
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(TableIndex)
        'bind the datagrid
        dg.DataBind()
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        response.Write(stringWrite.ToString)
        response.End()
    End Sub
 
    Public Shared Sub Convert(ByVal ds As DataSet, ByVal TableName As String, ByVal response As HttpResponse)
        'let's make sure the table name exists
        'if it does not then call the default method
        If ds.Tables(TableName) Is Nothing Then
            Convert(ds, response)
        End If
        'we've got a good table so
        'let's clean up the response.object
        response.Clear()
        response.AddHeader("content-disposition", "attachment;filename=ExcelReport.xls")
        response.Charset = ""
        'set the response mime type for excel
        response.ContentType = "application/vnd.xls"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New System.Web.UI.WebControls.DataGrid
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(TableName)
        'bind the datagrid
        dg.DataBind()
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        response.Write(stringWrite.ToString)
        response.End()
    End Sub
 
    Public Shared Sub Convert(ByVal ds As DataSet, ByVal response As HttpResponse, ByVal strdestinationpath As String)
        'first let's clean up the response.object
        response.Clear()
        response.AddHeader("content-disposition", "attachment;filename=ExcelReport.xls")
        response.Charset = ""
        'set the response mime type for excel
        response.ContentType = "application/vnd.xls"
        'create a string writer
        Dim stringWrite As New System.IO.StringWriter
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New System.Web.UI.WebControls.DataGrid
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(0)
        'bind the datagrid
        dg.DataBind()
        dg.HeaderStyle.Font.Bold = True
        dg.HeaderStyle.Font.Size() = System.Web.UI.WebControls.FontUnit.Point(8)
        dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
        dg.ItemStyle.Font.Size() = System.Web.UI.WebControls.FontUnit.Point(8)
        dg.ItemStyle.HorizontalAlign = HorizontalAlign.Center
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(strdestinationpath)
        sw.Write(stringWrite.ToString)
        sw.Close()
 
    End Sub
End Class

Open in new window

0
Comment
Question by:Paulconsulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
vavjeeva earned 2000 total points
ID: 24031669
you would have to change the settings client's registry in order to skip this warning.. check this link for more details..

http://blogs.msdn.com/vsofficedeveloper/pages/Excel-2007-Extension-Warning.aspx
0
 
LVL 9

Expert Comment

by:vavjeeva
ID: 24031682
Key: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
Value: (DWORD)"ExtensionHardening" = [0 = Disable check; 1 = Enable check and prompt; 2 = Enable check, no prompt deny open]
Default setting if value not present is 1 (enable and prompt)
0
 

Author Comment

by:Paulconsulting
ID: 24031782
Ah jeez.....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question