Solved

Export DataView to Excel Spreadsheet with C#

Posted on 2008-06-11
4
3,435 Views
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)
            {
                outputFile.Write(delim);
                outputFile.Write(col.ColumnName);
                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.
0
Comment
Question by:soapygus
[X]
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
4 Comments
 
LVL 5

Expert Comment

by:rstomar
ID: 21763327
0
 

Author Comment

by:soapygus
ID: 21769714
Thanks rstomar --

The second link led the way here: http://support.microsoft.com/kb/302096/EN-US/

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.
0
 
LVL 5

Accepted Solution

by:
rstomar earned 500 total points
ID: 21771564
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.xlNoChange,
      m_objOpt, m_objOpt, m_objOpt, m_objOpt);


0
 

Author Closing Comment

by:soapygus
ID: 31466302
Thank you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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