Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Excel File 1

Status
Not open for further replies.

emblewembl

Programmer
May 16, 2002
171
0
0
GB
I want to dynamically create an Excel file from my aspx page. I am having trouble finding examples on how to do this! Does anyone have a basic example they could share with me, including what Namespace has to be used?

Thanks :)

i love chocolate
 
i believe I used to do this in perl by just creating a tab delimited text file with a .xls extension and excel opened them fine. Though .net may have a way to do this the "right" way...
 
if you have a data grid on the page that you want turned into excel its pretty easy. Just stick the name of your datagrid in the response write. I use Datagrid1 below.

i have a datgrid on the page, have a button onclick calls this sub


sub saveexcel (Sender as Object, E As EventArgs)

Set the content type to Excel.
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
DataGrid1.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
end sub
 
gmontano & gagz et al...

I'm using a solution very similar to yours above, with datagrids, however the problem I am having is that my user wants to see the output of my report, which has 3 grids to appear in an 1 XL workbook file with 3 worksheets(tabs).

You wouldn't happen to know a way of achieving this similar to your solution above with ASP.NET using VB.NET?

Star to the first person with a working solution! :)

S.

(The more ASP.NET I do, the more Javascript I find myself using)
 
gmontano:

Looks good, but doesn't work for me.
I have used template columns in my datagrid, and have linkbutton controls, instead of pure text.

I get the following error message:
Control 'grdBookings__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.

Any one know how to get around this?
 
I like the method used here.

with some tweeking you could get 3 spreadsheets.

You need 2003.

To create an xlst just create the spreadsheet you want and save it as XML. Use the example in the above artical to alter your xml spreadsheet to perform the looping you need

NOTE This artical is out of date. Here is the code I use.
note one more thing.

''''''''''''''''''''''''
Public reconDataset As DataSet
Public CurrentAppLocation As String
Public ServerName As String
Public emailAddress As String
Public fileName As String
''''''''''''''''''''''''

Dim tw As XmlTextWriter
Dim tr As XmlResolver

Dim u As New Maxim.Mips.Utility.utility()

Dim xmlDoc As XmlDataDocument = New XmlDataDocument(reconDataset)
Dim xslTran As XslTransform = New XslTransform()
xslTran.Load(CurrentAppLocation & "\" & "ReconExcel.xslt")

tw = New XmlTextWriter(CurrentAppLocation & "ReconReport\" & fileName, System.Text.Encoding.UTF8)

tw.Formatting = Formatting.Indented
tw.Indentation = 3
tw.WriteStartDocument()

xslTran.Transform(xmlDoc, Nothing, tw, Nothing)

tw.Close()


'this part is for changing the version to 2000 so other people can view then
Dim oXL As New Excel.Application

Dim f As Excel.XlFileFormat
oXL.Visible = False

oXL.Workbooks.Open(CurrentAppLocation & "ReconReport\" & fileName)

oXL.Workbooks(1).SaveAs(CurrentAppLocation & "ReconReport\Recon" & fileName, f.xlWorkbookNormal)

oXL.Workbooks(1).Close()
oXL.Quit()
f = Nothing
oXL = Nothing
GC.Collect()

System.IO.File.Delete(CurrentAppLocation & "ReconReport\" & fileName)

Good luck.
 
If anybody knows, Is the same examples are available in C#.NET?. I am trying to use the same using C#.Net.

Please give me some suggestion on this.

Thanks in Advance.
 
The best advice I can give anyone who develops using the .NET framework, is to learn one language as best as you can, then learn the basics of the opposite.

I am a C# developer but I can also read VB.NET very confidently. You cross bridges and don't have to post replies asking for a translation. VB.NET and C# are very similar, I am currently working on someone elses project, a huge system completely written in VB.NET and found that after a bit of practice I can move seemlessly from my own work in C# to VB.NET. (with the exception of a few semicolons at the end of some lines!)

I would recommend this to anyone who relies on .NET

i love chocolate
 
You can do some pretty nifty stuff with Excel and XML to create workbooks and worksheets etc - I only found this out recently after having some difficulties with this myself. Its actually quite simple and avoids any server excel automation and the associated pitfalls. Check out this article at 15secs...



Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top