I have a datatable being created with various inputs. Sometimes the resulting table is 35000+ rows. Currently, the datatable gets displayed onto a gridview. It loads fine after a couple minutes. Then, theres an option to export the gridview to an excel file. Everytime we have a large table to export, the conversion fails.
My goal is to bypass the gridview step and take the formatted table and put it directly into an excel file. Could also be a csv file if thats faster to write/load, as long as the data table is similar to the gridview output.
I tried the following code here Export DataTable to Excel File. I did my best to convert it to vb, here...
Protected Sub btnExportData_Click(sender As Object, e As EventArgs) Handles btnExportData.Click
Dim dt As DataTable
dt = CreateDataSource()
Dim filename As String = "attachment; filename=DistComplain.xls"
Response.ClearContent()
Response.AddHeader("content-disposition", filename)
Response.ContentType = "application/vnd.ms-excel"
Dim tab As String = ""
For Each dc As DataColumn In dt.Columns
Response.Write((tab + dc.ColumnName))
tab = "" & vbTab
Next
Response.Write("" & vbLf)
Dim i As Integer
For Each dr As DataRow In dt.Rows
tab = ""
i = 0
Do While (i < dt.Columns.Count)
Response.Write((tab + dr(i).ToString))
tab = "" & vbTab
i = (i + 1)
Loop
Response.Write("" & vbLf)
Next
Response.End()
End Sub
CreateDataSource() is the table that gets created in memory. Then theres other buttons that call it to fill the gridview. Right now it successfully complies and runs, and then it successfully creates the file. Although, when the file tries to open I get this error...
This happens when I try both xls and csv files. Something is not getting translated right. Any solutions?
(Written with help from Google) Create an export using the StringWriter class:
Public Shared Sub ExportDataSetToExcel(ds As DataSet, filename As String)
Dim response As HttpResponse = HttpContext.Current.Response
'Clean response object
response.Clear()
response.Charset = ""
'Set response header
response.ContentType = "application/vnd.ms-excel"
response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")
'Create StringWriter and use to create CSV
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
'Instantiate DataGrid
Dim dg As New DataGrid()
dg.DataSource = ds.Tables(0)
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub
You just need to pass the function the DataSet and the File Name. If you do not want to edit your CreateDataSource() function, you can merge it into a DataSet first like so:
Dim dt As DataTable = CreateDataSource()
Dim ds As New DataSet
ds.Merge(dt)
Your question is about why you're getting the message about being unable to open the file, correct?
According to Microsoft, this occurs when you have the "Ignore other applications that use Dynamic Data Exchange (DDE)" setting turned on. (See here). The link includes instructions to change the setting.
Related
The following code is my current attempt at opening some data in excel from a website button in VB.Net. I would like the data to show up barebones, but the formatting from the table on the website always follows. The paging and colors make the data near impossible to read and can only see the first page of data. Any quick fixes? I've tried a lot of things I've found on here but to no avail.
Private Sub DownloadExcel()
Response.Clear()
'Dim dt As DataTable = TryCast(ViewState("GridData"), DataTable)
Grid_Bad_Meters.AllowPaging = False
Grid_Bad_Meters.AllowSorting = False
'Grid_Bad_Meters.DataSource = dt
'Grid_Bad_Meters.DataBind()
Dim sfile As String = "Communication_Failures" & Now.Ticks
Response.AddHeader("content-disposition", "attachment;filename=" & sfile & ".xls")
Response.Charset = ""
' If you want the option to open the Excel file without saving then
' comment out the line below
' Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
Grid_Bad_Meters.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
'Grid_Bad_Meters.AllowPaging = True
'Grid_Bad_Meters.AllowSorting = True
'GridView1.DataSource = dt
'GridView1.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim ScriptManager As ScriptManager = ScriptManager.GetCurrent(Me.Page)
ScriptManager.RegisterPostBackControl(Me.btnExportToExcel)
Catch ex As Exception
End Try
End Sub
Protected Sub btnExportToExcel_Click(sender As Object, e As EventArgs) Handles btnExportToExcel.Click
Try
Dim sw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(sw)
Dim style As String = "<style>.textmode{mso-number-format:\#;}</style>"
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=SignExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
For i As Integer = 0 To Me.Grid_Bad_Meters.Rows.Count - 1
Dim row As GridViewRow = Grid_Bad_Meters.Rows(i)
row.Attributes.Add("class", "textmode")
Next
'lblRptHeader.RenderControl(hw)
hw.WriteBreak()
'lblReportDateRange.RenderControl(hw)
Grid_Bad_Meters.RenderControl(hw)
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
Catch ex As Exception
End Try
End Sub
You could use one of the two approaches mentioned below. Of course, there are other ways of meeting your requirement like exporting to csv file as mentioned in a comment or using a .Net library meant for Excel exporting like epplus.
OpenXML Approach
If you are looking for a way to export to Excel without using the html approach, then you can use OpenXML approach that is explained very clearly with a working example at this URL: Export to Excel using OpenXML. This will eliminate all the CSS styles that can get associated with exporting using html approach and you seem to be using this html approach according to the code in your original post. However, if you want to use the html approach, then the code below should work and eliminate all CSS styles that can come in the way when viewing the excel file. I have actually tried this posted code on my machine before putting it here.
Html Approach
You can create a new instance of GridView in your export method rather than use an existing instance, and data bind it to same data as the existing gridview on your page before rendering it to excel. Before you data bind it in the export method you need to make sure that no styles are set and specifically the grid line are set to none as in code below.
You can see an actual video of how this works at this URL : Grid Export without any CSS Styles. This was how the code behaved on my laptop when I ran it.
You can use sample code below, but make sure the data source is set to data that includes all records across all pages of original gridview. I have used SqlDataSource1 as data source but you can replace it by an appropriate method in your situation.
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
Dim GridView2 As New GridView()
GridView2.AllowPaging = False
GridView2.AllowSorting = False
GridView2.Style.Clear()
GridView2.CellPadding = 0
GridView2.CellSpacing = 0
GridView2.GridLines = GridLines.None
GridView2.BorderStyle = BorderStyle.None
GridView2.BorderWidth = Unit.Pixel(0)
GridView2.AlternatingRowStyle.BorderStyle = BorderStyle.None
GridView2.DataSource = SqlDataSource1
GridView2.DataBind()
' Clear the response
Response.Clear()
' Set the type and filename
Response.AddHeader("content-disposition", "attachment;filename=griddata.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
' Add the HTML from the GridView to a StringWriter so we can write it out later
Dim sw As New System.IO.StringWriter()
Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
GridView2.RenderControl(hw)
' Write out the data
Response.Write(sw.ToString())
Response.[End]()
End Sub
Public Overrides Property EnableEventValidation() As Boolean
Get
Return False
End Get
'Do nothing
Set
End Set
End Property
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
'Allows for printing
End Sub
Ok, this is an interesting issue. I have been tasked with modifying an existing VB project. Currently the user selected from a series of dropdowns to select a sql query and then run that query. So the user selects and environment dropdown, the results of that dropdown populates the category dropdown. Once the category is selected, they get a dropdown of available queries. Once they select a query and hit the "Run" button, they get a gridview with the results of the query. Some of the query results are huge. The query I'm running as a test has 40 columns and 20,000 records. The query runs in less than 5 seconds but it takes over a minute to render the gridview. Once the gridview is done rendering, the user has the option to export the results to Excel. And by this, I mean the code opens an instance of Excel through gridview.RenderControl and displays the results in Excel. The user doesn't want to save the excel file and then navigate to the file, they want it to open right from the webform they are using which is what the code does currently.
However, the user doesn't care about the gridview. They don't care if they see it at all. They want to just open Excel. So instead of using gridview.RenderControl, I want to open Excel and populate it with the DataTable (or DataSet) in memory. Any thoughts on the best way to do that?
Here's how they are currently populating the gridview:
Dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim MyDataTable As DataTable
Dim MyReader As SqlDataReader
MyConnection = New SqlConnection()
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings(Connection).ConnectionString
MyCommand = New SqlCommand()
MyCommand.CommandText = Sqlquery
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
MyCommand.Connection.Open()
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
MyDataTable = New DataTable()
MyDataTable.Load(MyReader)
If (MyDataTable.Rows.Count > 0) Then
QueryresultPanel.Visible = True
gvLineItems.DataSource = MyDataTable
gvLineItems.DataBind()
End If
MyDataTable.Dispose()
MyCommand.Dispose()
MyConnection.Dispose()
Here's how they're opening and populating the instance of Excel:
Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
Response.Clear()
Response.Buffer = True
'
' Set the content type to Excel.
'
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
'
' Turn off the view state.
'
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
'
' Get the HTML for the control.
'
gvLineItems.RenderControl(oHtmlTextWriter)
'
' Write the HTML back to the browser.
'
Response.Write(oStringWriter.ToString())
Response.[End]()
End Sub
Obviously, there's no RenderControl for a DataTable or DataSet and can't figure out how to get this record set to render in an instance of Excel without saving it to a file first.
Alright, here's the solution I found (in case anyone is interested). It's pretty simple actually. I just looped through the datatable and used StringWriter.
Protected Sub WriteToExcelFile(dt As DataTable)
Dim sw As StringWriter
For Each datacol As DataColumn In dt.Columns
sw.Write(datacol.ColumnName + vbTab)
Next
Dim row As DataRow
For Each row In dt.Rows
sw.Write(vbNewLine)
Dim column As DataColumn
For Each column In dt.Columns
If Not row(column.ColumnName) Is Nothing Then
sw.Write(row(column).ToString() + vbTab)
Else
sw.Write(String.Empty + vbTab)
End If
Next column
Next row
Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=DataTable.xls")
Response.Output.Write(sw.ToString())
Response.Flush()
System.Web.HttpContext.Current.Response.Flush()
System.Web.HttpContext.Current.Response.SuppressContent = True
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()
End Sub
I Have export Gridview data to excel, here's my code :
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
Try
Dim dt As New DataTable
If CReversal.SearchReversal2(txtAccount.Text, txtCustName.Text, txtAmount.Text, dropResponse.SelectedValue.ToString, txtRefNo.Text, txtDate.Text) Then
dt = CReversal.DT
Else
eMessage("System failure: ", CReversal.eMsg)
End If
Dim DataGrd As New DataGrid()
DataGrd.DataSource = dt.DefaultView
DataGrd.DataBind()
Dim attachment As String
attachment = "attachment; filename=Inquiry_Report" & Format(Now, "ddMMMyyyy") & ".xls"
Response.Buffer = True
Response.ClearContent()
Response.ClearHeaders()
Response.AddHeader("content-disposition", attachment)
Response.ContentType = "application/ms-excel"
Dim sw As New StringWriter()
Dim htw As New HtmlTextWriter(sw)
DataGrd.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()
Catch ex As Exception
eMessage("Export Data failure: ", ex.ToString())
End Try
End Sub
the export process is run as i want. but when i open the excel file, i got some strings number character become like this :
but actually the datagridvie display is like this :
is it possible to format the string number character when we want to export to excel programatically? thk you
The problem with your approach is, that you are not writing "real" excel data. It is a html table which CAN be read by excel, but you are not really able to influence the way excel reads this mess. Libraries like EPPLus are much better at writing excel file and reduce your headaches by orders of magnitude. It is free, even in commercial contexts, so no problems when you use it. Just better results
As a side note: Epplus supports a "LoadFromDataTable" method- with a little luck, that all you need...
Hi I'm using the following code to generate a CSV file containing data from a dataset. The CSV generates fine and a prompt box displays so that the user can either open or save the csv file. What I'm wondering is whether it's possible to save the CSV file?
Thanks for any help
Edited with solution (NB I don't need the prompt box to open as I'll be writing additional code to generate and save a number of files and then display links to them). This saves the dataset as a CSV on our server.
Protected Sub exportBtn_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ds1 As DataSet
ds1 = csvdownload.ReturnNewPledges()
Dim csv As New chartnew.csvhelper
Dim strData1 As String = csv.Export(ds1, True)
Dim todaydate As Date = Date.Now.Date
Dim todaystr As String = todaydate.ToString("ddMMyyyy")
Dim filename As String = todaystr & "_filename.csv"
Dim saveDir As String = "writedir\files\"
Dim appPath As String = Request.PhysicalApplicationPath
Dim filePath As String = appPath + saveDir + filename
Dim writer As New StreamWriter(filePath)
writer.Write(strData1)
writer.Close()
End Sub
Use a StreamWriter:
Dim SwFromFile As StreamWriter = New StreamWriter(fileName)
SwFromFile.Write(strData1)
SwFromFile.Flush()
SwFromFile.Close()
I want to render a report directly to pdf. I have an objectdatasource with 2 parameters. I obtain these parameters from a hiddenfield on the webform and from the datakeyname on a gridview. The report works when I load it in report viewer without rendering to pdf. When I place the code to render the report as pdf the parameters dont load i.e. the report renders as pdf but there are no details on the report. My code is below, any help appreciated. I placed the code in gridview_selectedindexchanged:
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim ds1 As New seminarsTableAdapters.Sem1TableAdapter
Dim rdssem As New ReportDataSource("seminars.sem1TableAdapter", ds1.GetData(aid:=HiddenField1.Value, semid:=GridView1.SelectedDataKey.Value))
Dim reportsem As New LocalReport
reportsem.ReportPath = "Report1.rdlc"
Dim p1 As New ReportParameter("aid", HiddenField1.Value)
Dim p2 As New ReportParameter("semid", GridView1.SelectedDataKey.Value().ToString)
reportsem.SetParameters(New ReportParameter() {p1, p2})
reportsem.DataSources.Add(rdssem)
Me.ReportViewer1.LocalReport.Refresh()
Dim warnings As Warning() = Nothing
Dim streamids As String() = Nothing
Dim mimeType As String = Nothing
Dim encoding As String = Nothing
Dim extension As String = Nothing
Dim bytes As Byte()
bytes = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
Response.Clear()
Response.ContentType = mimeType
Response.AddHeader("content-disposition", "attachment; filename=foo." + extension)
Response.BinaryWrite(bytes)
Response.End()
End Sub
I overlooked the following line of code:
Dim rdssem As New ReportDataSource("seminars.sem1TableAdapter", ds1.GetData(aid......
it shoud read:
Dim rdssem As New ReportDataSource("seminars.sem1", ds1.GetData(aid.....
Working fine now!
Thanks!