473,406 Members | 2,710 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,406 software developers and data experts.

Export excel file from ASP.NET

im creating an asp.net page with visual basic laguage where i have a gridview, what i need to do is to export the data in that gridview as a web query that i can open with excel and be able to regresh the data form excel.
Aug 6 '12 #1
6 5602
kadghar
1,295 Expert 1GB
Since you cannot guarantee you'll have Excel installed on your web server, the easiest way is to create an HTML table in plain text, then throw it as an .XLS file.

Excel can open HTML tables quite nice; with the little inconvenient it'll show a dialog before opening. (no big deal)
Aug 7 '12 #2
But how do i do that?
Aug 7 '12 #3
Frinavale
9,735 Expert Mod 8TB
Well, you need to create an HTML table within something like a String or a StringBuilder.

Once you have generated the HTML table, you will have to write the HTML table to the Response stream. But first, you need to change the Header to indicate that the Content-Type is "Application/x-msexel" and that the Content-Disposition is an attachment with the file name that you want to save the file as.

For example, say you had an "Export.aspx" page whose purpose is to export the data as described.

In the Page Load event you would have something like:
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.   Dim fileName As String = "exportedData.xls"
  3.  
  4.   Response.Clear()
  5.   Response.AddHeader("Content-Type","Application/x-msexcel")
  6.   Response.AddHeader("Content-Disposition","attachment;filename=" & fileName)
  7.  
  8.   Response.Write(GenerateHTMLTable)
  9. End Sub
(C#)
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, System.EventArgs e)
  2. {
  3.     string fileName = "exportedData.xls";
  4.  
  5.     Response.Clear();
  6.     Response.AddHeader("Content-Type", "Application/x-msexcel");
  7.     Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
  8.  
  9.     Response.Write(GenerateHTMLTable());
  10. }
You would have to implement the GenerateHTMLTable as a method that returns the HTML in a string.

Something like:
Expand|Select|Wrap|Line Numbers
  1. Private Function GenerateHTMLTable() As String
  2.  
  3. 'Retrieve the data you want to export from where ever it is stored
  4.  
  5. 'Declare a StringBuilder to contain the exported HTML table content
  6.  
  7. 'Append a line containing an open <table> tag to the StringBuilder
  8.  
  9. 'Append a line containing a row for the column headers
  10. 'For each column that should exist in the HTML table, Append a column header to the table with the Column Name within it.
  11. 'Append a line closing the row for the column headers
  12.  
  13. 'For each row in your table, add an <tr>
  14. '  For each column in the row, add a <td>
  15. '    add the data within the column
  16. '    add a close </td>
  17. '  finish the loop through the columns
  18. ' add a close for the table row </tr>
  19. 'finish the for each for adding the html row 
  20.  
  21.   Dim theTableData As DataTable = GetDataToExport()
  22.   Dim theTable As New System.Text.StringBuilder
  23.  
  24.   theTable.AppendLine("<table>")
  25.  
  26.   'Inserting table Headers:
  27.   theTable.AppendLine("  <tr>") 
  28.   For Each columnHeader As DataColumn in theTableData.Columns
  29.     Dim columnName As String = columnHeader.ColumnName
  30.     theTable.AppendLine("    <th>")
  31.     theTable.AppendLine("       " & columnName)
  32.     theTable.AppendLine("    </th>")
  33.   Next
  34.   theTable.AppendLine("  </tr>")
  35.  
  36.   'Inserting table Data:
  37.   For Each row As DataRow In theTableData.Rows
  38.     theTable.AppendLine("  <tr>")
  39.  
  40.     Dim column As DataColumn
  41.     For Each column In theTableData.Columns
  42.       theTable.AppendLine("    <td>" & row(column) & "</td>")
  43.     Next
  44.  
  45.     theTable.AppendLine("  </tr>")
  46.   Next  
  47.  
  48.   theTable.AppendLine("</table>")
  49.  
  50.   Return theTable.ToString()
  51. End Function

(C#)
Expand|Select|Wrap|Line Numbers
  1. private string GenerateHTMLTable()
  2. {
  3.  
  4.     //Retrieve the data you want to export from where ever it is stored
  5.  
  6.     //Declare a StringBuilder to contain the exported HTML table content
  7.  
  8.     //Append a line containing an open <table> tag to the StringBuilder
  9.  
  10.     //Append a line containing a row for the column headers
  11.     //For each column that should exist in the HTML table, Append a column header to the table with the Column Name within it.
  12.     //Append a line closing the row for the column headers
  13.  
  14.     //For each row in your table, add an <tr>
  15.     //  For each column in the row, add a <td>
  16.     //    add the data within the column
  17.     //    add a close </td>
  18.     //  finish the loop through the columns
  19.     // add a close for the table row </tr>
  20.     //finish the for each for adding the html row 
  21.  
  22.     DataTable theTableData = GetDataToExport();
  23.     System.Text.StringBuilder theTable = new System.Text.StringBuilder();
  24.  
  25.     theTable.AppendLine("<table>");
  26.  
  27.     //Inserting table Headers:
  28.     theTable.AppendLine("  <tr>");
  29.     foreach (DataColumn columnHeader in theTableData.Columns) {
  30.         string columnName = columnHeader.ColumnName;
  31.         theTable.AppendLine("    <th>");
  32.         theTable.AppendLine("       " + columnName);
  33.         theTable.AppendLine("    </th>");
  34.     }
  35.     theTable.AppendLine("  </tr>");
  36.  
  37.     //Inserting table Data:
  38.     foreach (DataRow row in theTableData.Rows) {
  39.         theTable.AppendLine("  <tr>");
  40.  
  41.         DataColumn column = null;
  42.         foreach (DataColumn column_loopVariable in theTableData.Columns) {
  43.             column = column_loopVariable;
  44.             theTable.AppendLine("    <td>" + row[column] + "</td>");
  45.         }
  46.  
  47.         theTable.AppendLine("  </tr>");
  48.     }
  49.  
  50.     theTable.AppendLine("</table>");
  51.  
  52.     return theTable.ToString();
  53. }
Please note that I did not test the above code that generates the HTML Table, so if you copy & paste it into your code, be prepared to debug and test it.

-Frinny
Aug 7 '12 #4
that code export the entire page, but i only want to export a table or gridview
Aug 7 '12 #5
kadghar
1,295 Expert 1GB
1) Create a FORM than only contains the gridView, or
2) Create a string by your own. It's quite simple to develop it by yourself.
Aug 8 '12 #6
thanks a lot, i already fix my issue, i did the option with the grdiview into a single form. Thanks
Aug 8 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Martin | last post by:
I have a situation where I'm displaying some information in a table on a web page. I've given the user the ability to make several different "queries" and show different sub-sets of the data. I...
1
by: tkaleb | last post by:
I have to create output file in a text, MS Access, MS Excel and .dbf format from C# Win/ADO.NET application. Data are collected in DataSet and there is no problem to make text file. However, I have...
27
by: jeniffer | last post by:
I need to create an excel file through a C program and then to populate it.How can it be done?
2
by: RICHARD BROMBERG | last post by:
I wrote a small Access application that accepts a City Name and a Street Name and runs a Query based on them . I want to create an Excel Spread sheet that contains all the matches found by the...
1
by: Venkat | last post by:
Can we create an Excel Addin with VC++.net? We are developing an application in c# which interacts with Excel application. The UI will be Excel and based on the inputs given through excel I...
2
by: krissh | last post by:
I know How Excel sheet in php .But wat i want is how to create Multiple sheets in Excel . This is the code for creating one excel sheet <?php header('Content-type:application/ms-xls'); ...
0
by: kennedystephen | last post by:
For the life of me, I cannot get this ... I have 1 excel document. I want to open that document and copy the first 50 rows to a new document. Then get the next 50 rows and copy those to a brand...
4
by: Steve Kershaw | last post by:
Hi, I have a simple web page that starts Excel and fills it with some dummy data. Using IE I can run this web page from the C: drive (IE: "C: \startexcel.htm") and it runs fine. However, when I...
8
by: yogarajan | last post by:
hi All how can i create new excel sheet through asp.net (with c#) i have create report in aspx (with c#) and i store data in excel sheet also so user can select view report through web page or...
4
by: =?Utf-8?B?Sm9zaW4gSm9obg==?= | last post by:
I could create MS Excel sheet using ASP.NET 2.0 with C# but it is not being created in some systems, following error occurs when the program compiles : Microsoft Office Excel cannot open or...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.