Microsoft
Apple
Hardware
Software
Network
Shareware
Games Online
Live Scores - TV
Microsoft FAQ
»
Development
»
.NET
»
Visual C Sharp
»
Export DataView to Excel Spreadsheet with C#
Export DataView to Excel Spreadsheet with C#
Hello Experts,
I am using the code below to export a DataView to a CSV file. I would prefer to export the dataview into an Excel Spreadsheet because ultimately the data is being used as a datasource for a mail merge. It appears that I cannot use a CSV as a datasource for a mail merge so currently I have a procedure that inserts this data into a Word.doc-- however, it takes a long time to create this Word.doc. I would like to cut out the middle step and believe performance will be better if I use an .xls as opposed to .doc for the datasource.
How can I create a spreadsheet from a DataView?
StreamWriter outputFile = new StreamWriter("c:\\"+fileNa
me+".csv")
;
string delim;
//write Header Row
delim = "";
foreach (DataColumn col in dv.Table.Columns)
{
outputFile.Write(delim);
outputFile.Write(col.Colum
nName);
delim = ",";
}
outputFile.WriteLine();
//write data into each row
foreach (DataRowView row in dv)
{
delim = "";
foreach (object item in row.Row.ItemArray)
{
outputFile.Write(delim);
outputFile.Write(item);
delim = ",";
}
outputFile.WriteLine();
}
outputFile.Close();
Thanks for your help.
Solution: Export DataView to Excel Spreadsheet with C#
use SaveAs method of Excel.WorkBook object
E.g.
// Instantiate Excel and start a new workbook.
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add( Missing.Value );
Then use
objBook.SaveAs(@"C:\folder
\Book1.xls
", m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.x
lNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
Active
Counting the Items in a SharePoint View
remove excisting record from temporary table
Opening MS Word object with a custom normal.dot form MS Access
How to insert UMLAUT into the tables
Sending CListCtrl's NM_CLICK to parent
Password Protect a Network Share
How to prevent SkypeNames.exe process from running
Program will not start because of side-by-side configuration error
ActiveX control not installed
How do I display an outlook calendar on a web page
Problem: Exchange Migration from SBS 2003 to Exchange 2007
Problem: On Timer Stoppage & Macro Error Handling
outlook 2000 "TCP/IP connection was unexpectedly terminated by your mail server. If this problem persists, contact your LAN
Windows Server 2003 Scheduled Tasks Not Running in Background
email notification when disk space is running low
Active Directory failed to construct a mutual authentication service principal name (SPN) for the domain controller.
AccessVB:Set Focus on currently selected record
Problem: What's the best design of DNS in AD-Intergrated zone?
How to insert automatic, sequential numbering on an excel template
Making a chart see a #DIV/0! error as an empty cell
How can I stop MS Excel from calculating cells.
Pass Through Query Repeatedly Prompting for ODBC Connection information
Maintenance overlap for database
Problem: Exporting report to .pdf format
Problem: How can I have a macro execute automatically when data is scanned into a cell?
Problem: Set default line weight, font, etc. in Microsoft PowerPoint 2007
Modify find/replace dialog box settings
What is the Best Way to Setup Microsoft Access Record Navigation?
stsadm
Outer join in access