Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter...

15
Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously QMASTOR) Website: www.petermessenger.com Email : [email protected] Sole IT Developer : http://www.physiotherapyexercises.com

Transcript of Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter...

Page 1: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Using Open Xml and Helper Libraries to generate Excel and Word documents for websites

By Peter MessengerSenior Developer

Triple Point Technologies (previously QMASTOR)

Website: www.petermessenger.com Email : [email protected]

Sole IT Developer : http://www.physiotherapyexercises.com

Page 2: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Why Generate Excel/Word documents?

• Portable• Commonly Used• Users are quite familiar with using it• Can be read by many other programs

• Used at work and for my home project to output ResX files for translation by professional or other language speakers

Page 3: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

How to generate excel files?

Idea 1 – used by student who developed our translation program

COM Automation– “Ok” for single user desktop app– Not Thread Safe– Excel.exe not written to support concurrent users– Microsoft excel needs to be installed on the server– Very, very slow for large datasets– Not recommended by Microsoft

Page 4: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

How to generate excel files?

Idea 2 – used in my output page on my website (2005)

Generate CSV or tab files and import• No compression, files can be quite large

Import/Export using Open OLE and Sql• Can be quite temperamental due to the way it determines the column types – based on looking

at the first “X” rows• Mixed column types (integers and text) can result in data being nulled• Varying length columns can be truncated due to it determining column types text (255

characters max) versus long text columns types

using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\temp\\test.xls;Extended Properties='Excel 8.0;HDR=Yes'")) {

conn.Open(); OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ([Column1] string, [Column2] string)", conn); cmd.ExecuteNonQuery();

}

Page 5: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Problems?

• Both methods are problematic• The users can “break” the files by adding

columns, changing names or using sort badly• This results in data corruption and lots of

heart ache when uploading them

So it there a better way?

Page 6: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

A Better Way?

• Office 2007 introduced the Open Xml Format– Zipped xml format– Developer website http://openxmldeveloper.org/– Can download Open XML Productivity Tool• This can read any office file, display the xml and

generate C# script to regenerate the file• Based on this you can use files for templates and add

data or generate files from scratch

It all sounds good…….

Page 7: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Problems Still?

• Documentation on how to do things isn’t very good

• Can be quite difficult, a single change can result in the file refusing to open with very cryptic error messages

• Reading in large recordsets can be slow

So what then?

Page 8: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

A solution?

• Open source codeplex library– http://closedxml.codeplex.com/– http://epplus.codeplex.com/

I used Closed Xml as the documentation was better and it was a more active project.

It provides a more structured interface and generates the xml document for you in the background.

A good example

http://closedxml.codeplex.com/wikipage?title=Showcase&referringTitle=Documentation

Page 9: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Benefits

• Much easier to develop, takes out the complexity of having to know the xml

• Not brittle, doesn’t break• Loading in large datasets is lightning fast• Can still use existing files as templates• Can generate protected workbooks so users

cannot change anything you do not want them to change

Page 10: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Demo

Translation page download for translators

http://www.physiotherapyexercises.com/ExerciseData_Download.aspx

– Download file– Can only edit their language column, cannot edit english

or key column– Can still filter and sort to aid them in translation– Cannot sort in way that would break data relationships– We then upload file, it determines changes made and

generates sql scripts to update what has changed

Page 11: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Demo 2

Main data for english fileshttp://www.physiotherapyexercises.com/ExerciseData_English.aspx

– Download file– Can add, delete or modify records– Data can have validation built in, with comments

indicating what is acceptable• We then upload file, it determines changes made and

generates sql scripts to update what has changed

Page 12: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

What about Word?

• Have functionality for users to generate web, PDF, or word documents that they can customise. This uses the iTextSharp library.

• Some users want absolute flexibility, being able to provide their own word templates

Want another cheap (free) solution, something on codeplex again?

Page 13: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Word Document Generator

• Another open source codeplex project– http://worddocgenerator.codeplex.com/– Uses document placeholders– Had to amend the source code a bit so it could

handle custom images in the template file

Example for a client booklet– http://www.physiotherapyexercises.com/

Page 14: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Example Code DocumentGenerationInfo generationInfo = GetDocumentGenerationInfo("ExerciseDocumentGenerator", "1.0", datasource, templatefile, false);

var sampleDocumentGenerator = new ExerciseDocumentGenerator(generationInfo); -- ExerciseDocumentGenerator inherits off DocumentGeneratorbyte[] result = sampleDocumentGenerator.GenerateDocument();

Response.ClearContent();Response.Clear();Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";Response.AddHeader("Content-Disposition", "attachment; filename=Exercises.docx;");Response.BinaryWrite(result);Response.Flush();Response.End();

private static DocumentGenerationInfo GetDocumentGenerationInfo(string docType, string docVersion, object dataContext, string fileName, bool useDataBoundControls) { return new DocumentGenerationInfo { Metadata = new DocumentMetadata { DocumentType = docType, DocumentVersion = docVersion }, DataContext = dataContext, TemplateData = File.ReadAllBytes( HostingEnvironment.MapPath("~/" + fileName)), IsDataBoundControls = useDataBoundControls };

}

Page 15: Using Open Xml and Helper Libraries to generate Excel and Word documents for websites By Peter Messenger Senior Developer Triple Point Technologies (previously.

Summary

• Easy to integrate Word and Excel into your websites with open xml and these libraries

• Other libraries are also worth investigating

Comments and questions?