Export DataView to Excel Spreadsheet with C#

Posted on 2008-06-11
Last Modified: 2013-12-17
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:\\"+fileName+".csv");
            string delim;

            //write Header Row
            delim = "";
            foreach (DataColumn col in dv.Table.Columns)
                delim = ",";

            //write data into each row
            foreach (DataRowView row in dv)
                delim = "";
                foreach (object item in row.Row.ItemArray)
                    delim = ",";

Thanks for your help.
Question by:soapygus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Author Comment

ID: 21769714
Thanks rstomar --

The second link led the way here:

I've been able to populate an XLS with my DataView using that documentation.  However, I am struggling trying to figure out how to create an XLS with a filename that I specify.  I haven't yet found the answer.  Right now my procedure builds a spreadsheet called "Book1."

I need to pass the filename to the procedure and then save and close the XLS when it's done.  Can you point me in the right direction there?

Thanks for your help.

Accepted Solution

rstomar earned 500 total points
ID: 21771564
use SaveAs method of Excel.WorkBook object

// 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.xlNoChange,
      m_objOpt, m_objOpt, m_objOpt, m_objOpt);


Author Closing Comment

ID: 31466302
Thank you.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question