问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

如何在C#中使用OpenXml操作Word模版,向Bookmark中填充数据。_百度知 ...

发布网友 发布时间:2022-04-25 23:32

我来回答

2个回答

热心网友 时间:2022-04-08 21:04

对word的操作给你这个是把word打开 并把其内容 复制出来 你参考一下吧

//btnFindFile_Click(object sender, EventArgs e)中应该按照路径找章节编码,暂时没有实现;
private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic;
private Microsoft.Office.Interop.Word.Document oDoc;
object missing = System.Reflection.Missing.Value;
private List<string> SQlStrList = new List<string>();
private ArrayList arr = new ArrayList();
private string ZJBM = "";
private int C = 0;
public frm导入Word()
{
InitializeComponent();
}
public class objclass
{
public string _value;
public string _text;
public override string ToString()
{
return _text;
}
public objclass(string v, string t)
{
_text = t;
_value = v;
}
public override bool Equals(object obj)
{
return obj.ToString() == this._value;
}
}
private void frmKZQKDR_Load(object sender, EventArgs e)
{
this.cmbTX.SelectedIndex = 0;
基础类.buildTree.Bound难易程度(cmbNYCD);
cmbNYCD.SelectedIndex = -1;
基础类.buildTree.Bound题目类型(cmbTX);
}

private void btnFindFile_Click(object sender, EventArgs e)//将word内容导入到RichTextBox中
{
openFileDialog1.Filter = "(*.doc)|*.doc";
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
txtExcelFile.Text = openFileDialog1.FileName;
//打开word,拷贝内容至RTF
WordHelp();
Open(txtExcelFile.Text);
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();
richTextBox1.Clear();
richTextBox1.Paste();
Clipboard.Clear();
//避免弹出normal.dot被使用的对话框
oWordApplic.NormalTemplate.Saved = true;
oDoc.Close(ref missing, ref missing, ref missing);
Quit();
start = 0;
//从得到的路径获得当前章节编码(尚未实现)
ZJBM = "";
}
}

#region///word用函数
public Microsoft.Office.Interop.Word.ApplicationClass WordApplication
{
get { return oWordApplic; }
}
public void WordHelp()
{
// activate the interface with the COM object of Microsoft Word
oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
}
public void WordHelp(Microsoft.Office.Interop.Word.ApplicationClass wordapp)
{
oWordApplic = wordapp;
}
// Open a file (the file must exists) and activate it
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;

oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

oDoc.Activate();
}
// Open a new document
public void Open()
{
oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);

oDoc.Activate();
}
public void Quit()
{
oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
}
/// 打开Word文档,并且返回对象oDoc
/// 完整Word文件路径+名称
/// 返回的Word.Document oDoc对象
public Microsoft.Office.Interop.Word.Document CreateWordDocument(string FileName, bool HideWin)
{
if (FileName == "") return null;

oWordApplic.Visible = HideWin;
oWordApplic.Caption = "";
oWordApplic.Options.CheckSpellingAsYouType = false;
oWordApplic.Options.CheckGrammarAsYouType = false;

Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = false;
Object AddToRecentFiles = false;

Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
try
{
Microsoft.Office.Interop.Word.Document wordDoc = oWordApplic.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);
return wordDoc;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
public void SaveAs(Microsoft.Office.Interop.Word.Document oDoc, string strFileName)
{
object fileName = strFileName;
if (File.Exists(strFileName))
{
if (MessageBox.Show("文件'" + strFileName + "'已经存在,选确定覆盖原文件,选取消退出操作!", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
else
{
Clipboard.Clear();
}
}
else
{
oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
}
public void SaveAs(string strFileName)
{
object fileName = strFileName;

oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
#endregion
/// <summary>
/// 附加dot模版文件
/// </summary>
private void LoadDotFile(string strDotFile)
{
if (!string.IsNullOrEmpty(strDotFile))
{
Microsoft.Office.Interop.Word.Document wDot = null;
if (oWordApplic != null)
{
oDoc = oWordApplic.ActiveDocument;

oWordApplic.Selection.WholeStory();

string strContent = oWordApplic.Selection.Text;

oWordApplic.Selection.Copy();
wDot = CreateWordDocument(strDotFile, false);

//object bkmC = "Content";

//if (oWordApplic.ActiveDocument.Bookmarks.Exists("Content") == true)
//{
// oWordApplic.ActiveDocument.Bookmarks.get_Item
// (ref bkmC).Select();
//}

//对标签"Content"进行填充
//直接写入内容不能识别表格什么的
//oWordApplic.Selection.TypeText("123123123123123123");
//oWordApplic.Selection.Paste();
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();

oDoc.Activate();
oWordApplic.Selection.Paste();
wDot.Close(ref missing, ref missing, ref missing);
//给试卷加入内容

object bkmC = "Content1";

if (oWordApplic.ActiveDocument.Bookmarks.Exists("Content1") == true)
{
oWordApplic.ActiveDocument.Bookmarks.get_Item
(ref bkmC).Select();
}
//oWordApplic.Selection.TypeText("123123123123123123456456");
Clipboard.Clear();//清空剪切板

this.richTextBox1.SelectAll();
this.richTextBox1.Cut();
oDoc.Activate();
oWordApplic.Selection.Paste();

}
}
}

public int start = 0;

}

热心网友 时间:2022-04-08 22:22

private void FillBookmarksUsingOpenXml(string sourceDoc, string destDoc, Dictionary<string, string> bookmarkData) { string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; // Make a copy of the template file. File.Copy(sourceDoc, destDoc, true); //Open the document as an Open XML package and extract the main document part. using (WordprocessingDocument wordPackage = WordprocessingDocument.Open(destDoc, true)) { MainDocumentPart part = wordPackage.MainDocumentPart; //Setup the namespace manager so you can perform XPath queries //to search for bookmarks in the part. NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("w", wordmlNamespace); //Load the part's XML into an XmlDocument instance. XmlDocument xmlDoc = new XmlDocument(nt); xmlDoc.Load(part.GetStream()); //Iterate through the bookmarks. foreach (KeyValuePair<string, string> bookmarkDataVal in bookmarkData) { var bookmarks = from bm in part.Document.Body.Descendants<BookmarkStart>() select bm; foreach (var bookmark in bookmarks) { if (bookmark.Name == bookmarkDataVal.Key) { Run bookmarkText = bookmark.NextSibling<Run>(); if (bookmarkText != null) // if the bookmark has text replace it { bookmarkText.GetFirstChild<Text>().Text = bookmarkDataVal.Value; } else // otherwise append new text immediately after it { var parent = bookmark.Parent; // bookmark's parent element Text text = new Text(bookmarkDataVal.Value); Run run = new Run(new RunProperties()); run.Append(text); // insert after bookmark parent parent.Append(run); } //bk.Remove(); // we don't want the bookmark anymore } } } //Write the changes back to the document part. xmlDoc.Save(wordPackage.MainDocumentPart.GetStream(FileMode.Create)); } }
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
诺基亚5233直签工具 我的5233下了证书、也在塞班官网上在线签名了、怎么还是提示证实书错误... 你好 就是我用的诺基亚5233直签工具就是用不了什么受证书限制内部错误... 请问哪里有做餐用包装纸巾的.厂 怎么在餐巾纸上或者纸巾包装上打广告? 广告餐巾纸广告餐巾纸的分类 广告餐巾纸广告餐巾纸的制作形式 ...支付密码一样呢?支付密码和银行卡的关系是什么?求解答, ...今天是怎么了,换了数据线也不行,同事的M1也一样不能充电... 小米M1和小米青春版就是CPU和内存不一样,我是学生,买哪一个比较好? 手机怎么更新word目录 word 自动生成的目录修改后怎么更新? word之如何更新索引、目录 - 信息提示 银行卖保险是*允许的吗 上套的顾客多吗 尽是年龄大的顾客多哪 还是年轻的多哪 2019年保险监管环境 银行卖保险是政府允许的吗 上套的顾客多吗 尽是什么样的顾客哪 商业银行开展代理保险业务时,应当遵守监管机构哪些方面的规定 保监会 客户真实性 银行保险业处于不同客户生命周期阶段的客户需求有何不同 银行和保险业为什么要审慎监管 而证券行业主要是关注与客户间商业行为的监管 银行保险监管的模式有哪些? 急!!! 你们喜欢moco这个女装牌子吗?质量款式等有什么想说的 什么是反抽,反弹还是反转 外汇做空时怎样判断是反弹还是逆转? 摩安珂女装品牌介绍是什么? 反转和回调的区别图解? 如何辨别上涨是反抽,反弹还是反转 在股票里,反转与反弹的区别 股票的反弹和反转是一个意思吗 如何将WordOpenXML属性转换成System.IO.Packaging.Package 书法中的笔墨纸砚,有何讲究? css 文字图片不能居中 图片文字间距无法消除问题 求助 求用文字描述这个图片中的场景 详细点吧 海信5G阅读手机A7评测:长短作新语,墨纸似鸦浓 梦幻笔墨纸砚怎么得的? 打印条码机墨纸安装布骤 根据一张照片来写出其中的场面和环境描写 笔墨纸砚桌上摆,梅兰竹菊院中载,诗情画意勾思中,扬扬洒洒诗句来是诗句吗? 条码打印机总断墨纸怎么办 如何用x光的辐射强度转换出物体图像 一个去杭州有什么好玩的计划吗 谁知道柠檬蜜茶怎么做啊? 感冒了怎么办,嗓子还疼,呼吸道疼 柠檬和蜂蜜水一起喝好不好 蜂蜜姜和柠檬可以一起喝吗?是早上空腹喝还是晚上喝 柠檬蜜茶和柠檬蜜是一码事吗! 长期空腹喝柠檬蜜茶有什么功效,会有负作用吗? 新华惠天福满五年可以取出来吗 新华惠添富年金保险15年期