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

Need to export content of a datagrid to EXCEL

Hi,

I need to export the content of a DataGrid (in Windows application in C#), into
an Excel spreadsheet.

Anyone knows how to do this? Any code snippets would help!

thanks a lot,

Maria
Nov 15 '05 #1
5 3727
Maria,

Your options are expanded or reduced based on the version of Excel that
you are running. If you are runing Office XP (I believe), then I believe
that Excel has support for XML in it (I know that 2003 has it, and you can
definitely do it in this case). If there is XML support, you could save
your contents as an XML file and then import that into Excel.

If not, then you will have to cycle through the rows and columns and
insert the values one by one.

Either way, you will have to set a reference to the Microsoft Office
Object Library, as well as the Microsoft Excel Object library, and use the
classes in there to instatiate/get the Excel sheet and then write the values
to it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maria L." <an*******@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
Hi,

I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet.

Anyone knows how to do this? Any code snippets would help!

thanks a lot,

Maria

Nov 15 '05 #2
thanks for your reply

Actually, I am running Office 2000, so I will need to do it row by row

What is the name of the libraries that I have to import
Do you have any code snippet that might give me an idea
I know the general idea, but I don't know where to start

Maria
Nov 15 '05 #3
Maria,

You will have to go to the COM tab when you select "Add Reference" to
your project. The "Microsoft Office Object Library" will be there, as well
as the "Microsoft Excel Object Library". When you set references to that,
it will create interop assemblies in your project which you will have to
ship with your product.

Also, you will have to install Excel separately, and make sure that it
is the same version. The interop assemblies only handle the calling of the
COM objects, they don't wrap the COM objects for distribution.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Maria L." <an*******@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
thanks for your reply.

Actually, I am running Office 2000, so I will need to do it row by row.

What is the name of the libraries that I have to import?
Do you have any code snippet that might give me an idea?
I know the general idea, but I don't know where to start!

Maria

Nov 15 '05 #4
here is a code snippet from one of my apps..it works for me..

public void ExportToExcel(DataSet myDataSet) {

try {

// Export The Dataset passed to an Excel Spreadsheet.

Excel.Application oXL;

Excel.Workbook oWB;

Excel.Worksheet oSheet;

oXL = new Excel.ApplicationClass();

oXL.Visible = true;

oWB = (Excel.Workbook)(oXL.Workbooks.Add(Missing.Value)) ;

oSheet = (Excel.Worksheet)oWB.ActiveSheet;

oSheet.Name = "QueryBuilder Export";

oXL.WindowState = Excel.XlWindowState.xlMinimized;

// Initialise the progress form.

frmProgress newProgress = new frmProgress();

newProgress.pbProgress.Minimum = 1;

newProgress.pbProgress.Value = 1;

newProgress.pbProgress.Step = 1;

newProgress.Show();
foreach(DataTable table in myDataSet.Tables) {

// Set Maximum to the total number of files to copy.

newProgress.pbProgress.Maximum = table.Rows.Count;

int row = 1, column = 1;

// Need to export the column headers here.

foreach (object oH in table.Columns) {

oSheet.Cells[row, column] = oH.ToString();

column++;

}

row++;

foreach(DataRow r in table.Rows) {

column = 1;

foreach (object o in r.ItemArray) {

oSheet.Cells[row, column] = o.ToString();

column++;

}

row++;

newProgress.pbProgress.PerformStep();

Application.DoEvents();

}

}

newProgress.Close();
MessageBox.Show("Export Process has completed");

oXL.WindowState = Excel.XlWindowState.xlMaximized;
}

catch (Exception e) {

// Exception Handler

MessageBox.Show("Export Process has Errored : " + e.Message);

}

}

"Maria L." <an*******@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
thanks for your reply.

Actually, I am running Office 2000, so I will need to do it row by row.

What is the name of the libraries that I have to import?
Do you have any code snippet that might give me an idea?
I know the general idea, but I don't know where to start!

Maria

Nov 15 '05 #5
Hi Maria,

Check out this article:
http://support.microsoft.com/default...oduct=NETFrame

There is an another way which is more difficult.
You might build xls file directly...

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

"Maria L." <an*******@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
thanks for your reply.

Actually, I am running Office 2000, so I will need to do it row by row.

What is the name of the libraries that I have to import?
Do you have any code snippet that might give me an idea?
I know the general idea, but I don't know where to start!

Maria

Nov 15 '05 #6

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

Similar topics

2
by: bill | last post by:
Hi All, When I send html content with Excel MIME type, the page break style sheet {page-break-before: always} doesn't work at Excel spreadsheet. Any idea to convert html page break style sheet to...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
11
by: Grey | last post by:
is it possible to export excel directly from ASP.NET to Excel? My requirement is that the data on the datagrid is exported to MS Excel file after user clicks the button on the web page. Excel will be...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
1
by: Server Control | last post by:
Hi, I want to export an entire Page to excel. If I try to export the page with Inline disposition: Response.AddHeader("content-disposition", "inline;filename=abc.xls"); the css and formatting...
2
by: wubin_98 | last post by:
Hi, I want to export a gridview data and a image from image control to export to an Excel document. When I executed following code, GridView data was exported to Excel properly. But no image...
2
by: Amirallia | last post by:
Hello Here is nmy code to export a datagrid to csv file : For lLigne = 0 To Me.dg1.Items.Count - 1 For lColonne = 0 To Me.dg1.Items(lLigne).Cells.Count - 2 If...
4
by: mina | last post by:
One of my client want to export data into excel file but he can not installed office just installed excel and want to export file into ..xls format at that time he got error .... Retrieving the...
1
by: forumaic | last post by:
Hello, I am trying to export data to excel from datagrid, and I am getting an error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.