473,386 Members | 1,823 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,386 software developers and data experts.

How to export data into Excel which is diaplayed using GridView in Visual Studio 2008

Ewan
18
Hi

Im using Visual Studio 2008 and i have created a GridView which displays data from an SQL Table.

Could anyone help me on how could i export this data into an Excel sheet on click of a button.

Thanks
Feb 23 '11 #1

✓ answered by Ewan

I managed to get some answers after alot of googleing..
i created a button and defined its ID as Export2XL

In the Export2XL_Click event i pasted the following code
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Export2XL_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Export2XL.Click
  2.  
  3.         refGridView.Visible = True
  4.  
  5.         Response.Clear()
  6.         Response.Buffer = True
  7.         Response.ContentType = "application/vnd.ms-excel"
  8.         Response.Charset = ""
  9.         Me.EnableViewState = False
  10.         Dim oStringWriter As New System.IO.StringWriter
  11.         Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
  12.  
  13.         refGridView.RenderControl(oHtmlTextWriter)
  14.  
  15.         Response.Write(oStringWriter.ToString())
  16.         Response.End()
  17.  
  18.     End Sub
  19.  
However i got an error message as below:
"Control 'refGridView' of type 'GridView' must be placed inside a form tag with runat=server."

So by further googleing i found that by adding the below code, it sorts out the issue:

Expand|Select|Wrap|Line Numbers
  1.     Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal Control As Control)
  2.  
  3.         ' Verifies that the control is rendered 
  4.  
  5.     End Sub
  6.  

5 5466
Ewan
18
I managed to get some answers after alot of googleing..
i created a button and defined its ID as Export2XL

In the Export2XL_Click event i pasted the following code
Expand|Select|Wrap|Line Numbers
  1.     Protected Sub Export2XL_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Export2XL.Click
  2.  
  3.         refGridView.Visible = True
  4.  
  5.         Response.Clear()
  6.         Response.Buffer = True
  7.         Response.ContentType = "application/vnd.ms-excel"
  8.         Response.Charset = ""
  9.         Me.EnableViewState = False
  10.         Dim oStringWriter As New System.IO.StringWriter
  11.         Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
  12.  
  13.         refGridView.RenderControl(oHtmlTextWriter)
  14.  
  15.         Response.Write(oStringWriter.ToString())
  16.         Response.End()
  17.  
  18.     End Sub
  19.  
However i got an error message as below:
"Control 'refGridView' of type 'GridView' must be placed inside a form tag with runat=server."

So by further googleing i found that by adding the below code, it sorts out the issue:

Expand|Select|Wrap|Line Numbers
  1.     Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal Control As Control)
  2.  
  3.         ' Verifies that the control is rendered 
  4.  
  5.     End Sub
  6.  
Apr 6 '11 #2
Frinavale
9,735 Expert Mod 8TB
I'm glad you found a solution :)
Thanks for sharing it with us
Apr 12 '11 #3
Ewan
18
your Welcome :)
.........................................
Apr 12 '11 #4
Hi Ewan,

With this render control option did you try a gridview with hyperlinkedtext columns and template columns? I think there it fails and the format in excel is incorrect.

May be give a try with "DoddleReport" available @ http://doddlereport.codeplex.com/.
Apr 18 '11 #5
Hello,
It is not difficult to export data to Excel by using GirdView.
I want to share a simple example with you.
Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, EventArgs e)
  2.  
  3.         {
  4.  
  5.             Workbook workbook = new Workbook();
  6.  
  7.             //Initialize worksheet
  8.  
  9.             workbook.CreateEmptySheets(1);
  10.  
  11.             Worksheet sheet = workbook.Worksheets[0];
  12.  
  13.             //Insert DataTable to Excel
  14.  
  15.             sheet.InsertDataTable((DataTable)this.dataGridView1.DataSource, true, 2, 1, -1, -1);
  16.  
  17.             //Set Excel Style
  18.  
  19.             CellStyle Style = workbook.Styles.Add(“Style”);
  20.  
  21.             Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
  22.  
  23.             Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  24.  
  25.             Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
  26.  
  27.             Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
  28.  
  29.             Style.Borders.Color = Color.DarkCyan;
  30.  
  31.             Style.Color = Color.Lavender;
  32.  
  33.             Style.Font.FontName = “Calibri”;
  34.  
  35.             Style.Font.Size = 12;
  36.  
  37.             CellRange range = sheet.Range["A3:F26"];
  38.  
  39.             range.CellStyleName = Style.Name;
  40.  
  41.             //Set Header Style
  42.  
  43.             CellStyle styleHeader = sheet.Rows[0].Style;
  44.  
  45.             styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
  46.  
  47.             styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
  48.  
  49.             styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
  50.  
  51.             styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
  52.  
  53.             styleHeader.Borders.Color = Color.DarkCyan;
  54.  
  55.             styleHeader.VerticalAlignment = VerticalAlignType.Center;
  56.  
  57.             styleHeader.HorizontalAlignment = HorizontalAlignType.Center;
  58.  
  59.             styleHeader.KnownColor = ExcelColors.Cyan;
  60.  
  61.             styleHeader.Font.FontName = “Calibri”;
  62.  
  63.             styleHeader.Font.Size = 14;
  64.  
  65.             styleHeader.Font.IsBold = true;
  66.  
  67.             //Set Row Height and Column Width
  68.  
  69.             sheet.AllocatedRange.AutoFitColumns();
  70.  
  71.             sheet.AllocatedRange.AutoFitRows();
  72.  
  73.             sheet.Range["A3:F26"].RowHeight = 16;
  74.  
  75.             sheet.Rows[0].RowHeight = 20;
  76.  
  77.             //Save and Launch File
  78.  
  79.             workbook.SaveToFile(“DataImport.xlsx”, ExcelVersion.Version2010);
  80.  
  81.             System.Diagnostics.Process.Start(workbook.FileName);
  82.  
  83.         }
  84.  
  85.         private void Form1_Load_1(object sender, EventArgs e)
  86.  
  87.         {
  88.  
  89.             //Load Data from Database to DataGridView
  90.  
  91.             string connString = @”Provider=Microsoft.ACE.OLEDB.12.0;
  92.  
  93.                                 Data Source=D:\work\VIP.mdb;Persist Security Info=False;”;
  94.  
  95.             DataTable dataTable = new DataTable();
  96.  
  97.             using (OleDbConnection conn = new OleDbConnection(connString))
  98.  
  99.             {
  100.  
  101.                 conn.Open();
  102.  
  103.                 string sql = “select Name,Gender,Birthday,Email,Number,Country from VIP”;
  104.  
  105.                 OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, conn);
  106.  
  107.                 dataAdapter.Fill(dataTable);
  108.  
  109.             }
  110.  
  111.             this.dataGridView1.DataSource = dataTable;
  112.  
  113.         }
If you want to get some more details about it, please visit this article:
http://janewdaisy.wordpress.com/2011...-datagridview/

Hope helpful!
Mar 9 '12 #6

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

Similar topics

0
by: fiona | last post by:
Reading, Berkshire, UK 05 June 2007 - Crainiate Software make details available of the release of Objecto Framework 2.0, an upgrade to their enterprise business component framework, designed to...
24
by: JJ | last post by:
I see the new software is 'RTM' but what does that mean in terms of when we can actually purchase it? Thanks, JJ
4
by: Colin J. Williams | last post by:
The readme.txt has: All you need to do is open the workspace "pcbuild.sln" in Visual Studio, select the desired combination of configuration and platform and eventually build the solution....
2
by: Nikolay Belyh | last post by:
1. Why does Visual Studio 2008 started ignoring //{NO_DEPENDENCIES} in resource.h? If I edit file "resource.h" _manually_, it rebuilds the whole solution. It worked just fine with Visual Studio...
4
by: lichaoir | last post by:
Can I develop asp.net 1.1 application in visual studio 2008? I've tried to find .net framework 1.1 in the project's properties window, but I can't find it! Please help! Thanks for your...
6
by: Miro | last post by:
I can run an exe ( and its install ) i have created on my machine. The exe has a button that populates a dataset and then shoots it to a crystal report. But... Installing the setup.exe on my...
4
by: Miro | last post by:
<i have also added this reply to the other newsgroup - now that I have realizd ( and assuming ) it is not a localized error directly to vb.> I have found this link on the website:...
0
by: _Who | last post by:
I'm trying to free up some space on my system disk. In Add or Remove Programs I see: Microsoft Windows SDK for Visual Studio 2008 .NET Framework Tools Microsoft Windows SDK for Visual Studio...
1
by: Puja Patel | last post by:
hi all, am not sure if this is the right place for this post. I created a website on .net framework 2.0 using visual studio 2005 and web service software factory. I created all my business...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.