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."