Solved

Export DataView to Excel Spreadsheet with C#

Posted on 2008-06-11
4
3,310 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now