C # export Word documents and batch export Word documents (3 ),

Source: Internet
Author: User

C # export Word documents and batch export Word documents (3 ),

Obtain the relative path of the template when initializing WordHelper. To obtain the relative path of a document, you must use it in multiple places. For example, you must first save the file to the specified path during batch export, and then compress the package for download, therefore, I wrote a class about obtaining the relative path of a document.

1 # region obtain the relative path of the document 2 public class WordFilePath 3 {4 # region returns the file path value 5 /// <summary> 6 // returns the file path value 7/ /// </summary> 8 /// <param name = "FilePth"> name of the folder where the file is located </param> 9 /// <param name = "FileName"> file name </param> 10 // <returns> </returns> 11 public static string FileWithFilePath (string FilePth, string FileName = null) 12 {13 string strFile = ""; 14 if (! String. IsNullOrEmpty (FilePth) 15 strFile ++ = "~ /"+ FilePth +"/"; 16 if (! String. isNullOrEmpty (FileName) 17 strFile + = FileName; 18 19 return strFile; 20} 21 # endregion22 23 # region determines whether a file or folder exists 24 // <summary> 25 // determines whether a file or folder exists 26 /// </summary> 27 /// <param name = "FilePth"> file folder name </param> 28 // <param name = "FileName"> file name </param> 29 /// <returns> </returns> 30 public static bool ExistFile (string FilePth, string FileName = null) 31 {32 return System. IO. file. exists (GetFilePath (FilePth, FileName )); 33} 34 # endregion35 36 # region obtains the relative path of the file and folder 37 // <summary> 38 // obtains the relative path 39 of the file and folder. // </summary> 40 // <param name = "FilePth"> </param> 41 // <param name = "FileName"> </param> 42 // <returns> </returns> 43 public static string GetFilePath (string FilePth, string FileName = null) 44 {45 string strPath = ""; 46 strPath = FileWithFilePath (FilePth, FileName); 47 strPath = HttpContext. current. request. mapPath (strPath); 48 return strPath; 49} 50 # endregion51} 52 # endregionView Code

The preceding method can export images in a single word document. Multiple documents can also be generated, but the downloaded code is not packaged yet.
The final call method is as follows. You must first determine whether the template exists:

1 string strpath = "Content/templates"; // file folder where the template is located; 2 string templateFile = "trainee Learning Record table guide template .doc"; 3 if (WordFilePath. existFile (strpath, templateFile) 4 {5 string saveName = WordHelper. saveDocName (form ["NTName"], form ["CardNo"]); // saved name 6 saveName = HttpUtility. urlEncode (saveName, Encoding. getEncoding ("UTF-8"); 7 WordHelper wordhelper = new WordHelper (templateFile); 8 getWordInfo (wordhelper, temp LateFile, NTID, stype. toString (), majorid. toString (), sequence. toString (); 9 return base. file (wordhelper. exportDoc (). toArray (), "application/msword", saveName); 10} 11 else12 {13 return ShowRedirectMessage ("the exported template does not exist! ", StrUrl); 14}View Code

The getWordInfo method is used to piece together the required information into a Word document to reduce code repetition. For example:

1 # region assemble all the information into a Word document 2 public void getWordInfo (WordHelper wordhelper, string tempFile, string ntid, string stype, string mid, string sequence) 3 {4 if (tempFile. contains ("intern Learning Record table export template") 5 {6 Dictionary <string, string> dicWhere = new Dictionary <string, string> (); 7 dicWhere. add ("NTID", ntid); 8 wordhelper. getBasicInfo (typeof (BLL. newTraineeInfo), dicWhere); 9 bytes (wordhelper, ntid); 10 TrainListWord (wordhelper, ntid, stype, mid); 11 AwardListWord (wordhelper, ntid, stype, mid ); 12 ArrangeListWord (wordhelper, ntid, stype); 13 ScoreListWord (wordhelper, ntid, stype, mid, sequence); 14 EvalWord (wordhelper, ntid, stype, mid ); 15} 16} 17 18 # other region information 19 // pre-job training 20 public void PrejobTrainWord (WordHelper wordhelper, string ntid) 21 {22 Dictionary <string, string> dicWhere = new Dictionary <string, string> (); 23 dicWhere. add ("NTID", ntid); 24 wordhelper. getBasicInfo (typeof (BLL. prejobTraining), dicWhere); 25} 26 27 // Round Table 28 public void ArrangeListWord (WordHelper wordhelper, string ntid, string stype) 29 {30 Dictionary <string, string> dicWhere = new Dictionary <string, string> (); 31 dicWhere = new Dictionary <string, string> (); 32 dicWhere. add ("NTID", ntid); 33 dicWhere. add ("StudentType", stype. toString (); 34 wordhelper. getTableList (typeof (BLL. majorCycle), dicWhere, "ArrangeList"); 35}View Code ArrangeList is previously set in the word template, for example, loop out the list with,

The other methods in getWordInfo are similar to the two.

The exported Word documents are roughly as follows:

 

 

Related Article

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.