Solved

StreamWriter to output grid into .xls

Posted on 2013-06-26
6
383 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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