Asp. NET exports word using Doc templates

Source: Internet
Author: User

First create Doc Template

Again, add this focus method to the related pages in the project

Key methods:

 private String Printword () {try {string templatepath = _savedocpath + "
                /template/rpmb.doc ";
                The generated document path FilePath = _savedocpath + "/hzm/"; if (!
                Directory.Exists (FilePath)) directory.createdirectory (FilePath); if (!

                File.exists (TemplatePath)) return "error: The server does not have a template";
                FilePath = FilePath + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". Doc";

                Copy a copy of File.Copy (TemplatePath, FilePath, true);
                object omissing = System.Reflection.Missing.Value;
                App = new Word.Application (); App.
                Visible = false;
                Object fileName = FilePath; doc = App.
               Documents.Open (ref fileName, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,
           ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,    ref omissing, ref omissing, ref omissing, ref omissing, ref omissing);
                if (doc = = null) {return "error: Server does not open word"; //determine how to Bookmark//Add doc bookmarks if Doc. Bookmarks.exists ("KJJBJNF")) {Doc. bookmarks["KJJBJNF"]. Range.Text = DateTime.Now.ToString ("Yyyymmddhhmmss").
                ToString () + "title"; } word.tables TABS1 = Doc.
                Tables; if (tabs1!= null && TABS1.

                    Count > 0) {word.table Dt_word = tabs1[1];
                    String strSQL = "SELECT * from Stuinfo";
                    DataTable Dt_stu = runsqlreturntable (strSQL);
                    The default starts at line 2nd, and the header int rowidex = 2 in the 1th behavior template; foreach (DataRow row in dt_stu.rows) {Object miss = System.Reflection.Missing .
                        Value;DT_WORD.ROWS.ADD (Miss); The data that will be queried from the database can be//1 by cyclic loading. Add data to a cell Dt_word.cell (Rowidex, 1). Range.Text = row["No"].

                        ToString (); 2. Sets the alignment of the font in this cell Dt_word.cell (Rowidex, 1).

                        Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; Dt_word.cell (Rowidex, 2). Range.Text = row["Name".
                        ToString (); Dt_word.cell (Rowidex, 3). Range.Text = row["Sex"].
                        ToString (); Dt_word.cell (Rowidex, 4). Range.Text = row["Age"].

                        ToString (); 3. Merge cell merge (rows, columns)//dt_word.cell (Rowidex, 3).

                        Merge (Dt_word.cell (Rowidex, 4));
                    rowidex++; } doc.
                Save ();
            Quitword (); catch (Exception ex) {QuitWord (); Return "ERROR:" + ex.
            message;
        Return "true"; }


3. Export can be.

Additional Source http://download.csdn.net/detail/hugaozhuang/6652495

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.