Creating an Excel SpreadsheetML in code. (Without Excel!) - c#

With Excel 2003 and higher it is possible to use the SpreadsheetML format to generate Excel spreadsheets with just an XML stylesheet and XML data file. I've used this in some project and works quite nice, even though it's not easy to do.
From the Microsoft Download site I've downloaded the XSD's that make up SpreadsheetML and in my ignorance, I've tried to convert them to C# classes. Unfortunately, xsd.exe isn't very happy about these schema files so I tend to be stuck.
I don't need an alternative solution to SpreadsheetML since it works fine for my needs. It's just that my code would be a bit easier to maintain for my team members if it's not written in a complex stylesheet. (It sucks to be the only XSLT expert in your company.)
All I want to know if someone has successfully created Excel SpreadsheetML files with .NET without the use of third-party code and without XSLT. And if you do, how did you solve this?
(Or maybe I just have to discover how to add namespaces to XML elements within XML.Linq...)

A while ago I used the XmlDocument and friends to create a SpreadsheetML document with formulae, formats and so on, so it is possible if a bit fiddly.
This MSDN page is what you need to get started with using the namespace in LINQ.

I have used this library and there is even a tool to generate the C# code that you need from an exsisting excel file.
http://www.carlosag.net/Tools/ExcelXmlWriter/

I had started on a similar problem a few weeks back, but due to some impending issues I had to put it at the back burner.
Back then I referred to this http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx?fid=113399&df=90&mpp=25&sort=Position&tid=2609600
I really couldn't get started with it but plan to get back on it soon. I hope the link helps.
cheers

Related

how to write dll file to access pdf

Hi i'm new programming and i have written few application to access pdf content by using some dll files, but now my question is how can we write our own dll to access the pdf files. I know it's a big process but i'm very much interested to learn about this. any one please help me.
You can start by reading the PDF specification (warning 32MB behind this link) in order to understand how the PDF file format is implemented. This is necessary if you want to be able to parse it and extract the information you are interested in.
In the meantime (as this reading might occupy you during a certain amount of time) if you have pressing project deadlines you probably want to use an existing library such as iTextSharp.
I know it's a big process but i'm very much interested to learn about this.
That's true. I'd like to suggest to study some open source APIs (iTextSharp) and PDF SDK.

How do I generate a PDF/Excel file from an SQL database using C# and MVC 2?

I need to make the information in the database usable by allowing the user to download it as a PDF or Excel spreadsheet (either one works, both is perfect).
I've looked around at a bunch of options, but I really can't decide which one I should use, let alone if any of those options are actually useful. Most of the options I've found revolve around converting already existing HTML files into PDFs which is not what I need. Also, it needs to be free. My bosses haven't given me a budget to spend on this
I'm not sure what other information I should include here.
Well, any help is greatly appreciated. If you have questions about missing information, I'll get it posted ASAP. I'm here all day, so I'll be able to respond to any comments very quickly.
EDIT: Oh wow! Huge thanks, guys, for the massive response! I got a ton of ideas. This is super-helpful. Thanks!
if you want to generate an Excel (or also a Word) you can use openXml. You can create a new document exactly the way you want from pure code.
OpenXml SDK page
The solution I usually propose to my clients in this situation is to use Sql Server Reporting Services (SSRS). You can use the ReportViewer control included with it in order to generate PDF's, Excel spreadsheets, XML files, CSV files, and others. If you need ad hoc reporting, there is a Report Builder available as well.
Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.
Have you looked into the reportviewer control, which is part of Visual Studio?
It allows you to export the report in PDF or Excel format.
http://www.carlosag.net/tools/excelxmlwriter/sample
check this might be useful for you
There are lots of reporting solutions out there such as SQL Server Reporting Services(for which you might already have a license). Take a look at Reporting (free || open source) Alternatives to Crystal Reports in Winforms which can likely be applied to the web with a bit of serialization.
I would suggest thinking about rolling your own depending on the situation. You could use pdfsharp for the pdf export and EPPlus for excel. They are both very easy to use and, I'm pretty sure, available in nuget with a couple of clicks.
If you want to go the Excel route, i'd recommend this article from Stephen Walther entitled ASP.NET MVC Tip #2 - Create a custom Action Result that returns Microsoft Excel Documents. This uses an old trick of writing an HTML document with an Excel mime type. This is different than streaming a native Excel file. And it's fairly easy to change the to rendering a CSV file if you want to strip it down, and make it a more universal file. Just remember to double-quote all the fields if there's a possibility of commas showing up.
If what your doing isn't too complicated you can use CSV files. CSV stands for comma separated values, and it is what it sounds like. You can create simple tables and columns using commas. For example paste the following lines into a text file:
heading1,heading2,heading3
info1,info2,info3
info1,info2,info3
Save the text file as a .csv file and voila - an excel spreadsheet. Obviously it is extremely easy to build these looping object collections. Mind you if you need any complicated text formatting etc then it is not really the best option.

Is there a code generation tool that accepts Excel files as input?

I'm looking to generate some C# code based on Excel data. Basically the Excel sheet contains definitions of functions that need to be generated in C#.
My current experience with code generation is mainly with ORM's. But now the "input" is Excel data.
I'm asking this to prevent me from re-inventing the wheel. Reading data from Excel is easy, and generating C# also. But I'm a lazy programmer.
Is there a code generation tool that accepts Excel files as input?
You can use T4 templates. I know this too common/abstract answer, but this is the way, I think, and I don't think you can find something already exist for that purpose. Visual Studio 2008 has native supports for them. In your T4 template you can read excel file and then use its data to create any code file you want.
I am guessing you will have to hand roll this solution. This does seem a little wonky, would be curious how you got in a situation where you are needing to do code gen based on Excel?!?
Having worked with this problem before, I suggest that you convert the excel file to a CSV.
Im my case, I transformed the CSV file into C# classes (which include property and method definitions).

Is there a way to generate word documents dynamically without having word on the machine

I am planning on generating a Word document on the webserver dynamically. Is there good way of doing this in c#? I know I could script Word to do this but I would prefer another option.
I've worked at a company in the past that really wanted generated word documents, in the end they were perfectly satisfied with RTF docs that had a ".doc" extension. Word has no problem recognizing and opening them.
The RTF docs were generated with iText.net (free .net library), the API is pretty easy to use, performs extremely well, you don't need word on the machine, also, you could extend to generating PDF, HTML, and Text docs in the future with very little effort. After four years the solution I created is still in place, so that's a little testimony in iText.net's favor.
It looks like the official iText page suggests that iText Sharp is the best .Net choice right now, so that's another option
You'd be better off generating an rtf file, which word will know how to open.
If want to generate Office 2007 documents check the Open XML File Formats, they're simple zipped XML files, check this links:
Open XML File Formats: What is it, and how can I get started?
Introducing the Office (2007) Open XML File Formats
Edit: Check this project, can serve you as a good starting point:
DocumentMaker
Seems very simple and customizable, look this code snippet:
Paragraph p = new Paragraph();
p.Runs.Add(new Run("Text can have multiple format styles, they can be "));
p.Runs.Add(new Run("bold and italic",
TextFormats.Format.Bold | TextFormats.Format.Italic));
doc.Paragraphs.Add(p);
Word will quite happily open a HTML with a .doc extension. If you include an internal style sheet, you can have it fully formatted. There was previous post on this subject:
Export to Word Document in C#
Creating the old .DOC files (pre-Word 2007) is nigh-impossible without Word itself. The format is just too complex. Microsoft has released the format description, but it's enough to reduce a grown programmer to tears. There is a reason for that too (historical), but that doesn't make things better.
The new .DOCX would be easier, although quite a bit of hassle still. However depending on which Word versions you are targeting, there are some other options too.
For one, there is the classic .RTF. The format is pretty complex still, yet well documented and has strong support across many applications and platforms. And you might use some string-replacing into template files to make things easier (it's non-binary).
Then there are the "old" Word XML files. I think they worked starting with Word XP. Kinda the predecessors of .DOCX. I've used them, not bad. And the documentation is pretty OK.
Finally, the easy way that I would choose, is to make a simple HTML. Word can load HTML files just fine starting with version 2000. In the simplest way just change the extension of a HTML file to .DOC and you have it. You can also add a few word-specific tags and comments to make it look even better in Word. Use the Word's Save As...HTML option to see what they are.
There are third party libraries about that will do the job.
Doing a quick google came up with this one, for example.
I haven't tried any, so I can't give you specific advice, I'm afraid!
Let us know how you get on...
In Office 2007 Microsoft introduced a new file format called the Microsoft Open Office XML Format (.docx). This format is not compatible with older versions of Microsoft Word. Since this is XML you can create or read with out having a Word installed.
Here is the component that generates document based on the custom template. The documents are generated from the sharepoint list ... so the data is pulled from the list item into the document on the fly:
http://store.sharemuch.com/products/generate-word-documents-from-sharepoint-list
Hope that helps,
Yaroslav Pentsarskyy
Blog: www.sharemuch.com

Create an Excel workbook on a x64 system

I have a requirement to export the results of a SQL query to excel. I am currently exporting it into 2007 format, but everything I have found will only run in a x86 enviroment. The web site where the export is to take place is running on a x64 version of IIS.
Changing IIS to run x86 is not an option. My current solution is to save the export request to a DB, and write a windows service that can run in x86 and use that service to process the request.
I have started to look at the Open XML Format SDK 2.0 as a possible better solution.
Has anyone got any experence that would lend to a native .NET solution.
FYI: My budget is $0.00 so buying a tool is a no-go.
Personally from an ASP.NET application standpoint I have found that using XML and XSLT to go into the Excel XML format is the most effective, and really trouble free way to go. There are no costs involved, and once you get the hang of it, it is really quick!
In addition to that, I'm personally strongly against any form of automation to accomplish that kind of task.
I agree with Mitchel Sellers.
Another way is create a simple html document with table, the excel will quite accept.
Example:
<html><body>
<table>
<tr><td>First column</td><td>Second column</td></tr>
<tr><td>Value of first row in first column</td><td>Value of first row in second column</td></tr>
<tr><td>Value of second row in first column</td><td>Value of secondrow in second column</td></tr>
</table>
</body></html>
Mike, do you have any example I can look at? Exporting to Excel isn't something I have had much call foor ( None really ), so any head start would be nice.
TcKs, I tried the Html route and didn't much care for the way excel complained about the file format of html not being the expected .xls(x) format. I'm exporting for Sales guys, so the less messages thay have to wade through, ( and as a result questions I have to anyswer ), the better.
The simplest way that worked well for me is to create the Excel file in the correct format with one row of data.
Then save the file in html format and use it as a template for the actual data.
There is some metadata included that will automatically start Excel when doubleclicking the generated html file.
I had to go this way because we use Excel 2002 and 2003 which have no open file format.
Have a look at: http://www.codeproject.com/KB/office/excelxmlspreadsheet.aspx
I think we modelled an inhouse solution from this.

Categories