关于asp.net 操作 Word 剪贴板 的问题
有谁遇到过类似的问题:
两个word文档之间进行copy、paste操作时出现以下错误:
此方法或属性无效,因为剪贴板是空的或无效的!
生成服务合同的时候,合同附件有一个服务内容描述,这是需要根据配置不同的产品服务内容都不一样。
从对应的字模板中读出内容显示在合同中。
生成服务合同我用的是Office的COM组件,从子模板中读内容到服务合同,我就用了复制方法。
Range.Copy();
object Nothing = System.Reflection.Missing.Value; Word.Application attachApp = new Application(); object type = WdBreakType.wdSectionBreakContinuous; Word.Document attachDoc = attachApp.Documents.Open(ref docFile, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); attachDoc.Select(); attachApp.Selection.WholeStory(); attachDoc.Sections[1].Range.Copy(); object attach_ServiceContent = "attach_ServiceContent"; doc.Bookmarks.get_Item(ref attach_ServiceContent).Range.InsertBreak(ref type); doc.Bookmarks.get_Item(ref attach_ServiceContent).Range.PasteAndFormat(WdRecoveryType.wdPasteDefault); attachApp.NormalTemplate.Saved = true; object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges; attachDoc.Close(ref doNotSaveChanges, ref Nothing, ref Nothing); attachApp.Quit(ref doNotSaveChanges, ref Nothing, ref Nothing);
object clipboardData = Clipboard.GetData("userEntites");this.btnPaste.Enabled = (clipboardData != null); private void btnCopy_Click(object sender, EventArgs e) { // 读取数据 List<BaseUserEntity> userEntites = new List<BaseUserEntity>(); for (int i=0; i<this.DTUser.Rows.Count; i++) { BaseUserEntity userEntity = new BaseUserEntity(this.DTUser.Rows[i]); userEntites.Add(userEntity); } // 复制到剪切板 Clipboard.SetData("userEntites", userEntites); this.btnPaste.Enabled = true; } private void btnPaste_Click(object sender, EventArgs e) { object clipboardData = Clipboard.GetData("userEntites"); if (clipboardData != null) { List<BaseUserEntity> userEntites = (List<BaseUserEntity>)clipboardData; string[] addUserIds = new string[userEntites.Count]; for (int i = 0; i < userEntites.Count; i++) { addUserIds[i] = userEntites[i].Id.ToString(); } // 添加用户到角色 ServiceManager.Instance.RoleService.AddUserToRole(this.UserInfo, this.TargetRoleId, addUserIds); // 加载窗体 this.FormOnLoad(); // 设置按钮状态 this.SetControlState(); } }
[解决办法]
用富文本框把word中数据插入数据库.
然后用label读取出来.效果和原本一样.