473,387 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exporting from an HTML table to a Spreadsheet

Hi there,

I have a asp.net page that dynamically constructs a table based on data
retrieved from a database. This is an excerpt of the code used:

' Create a row
Dim row As HtmlTableRow = New HtmlTableRow
' Create a column and add it to the row
Dim cell As HtmlTableCell = New HtmlTableCell
cell.InnerText = sValue
row.Cells.Add(cell)
' ... other columns created and added...
tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
running at server
' ... loop to add more rows as needed...

Now I need to export this table to a spreadsheet when the user clicks a
button on the page.

I have tried a variety of routines, suggestions, etc. None of them have
worked. I only get an empty spreadsheet, sometimes with very wide column (I
don't know why).

Almost all examples I have seen involve the use of datasets/datagrids. For
reasons not relevant to the question, I cannot use datagrids for this.

The question is: Can an HTML table be exported or converted to a spreadsheet
directly (or through a routine, whatever)?

Many thanks,

--
Carlitos
Feb 4 '08 #1
4 1740
This sample is for a GridView, but it should work for any servercontrol:

// you also need this to suppress "not inside Form tag" errors on
RenderControl
public override void VerifyRenderingInServerForm(Control control)
{
// no-op, just override so base doesn't do anything
}
protected void lnkExport_Click(object sender, EventArgs e)
{

Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=OvertureReport.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving
than
// uncomment the line below
Response.Cache.SetCacheability(HttpCacheability.No Cache);
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
gvDomains.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

}

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Carlitos" wrote:
Hi there,

I have a asp.net page that dynamically constructs a table based on data
retrieved from a database. This is an excerpt of the code used:

' Create a row
Dim row As HtmlTableRow = New HtmlTableRow
' Create a column and add it to the row
Dim cell As HtmlTableCell = New HtmlTableCell
cell.InnerText = sValue
row.Cells.Add(cell)
' ... other columns created and added...
tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
running at server
' ... loop to add more rows as needed...

Now I need to export this table to a spreadsheet when the user clicks a
button on the page.

I have tried a variety of routines, suggestions, etc. None of them have
worked. I only get an empty spreadsheet, sometimes with very wide column (I
don't know why).

Almost all examples I have seen involve the use of datasets/datagrids. For
reasons not relevant to the question, I cannot use datagrids for this.

The question is: Can an HTML table be exported or converted to a spreadsheet
directly (or through a routine, whatever)?

Many thanks,

--
Carlitos
Feb 4 '08 #2
Yes, you can use this free control to export your HTML to Excel, or you can
read about how the source code works and do it yourself:
http://SteveOrr.net/articles/ExportPanel.aspx

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
http://iPhonePlaza.net
"Carlitos" <Ca******@discussions.microsoft.comwrote in message
news:27**********************************@microsof t.com...
Hi there,

I have a asp.net page that dynamically constructs a table based on data
retrieved from a database. This is an excerpt of the code used:

' Create a row
Dim row As HtmlTableRow = New HtmlTableRow
' Create a column and add it to the row
Dim cell As HtmlTableCell = New HtmlTableCell
cell.InnerText = sValue
row.Cells.Add(cell)
' ... other columns created and added...
tblSummaryTable.Rows.Add(row) ' tblSummaryTable already exists on page,
running at server
' ... loop to add more rows as needed...

Now I need to export this table to a spreadsheet when the user clicks a
button on the page.

I have tried a variety of routines, suggestions, etc. None of them have
worked. I only get an empty spreadsheet, sometimes with very wide column
(I
don't know why).

Almost all examples I have seen involve the use of datasets/datagrids.
For
reasons not relevant to the question, I cannot use datagrids for this.

The question is: Can an HTML table be exported or converted to a
spreadsheet
directly (or through a routine, whatever)?

Many thanks,

--
Carlitos
Feb 4 '08 #3
On Feb 4, 1:23*pm, "Steve C. Orr [MCSD, MVP, CSM, ASP Insider]"
<St...@Orr.netwrote:
Yes, you can use this free control to export your HTML to Excel, or you can
read about how the source code works and do it yourself:http://SteveOrr.net/articles/ExportPanel.aspx

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsiderhttp://SteveOrr.nethttp://iPhonePlaza.net
I have been using Steve's free ExportPanel to export web page to
Word. Really nice, and I strongly recommend. Thank Steve for sharing
for free.
Feb 4 '08 #4
you can save a HTML table to a file with extension "xls" and then
redirect user (i.e users browser) to that page:
http://www.siccolo.com/temp/html_2_excel.gif
Feb 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
Hi all, I've seen this noted in many posts, but nothing I've checked out gives me any clue on how to do this. Basically as my topic says, I have a DTS and I simply need to export some data...
2
by: Regnab | last post by:
I've got my code working so that it'll count the number of columns in the table and move across (eg Range A-P and then range Q-W). Problem is when I get to the end of the single letters and get...
1
by: setterst | last post by:
I am quite new to VBA and Access, but I am trying to figure out how to export specific values from a table in Access, so I can save it in a given cell in Excel. I have been able to figure out how...
6
by: Kevin Humphreys | last post by:
Hi There, I am trying to export a recordset to an excel file using the Content Type below in the header. Response.ContentType = "application/vnd.ms-excel" Which works fine however the...
21
by: bobh | last post by:
Hi All, In Access97 I have a table that's greater than 65k records and I'm looking for a VBA way to export the records to Excel. Anyone have vba code to export from access to excel and have the...
7
by: semijoyful | last post by:
OS: Win XP SP2 Access version: 2003 Excel version: 2003 I am new at this, as I am sure you have gathered from this post title:) I am working on a form where users can input data in Access and...
9
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes...
3
LoanB
by: LoanB | last post by:
Hi Guys I am writing an application where by I need to export data from a data table (already populated with data from SQL Server). I have never done this before so not sure where to start. ...
7
by: jkwok | last post by:
Hi, I have a spreadsheet which I've imported into one table, 'Assets', in Access. In the table, I have to separate the data based on one attribute: 'empID'. empID is the employee ID and can occur...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.