Bulk export Word using the Icsharpcode.sharpziplib+aspose template

Source: Internet
Author: User

Because it is a web-side project, it reads the data directly from the Aspose template after clicking the button, then operates in memory instead of downloading it locally and then packaging it for in-memory download. Nonsense not much to say directly on the code

1         PublicActionResult Exportzip (intTestID)2         {3             stringStrsavepath = Server.MapPath ("~/wordtemplate/personalitytest.zip");4             //get the data that users can redefine according to their needs5             varTester =testerbll.getalllist ();6             stringZipfilename ="Human Resource personality test-zip";7MemoryStream ms =NewMemoryStream ();8             byte[] buffer =NULL;9             varFilePath =string. Empty;Ten             using(ZipFile file =Zipfile.create (ms)) One             { A file. BeginUpdate (); -                 //The loop users here can define their own requirements according to their own needs. -                 foreach(varTinchTester) the                 { -                     vardata =Newdictionary<string,int>(); -                     #regionBuild a Word file and populate the data -                     stringTemplatePath = Server.MapPath ("~/wordtemplate/test.doc"); +                     varDoc =NewDocument (TemplatePath);//Loading Templates -  +                     //Populating Data ADoc. range.bookmarks["A"]. Text = data. ContainsKey ("A") ? data["A"]. ToString ():string. Empty; atDoc. range.bookmarks["B"]. Text = data. ContainsKey ("B") ? data["B"]. ToString ():string. Empty; -Doc. range.bookmarks["C"]. Text = data. ContainsKey ("C") ? data["C"]. ToString ():string. Empty; -Doc. range.bookmarks["D"]. Text = data. ContainsKey ("D") ? data["D"]. ToString ():string. Empty; -Doc. range.bookmarks["E"]. Text = data. ContainsKey ("E") ? data["E"]. ToString ():string. Empty; -Doc. range.bookmarks["F"]. Text = data. ContainsKey ("F") ? data["F"]. ToString ():string. Empty; -Doc. range.bookmarks["G"]. Text = data. ContainsKey ("G") ? data["G"]. ToString ():string. Empty; inDoc. range.bookmarks["H"]. Text = data. ContainsKey ("H") ? data["H"]. ToString ():string. Empty; -Doc. range.bookmarks["I"]. Text = data. ContainsKey ("I") ? data["I"]. ToString ():string. Empty; toDoc. range.bookmarks["Count"]. Text =count. ToString (); +Doc. range.bookmarks["name"]. Text =Tester.name; -Doc. range.bookmarks["Tel"]. Text =Tester.tel; the                     //Doc. range.bookmarks["Result"]. Text = result. Caption.replace ("<br/>", String. Empty); //reject <br/> line break symbols *Doc. range.bookmarks["result"]. Text =string. IsNullOrEmpty (result. Caption)?string. Empty:result. Caption.replace ("<br/>",string. Empty);//reject <br/> line break symbols $                     #endregionPanax Notoginseng                     //name of person under test. doc -                     varFileName =string. Format ("{0}.doc", t.name); the                     using(MemoryStream stream =NewMemoryStream ()) +                     { A Doc. Save (stream, saveformat.doc); theStringdatasource ds =NewStringdatasource (stream); + file. Add (ds, fileName); -                     } $                      $                 } - file.commitupdate (); -Buffer =New byte[Ms. Length]; theMs. Position =0; -Ms. Read (Buffer,0, buffer. Length);//reads the contents of the file (1 reads Ms. length/1024m)Wuyi Ms. Flush (); the Ms. Close (); -             } Wu response.clear (); -Response.Buffer =true; AboutResponse.ContentType ="application/x-zip-compressed"; $Response.AddHeader ("content-disposition","attachment;filename="+Httputility.urlencode (Zipfilename)); - response.binarywrite (buffer); - Response.Flush (); - Response.End (); A  +             returnView (); the}

There is also an inner class that implements the Istaticdatasource interface

1     classStringdatasource:istaticdatasource2     {3          Public byte[] bytes {Get;Set; }4          PublicStringdatasource (MemoryStream ms)5         {6bytes =Ms. GetBuffer ();7         }8 9          PublicStream GetSource ()Ten         { OneStream s =NewMemoryStream (bytes); A             returns; -         } -}

To prevent the generated compressed file from garbled, you can add these two lines of code on the Using

1 Encoding GBK = encoding.getencoding ("gbk"); 2 ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = GBK. CodePage;

Bulk export Word using the Icsharpcode.sharpziplib+aspose template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.