Solved

StreamWriter to output grid into .xls

Posted on 2013-06-26
6
369 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 74

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 74

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 74

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.Net Web Site Password specs 2 21
Library to convert HTML to PDF. 8 46
Code enhancement 5 14
VB.NET HttpWebRequest 12 34
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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