Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 17th, 2006, 02:27 PM
Authorized User
 
Join Date: Jun 2003
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default Save Datagrid in a Excell format

Hi;

i use the DataSet to fill a datagrid, and i don´t know how to save the values of a datagrid in a Excell format....

Thanks...
 
Old February 17th, 2006, 06:36 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You could save the DataSet in XML format, then open it with Excel.

- A.Kahtava
 
Old February 22nd, 2006, 01:47 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi..here is the code to save the datagrid in excel format. the language used is C#. You will have to pass the dataset that is being used to fill the datagrid as parameter to the function

public static void Convert(DataSet ds ,HttpResponse response)
        {
            //first let's clean up the response.object
            response.Clear();
            response.Buffer = true;
            response.Charset = "";
            response.AddHeader("Content-Disposition", "attachment;filename=Report.xls");

            //set the response mime type for excel
            response.ContentType = "application/vnd.ms-excel";
            //create a string writer

            System.IO.StringWriter stringWrite;
            stringWrite=new System.IO.StringWriter();
            //create an htmltextwriter which uses the stringwriter

            HtmlTextWriter htmlWrite;
            htmlWrite=new System.Web.UI.HtmlTextWriter(stringWrite);
            //instantiate a datagrid
            System.Web.UI.WebControls.DataGrid dg=new System.Web.UI.WebControls.DataGrid();
            //set the datagrid datasource to the dataset passed in
            dg.DataSource = ds.Tables[0];
            //bind the datagrid
            dg.DataBind();
            //tell the datagrid to render itself to our htmltextwriter
            dg.RenderControl(htmlWrite);
            //all that's left is to output the html
            response.Write(stringWrite.ToString());
            response.End();
        }


 
Old February 22nd, 2006, 05:00 PM
Authorized User
 
Join Date: Jun 2003
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

agarwalvidhu;

thanks for your reply.

but i cant execute the code because the VS dont recognizing the response object.

Can you help me again to solve this problem...


Thanks...
 
Old February 23rd, 2006, 01:32 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi...

where exactly are you using this code..?is it a class file.?In that case did u import required SYstem namespaces..? System.Web is the namespace that contains definition for HttpResponse object. Please check if this namespace is imported in the code or not

 
Old February 23rd, 2006, 03:41 PM
Authorized User
 
Join Date: Jun 2003
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi agarwalvidhu;

thanks for your reply again.

My problem is: I have a windows application, not a web project in C#, and this application has a windows form with a datagrid.

I create a button to save the datagrid context to a excell format.
To do this i use your example code.

The System.Web namespace was imported correctly and i use the instance of HttpContext, like below, to acess all of the Response propertys and methods.


public static void ConvertXLS(DataSet ds)
        {
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

            //first let's clean up the response.object
            response.Clear();
            response.Buffer = true;
            response.Charset = "";

But in a runtime the line "System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;" provokes an exception like below:
"System.NullReferenceException: Object reference not set to an instance of an object."

Can you help me again???
thanks...
 
Old February 24th, 2006, 12:53 AM
Registered User
 
Join Date: Feb 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi there...

the exception occured bcoz i observed that you have declared response object of System.Web.HttpResponse class n while instantiating u have used System.Web.HttpContext.Current.Response class.

Also in windows form too, if u r writing the function in code behind page than u can directly say Response.clear() and access all its properties. If the function is in some other class file then i would suggest u to pass Response object as parameter to this function

C if this thing works out






Similar Threads
Thread Thread Starter Forum Replies Last Post
Excell to Datagrid in asp.net sh.rajkumar ASP.NET 1.0 and 1.1 Professional 3 May 22nd, 2006 06:00 AM
save dataset in txt format??? thomaz C# 3 March 20th, 2006 01:29 PM
Export DatSet to Excell format thomaz C# 2 September 16th, 2005 08:43 AM
Save universal date format einarhansen Classic ASP Databases 2 November 26th, 2003 05:58 AM
how to save text with format?? 6cet6 ASP.NET 1.0 and 1.1 Basics 4 November 20th, 2003 06:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.