|
-
Oct 19th, 2009, 05:30 AM
#1
Thread Starter
Lively Member
[RESOLVED] Textbox EXPORT
Alright guys,
I can export gridview contents to an Excel document no problem, but I am struggling to find out how I can simply export the contents of a textbox to an excel document. Would anybody be able to shed any light on this problem? Any help would be greatly appreciated.
-
Oct 19th, 2009, 05:43 AM
#2
Re: Textbox EXPORT
Hey,
What mechanism are you currently using to write out your GridView to excel?
Depending on which technique you are using, it should be a simple extension to write the value of a textbox to excel.
Gary
-
Oct 19th, 2009, 05:54 AM
#3
Thread Starter
Lively Member
Re: Textbox EXPORT
Thanks for getting back to me gep13. Yeah, thats what I thought. It doesn't appear to be the case though. This is the code that I am using for a Gridview Export;
Code:
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Then, within the export button function;
Code:
Dim sw As StringWriter = New StringWriter()
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
Response.Clear()
Response.ContentType = "application/ms-excel"
'Response.ContentType = "application/vnd.excel"
Response.Charset = ""
Response.AddHeader("content-disposition", "attachment;filename=" & Me.txt_SReference.Text & ".xls")
'Bind the dataset to the datagrid, to export the same to excel
Me.grd_order.RenderControl(htw)
'Replace the hyperlinks
Response.Write(Regex.Replace(sw.ToString(), "(<a[^>]*>)|(</a>)", " ", RegexOptions.IgnoreCase))
Response.End()
-
Oct 19th, 2009, 06:28 AM
#4
Re: Textbox EXPORT
Hey,
So here you are using the RenderControl method on the GridView, which happens to render a string that Excel knows about, namely an HTML table.
In the case of a TextBox though, it is likley that this won't be understood by Excel, so simply doing:
Code:
Me.TextBox1.RenderControl(htw)
likely won't work.
But there is nothing to stop you creating a table by directly creating the table in the StringWriter, and then write the contents of the textbox out into the td of the table.
Gary
-
Oct 19th, 2009, 09:28 AM
#5
Thread Starter
Lively Member
Re: Textbox EXPORT
Ok, thanks very much Gary. I'll have a look at trying to do that.
-
Oct 19th, 2009, 09:30 AM
#6
Re: Textbox EXPORT
Hey,
Sounds like a plan.
Let me know how you get on, and whether you have any other questions.
Gary
-
Oct 21st, 2009, 03:17 AM
#7
Thread Starter
Lively Member
Re: Textbox EXPORT
Haha, yeah good plan - just not too sure the execution is good!! I managed to put together the following;
Code:
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
hw.WriteLine("PURCHASE ORDER REFERENCE:")
hw.WriteLine(purchaseorderref) 'PURCHASE ORDER REFERENCE
hw.WriteLine("PURCHASE ORDER DATE:")
hw.WriteLine("12/12/2009") '**************PUT PURCHASE ORDER DATE HERE
hw.WriteLine("CONTACT NAME:")
hw.WriteLine("Alan Thompson") 'CONTACT
hw.WriteLine("")
hw.WriteLine("")
hw.WriteLine("REQUESTED DELIVERY DATE:")
hw.WriteLine(Session("orderdeldate"))
hw.WriteLine("PART SHIP ALLOWED:")
hw.WriteLine("No")
hw.WriteLine("")
hw.WriteLine("")
etc etc....
I guess the major sticking point is that everything is output and written to a new line. Ideally, I want to be able to write to particular cells (so I can line data side by side in cells instead of on different lines) or perhaps construct the putput so it appears in the form of a table. If you have any suggestions, I would gladly welcome them.
Thanks again.
-
Oct 21st, 2009, 04:01 AM
#8
Re: Textbox EXPORT
Hey,
This is sort of what I was suggesting, but you are missing one thing, and that is the construct of a table:
i.e. try this:
Code:
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
hw.WriteLine("<table><tr><td>")
hw.WriteLine("PURCHASE ORDER REFERENCE:")
hw.WriteLine("</td><td>")
hw.WriteLine(purchaseorderref) 'PURCHASE ORDER REFERENCE
hw.WriteLine("</td></tr><tr><td>")
hw.WriteLine("PURCHASE ORDER DATE:")
hw.WriteLine("</td><td>")
hw.WriteLine("12/12/2009") '**************PUT PURCHASE ORDER DATE HERE
hw.WriteLine("</td></tr><tr><td>")
hw.WriteLine("CONTACT NAME:")
hw.WriteLine("</td><td>")
hw.WriteLine("Alan Thompson") 'CONTACT
hw.WriteLine("</td></tr></table>")
Here I am constructing the html table, around the data that you are pulling from different places.
Now this is shear hypothesis, at the minute, I haven't tested it, but in theory, it "should" work.
Let me know how you get on!
Gary
-
Oct 21st, 2009, 04:17 AM
#9
Thread Starter
Lively Member
Re: Textbox EXPORT
Gary, you're a life saver pal. That worked spot on - exactly what I need. Thank you very much for all the help.
-
Oct 21st, 2009, 04:18 AM
#10
Re: [RESOLVED] Textbox EXPORT
Woo hoo!! I wasn't expecting it to go that well Let's put that down to fluke!!
Glad you got what you wanted though!!
Gary
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
|