页面数据写入word的具体代码

//导出word
        function outWord() {
            try {
                //创建word应用程序对象
                var wordapp = new ActiveXObject("word.application");
                //显示word应用程序窗口
                wordapp.visible = false;
                //设置word文件模板的路径
                var s = getFileTemplatePath();
                //打开要填写的模板
                var worddoc = wordapp.application.documents.open(s);
                var range;
                range = wordapp.activedocument.Bookmarks("txtRecordProjectName").Range;
                range.text = document.Form1.txtRecordProjectName.value;
                range = wordapp.activedocument.Bookmarks("txtRecordProjectCode").Range;
                range.text = document.Form1.txtRecordProjectCode.value;
                range = wordapp.activedocument.Bookmarks("chkDesignAmount").Range;
                if (document.getElementById("chkDesignAmount").checked == true) {
                    range.text = "√";
                } else {
                    range.text = "";
                }

                range = wordapp.activedocument.Bookmarks("txtDesignStage").Range;
                range.text = document.Form1.txtDesignStage.value;
                range = wordapp.activedocument.Bookmarks("ddlIsStudy").Range;
                var ddlIsStudy = document.getElementById('<%=ddlIsStudy.ClientID %>');
                var ddlIsStudyValue = ddlIsStudy.options[ddlIsStudy.selectedIndex].value;
                range.text = ddlIsStudyValue;
                range = wordapp.activedocument.Bookmarks("txtDesignAmount").Range;
                range.text = document.Form1.txtDesignAmount.value;
                range = wordapp.activedocument.Bookmarks("areaProjectDescription").Range;
                range.text = document.Form1.areaProjectDescription.value;
                range = wordapp.activedocument.Bookmarks("txtProjectVolumes").Range;
                range.text = document.Form1.txtProjectVolumes.value;
                range = wordapp.activedocument.Bookmarks("txtDesignVolumes").Range;
                range.text = document.Form1.txtDesignVolumes.value;
                range = wordapp.activedocument.Bookmarks("txtFileInstructions").Range;
                range.text = document.Form1.txtFileInstructions.value;
                range = wordapp.activedocument.Bookmarks("txtFileTables").Range;
                range.text = document.Form1.txtFileTables.value;
                range = wordapp.activedocument.Bookmarks("txtFileDrawings").Range;
                range.text = document.Form1.txtFileDrawings.value;
                range = wordapp.activedocument.Bookmarks("txtFileDrawingsA3").Range;
                range.text = document.Form1.txtFileDrawingsA3.value;
                range = wordapp.activedocument.Bookmarks("txtFileDrawingsA4").Range;
                range.text = document.Form1.txtFileDrawingsA4.value;
                range = wordapp.activedocument.Bookmarks("txtDestCompany").Range;
                range.text = document.Form1.txtDestCompany.value;
                range = wordapp.activedocument.Bookmarks("txtDateYear").Range;
                range.text = document.Form1.txtDate.value.split('-')[0];
                range = wordapp.activedocument.Bookmarks("txtDateMonth").Range;
                range.text = document.Form1.txtDate.value.split('-')[1];
                range = wordapp.activedocument.Bookmarks("txtDateDay").Range;
                range.text = document.Form1.txtDate.value.split('-')[2];
                range = wordapp.activedocument.Bookmarks("txtTimeRequired").Range;
                range.text = document.Form1.txtTimeRequired.value;
                range = wordapp.activedocument.Bookmarks("txtDestDepartment").Range;
                range.text = document.Form1.txtDestDepartment.value;
                range = wordapp.activedocument.Bookmarks("txtDestPerson").Range;
                range.text = document.Form1.txtDestPerson.value;
                range = wordapp.activedocument.Bookmarks("txtTel").Range;
                range.text = document.Form1.txtTel.value;
                range = wordapp.activedocument.Bookmarks("txtDesignSender").Range;
                range.text = document.Form1.txtDesignSender.value;
                range = wordapp.activedocument.Bookmarks("txtDesignSendTime").Range;
                range.text = document.Form1.txtDesignSendTime.value;
                range = wordapp.activedocument.Bookmarks("txtProfViceGM").Range;
                range.text = document.Form1.txtProfViceGM.value;
                range = wordapp.activedocument.Bookmarks("txtRegistDate").Range;
                range.text = document.Form1.txtRegistDate.value;
                range = wordapp.activedocument.Bookmarks("txtPublisher").Range;
                range.text = document.Form1.txtPublisher.value;
                range = wordapp.activedocument.Bookmarks("txtPublishTime").Range;
                range.text = document.Form1.txtPublishTime.value;
                range = wordapp.activedocument.Bookmarks("txtFileSender").Range;
                range.text = document.Form1.txtFileSender.value;
                range = wordapp.activedocument.Bookmarks("txtFileSendTime").Range;
                range.text = document.Form1.txtFileSendTime.value;
                range = wordapp.activedocument.Bookmarks("areaOther").Range;
                range.text = document.Form1.areaOther.value;
                range = wordapp.activedocument.Bookmarks("txtPublishRoom").Range;
                range.text = document.Form1.txtPublishRoom.value;
                range = wordapp.activedocument.Bookmarks("txtFillTime").Range;
                range.text = document.Form1.txtFillTime.value;
                /*---该方法可以导出表格,但是格式跟网页上一样(开始)---
                range = wordapp.activedocument.Bookmarks("tbBudget").Range;

                var sel = document.body.createTextRange();

                sel.moveToElementText(document.getElementById("tbRecord")); //此处tbShow是页面tbShow的id

                sel.select();

                sel.execCommand("Copy");

                range.Paste();
                //                wordapp.application.Visible = true;
                --(结束)---*/

                /*---该方法针对word文件的table所有的单元格都有名字(模版中只设定了10行)(开始)---*/
                var valList = document.getElementById("hidValList").value;
                if (valList != "") {
                    var jsonArray = eval(valList);
                    for (var i = 0; i < jsonArray.length; i++) {
                        range = wordapp.activedocument.Bookmarks("txtBudgetName" + i).Range;
                        range.text = jsonArray[i].BudgetName;
                        range = wordapp.activedocument.Bookmarks("txtProjectInvestSum" + i).Range;
                        range.text = jsonArray[i].ProjectInvestSum;
                        range = wordapp.activedocument.Bookmarks("txtDesignFee" + i).Range;
                        range.text = jsonArray[i].DesignFee;
                        range = wordapp.activedocument.Bookmarks("txtChangeProjectInvestSum" + i).Range;
                        range.text = jsonArray[i].ChangeProjectInvestSum;
                        range = wordapp.activedocument.Bookmarks("txtChangeDesignFee" + i).Range;
                        range.text = jsonArray[i].ChangeDesignFee;
                        range = wordapp.activedocument.Bookmarks("txtDesigner" + i).Range;
                        range.text = jsonArray[i].Designer;
                        range = wordapp.activedocument.Bookmarks("selRemark" + i).Range;
                        range.text = jsonArray[i].Remark;
                    }
                }
                /*---(结束)---*/

 


                var objPath = new ActiveXObject("Shell.Application").BrowseForFolder(0, "请选择要导出的路径", 0, "");
                var localpath = "";
                if (objPath != null && objPath.Items().Item() != null) {
                    localpath = objPath.Items().Item().Path;
                }
                else {
                    return false;
                }
                localpath += "/SJZ-4.2-06设计文件出版交接记录表.doc";
                wordapp.application.activedocument.SaveAs(localpath, true);
                alert("导出成功!");
                wordapp.activedocument.close(0);
            }
            catch (e) {
                alert(e.description);
            }
            finally {
                //                wordapp.activedocument.close();
                //                wordapp.activedocument.printout();
                //                //关闭word文档
                //                wordapp.Templates.Application.ActiveDocument.close();
                //关闭word应用程序
                //                wordapp = null;
                //                wordapp.quit();
                if (wordapp != null) {
                    wordapp.quit();
                }
                wordapp.application.quit();

            }
        }

posted @ 2013-05-20 23:50  Trilyn  阅读(299)  评论(0)    收藏  举报