Solved

StreamWriter to output grid into .xls

Posted on 2013-06-26
6
397 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 500 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 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