Exporting data to an Excel file.

  • Thread starter Thread starter Greg Smith
  • Start date Start date
G

Greg Smith

Hi, I would like to export the data in a table, or a subset of the data to
an Excel file. Is this do-able?

Any help is greatly appreciated.
 
Thank you for your reply. Most of the stuff seems to deal with coming FROM
Excel and the one dealing with exporting is in vb and has a lot of extra
sheet info etc. Do you know of any plain vanilla C# examples you can point
me to?


Miha Markic said:
Hi Greg,

There is a thread few threads below this one (ADO for Excel).

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Greg Smith said:
Hi, I would like to export the data in a table, or a subset of the data to
an Excel file. Is this do-able?

Any help is greatly appreciated.
 
Hi Greg,

Try this two links:

HOW TO: Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With
Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;316934

PRB: Error Occurs When You Use ADO.NET OLEDbDataAdapter to Modify Excel
Workbook
http://support.microsoft.com/default.aspx?scid=kb;en-us;316756

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Greg Smith said:
Thank you for your reply. Most of the stuff seems to deal with coming FROM
Excel and the one dealing with exporting is in vb and has a lot of extra
sheet info etc. Do you know of any plain vanilla C# examples you can point
me to?


Miha Markic said:
Hi Greg,

There is a thread few threads below this one (ADO for Excel).

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Greg Smith said:
Hi, I would like to export the data in a table, or a subset of the
data
 
Hi Greg,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to export the values in a
DataTable to an Excel file. If there is any misunderstanding, please feel
free to let me know.

Generally, there are two ways to do that.

1. The simplest way to achieve this is to write each value of a field to a
text file (seperate with a tab stop "\t" ), and save it with the extension
".xls". When opening the file with Excel, it will be converted to Excel
format automatically. Here is an example:

StreamWriter sw = new StreamWriter(@"c:\ex.xls", false,
System.Text.Encoding.Unicode);
foreach(DataRow dr in dt.Rows)
{
for(int i=0;i<dt.Columns.Count;i++)
sw.Write(dr + "\t");
sw.WriteLine(System.Environment.NewLine);
}
sw.Close();

For more information about StreamWriter class, please check the following
link for reference:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemIOStreamWriterClassctorTopic.asp

2. We can use the Excel COM component to achieve that. In the Project menu,
select Add Reference. You can find Microsoft Excel x.0 Object Library in
the COM tab, if Excel has been properly installed on your computer. (x
refers to the version of Excel that has been installed.) Reference that
component and you can create an Excel worksheet to manipulate. However,
this requires Excel to be installed on the machine which runs your
application.

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Greg,

I'd like to know if this issue has been resolved yet. Is there anything I
can help? If anything is unclear, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top