|
-
Jan 3rd, 2003, 01:17 PM
#1
Thread Starter
Member
Output to Excel
Using this code:
Response.ContentType = "application/ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=worksheet.xls")
Response.Flush()
...I'm getting an "unable to read file" error when I try to open the file in Excel. It seems to be saving it, but I'm not convinced in a readable format. Does anyone have any suggestions regarding this or other methods of sending an aspx page to Excel from the web form?
Thanks...Ooogs
-
Jan 3rd, 2003, 03:29 PM
#2
Lively Member
Hello,
This can be a problem, specially if you want it to in a preformatted readable format. I had the same problem, and it would mess up the data in certain columns and do all types of crazy stuff.
What I had to do.
If you are using SQL Server, You can build DTS Package that runs daily or when ever you want it to, and have it build a the excel files.
Then just have a button on your web page that will open up the already created file that is located on your server somewhere.
If you do not have SQL Server, not sure what to tell you.
Give me more details about your problems and I will try to help you further, with greater details.
-
Jan 3rd, 2003, 03:35 PM
#3
Thread Starter
Member
Output to Excel
Basically, I'm using a datagrid on a nice colorful page to display the results of a query. I have a link on that page to a "Printer-friendly version", which is another aspx page with the same datagrid re-done in black and white (not "fancy"). That part works just great.
Unfort, my boss wants me to add an option for output to Excel so our employees can sort and format away before they print. The code I used (and included before), worked awesome a couple times, then stopped....???
Any ideas?
Thanks....Ooogs
-
Jan 3rd, 2003, 03:51 PM
#4
Lively Member
I looked at some code that works of mine, and the only thing different that I see is that I have:
Response.clear
Then what you had.
Kenny
-
Jan 3rd, 2003, 03:58 PM
#5
Thread Starter
Member
Kenny - humor me and post your code if you don't mind. I'd like to see if the problem is systematic or programatic. I'm still having no luck.
Thanks...Ooogs
-
Jan 3rd, 2003, 04:03 PM
#6
Lively Member
-
Jan 3rd, 2003, 04:21 PM
#7
Thread Starter
Member
We're so close...
So far this is what I've got:
Private Sub cmdExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcel.Click
Try
Dim FileName As String = "PendingSteps.xls"
Dim FileAndPath As String = "C:\My Documents\" & FileName
Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & FileName)
Response.Flush()
Response.WriteFile(FileAndPath)
Catch ex As OleDb.OleDbException
lblError.Text = "There has been an error. Please advise MIS of this error message: " & ex.Message
End Try
End Sub
It is finally making an Excel worksheet, but it's 0 bytes (meaning it's not sending the datagrid, just a blank file) How would I make sure the datagrid is going too?
Thanks again for all the help Ken...
Ooogs
-
Jan 3rd, 2003, 04:40 PM
#8
Lively Member
have to referece OWC
Sub Export(Source As Object, E As EventArgs)
Dim objExcel As New OWC.Spreadsheet() 'Referencing Microsoft Office Web Compenents 9.0'
Dim myRow As DataRow
Dim myColumn As New DataColumn()
Dim RowCount As Object
Dim ColumnCount As Object
Dim FileName As String = "Example.xls"
Dim FileAndPath As String = "C:\" & FileName
'Add the column headers to the excel sheet'
For Each myColumn In myTable.Columns
ColumnCount += 1
objExcel.ActiveSheet.Cells(1, ColumnCount) = myColumn.ColumnName
Next
ColumnCount = 0
RowCount = 0
'Add the data to the excel sheet'
For Each myColumn In myTable.Columns
ColumnCount += 1
RowCount = 1
For Each myRow In myTable.Rows
RowCount += 1
objExcel.ActiveSheet.Cells(RowCount, ColumnCount) = CStr(myRow(myColumn))
Next
Next
ColumnCount = 0
'Autofit the columns to make them look pretty'
For Each myColumn In myTable.Columns
ColumnCount += 1
objExcel.ActiveSheet.Columns(ColumnCount).EntireColumn.AutoFitColumns()
Next
'Do any formatting you wish.....'
'objExcel.ActiveSheet.Columns.InsertColumns(0)
objExcel.ActiveSheet.Rows.InsertRows(2)
objExcel.ActiveSheet.Cells(1, 1) = "Test Report"
objExcel.ActiveSheet.Rows(1).Font.Bold = True
objExcel.ActiveSheet.Rows(3).Font.Bold = True
'This saves the excel file to C:\Example.xls on the server'
objExcel.ActiveSheet.Export(FileAndPath, OWC.SheetExportActionEnum.ssExportActionNone)
'This opens the download dialoge box and allows the user'
'to download the excel sheet from the server'
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName)
Response.Flush()
'This command actually transfers the file'
Response.WriteFile(FileAndPath)
End Sub
Sub Page_Load(Source As Object, E As EventArgs)
'Set your connection and commandstring'
'I am using the Northwind database'
Dim myConnection As New SqlClient.SqlConnection("server=test;uid=test;pwd=test;database=test")
Dim myCommand As New SqlClient.SqlCommand("Select * From test", myConnection)
Dim myDataAdapter As New SqlClient.SqlDataAdapter(myCommand)
'Clear out the public datatable'
'Dim myTable As New DataTable()
myTable = New DataTable()
'Open connection'
myConnection.Open()
'Fill the public datatable'
myDataAdapter.Fill(myTable)
'Close connection'
myConnection.Close()
'Autogeneratecolumns = false, so you have to'
'create the datagrid through code based on the'
'data in the public datatable'
FormatDataGrid(myTable)
'Set the datagrids datasource and bind the grid'
grdMain.DataSource = myTable
grdMain.DataBind()
End Sub
-
Jan 6th, 2003, 09:32 AM
#9
Thread Starter
Member
KMG -
Thanks much for the help. I do appreciate it. A quick question: I don't see the call to the "SUB Export" procedure in the page_load handler. Should it be in there, or is it called at some other point?
Ooogs
-
Jan 7th, 2003, 10:00 AM
#10
Lively Member
That is because I used a button that the user could click to view in excel.
-
Jan 7th, 2003, 10:13 AM
#11
Thread Starter
Member
So, the click event handler for the button calls the "Sub Export" procedure, correct?
-
Jan 8th, 2003, 12:04 PM
#12
Lively Member
yes
<asp:Button id="btnOpenExcel" runat="server" Text="View in Excel" OnClick="OpenExcel" ToolTip="Opens the Report in Excel." onmouseover="this.style.fontWeight='bold'" onmouseout="this.style.fontWeight='normal'" ></asp:Button>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|