?
Solved

StreamWriter to output grid into .xls

Posted on 2013-06-26
6
Medium Priority
?
412 Views
Last Modified: 2013-11-08
Hi experts,

As above I need to output a wpf grid control into xls. I tried the below code to start, however when I try to open the xls I get 'the file you are trying to open is in a different format than specified by the file extension '

Code so far...

 var writer = new System.IO.StreamWriter("C:\\Users\\dean.lee\\Desktop\\moi.xls");

                writer.Write("Header1");
                writer.Write("Header2");
                writer.WriteLine();
                writer.Close();

Open in new window



Ideally I want the above and then I will FOR loop through the grid to populate with data.

Thanks,
Dean
0
Comment
Question by:deanlee17
[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
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39279820
You say ".xls", but do you really mean an Excel file, or are you simply trying to write a file that Excel can open (e.g. .csv)?
0
 

Author Comment

by:deanlee17
ID: 39279827
I mean an xls ideally, I really dont want a .csv file.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39279902
Then you'll need to add a reference to the Excel Interop libraries and become cozy with the Excel object model. You'll need Office installed in order to be able to reference the Excel Interop libraries.
0
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 

Author Comment

by:deanlee17
ID: 39279908
I'm sensing you don't recommend the above?

So its easier to just output to CSV? I just didn't want the user having to run through the process of choosing comma delimited etc etc as opposed to the file just opening.

Cheers.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39279913
The above won't work for an Excel file. An Excel file (we're talking .xls here) is a binary file, not a text file. StreamWriter isn't going to work for creating a .xls.

So its easier to just output to CSV?
In my opinion:  yes.

I just didn't want the user having to run through the process of choosing comma delimited etc etc as opposed to the file just opening.
I don't have Office installed on my home machine to confirm, but as I recall when you install Excel it registers itself as the default application for .csv files (and even if it doesn't you could change this). I believe comma-separated files are opened automatically, without the user having to do anything extra. I can confirm this tomorrow when I get to the office.

Now if you choose to delimit the file using something other than a comma...that's another story  : )
0
 

Author Comment

by:deanlee17
ID: 39280563
haha ok cool.

Well I am happy to use a CSV but ideally I need it to open without asking the user any questions, I realise this may not be possible?
0

Featured Post

Industry Leaders: 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 document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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