Solved

Export DataView to Excel Spreadsheet with C#

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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.

861 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