如何在C#中使用OpenXml *** 作Word模版,向Bookmark中填充数据。

如何在C#中使用OpenXml *** 作Word模版,向Bookmark中填充数据。,第1张

对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()

//避免d出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

}

OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势:

1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩溃的问题.

2.对比填满一张30多页的WORD来说(包含图,表等),用COM组件来生成会占用20秒,Openxml1秒.

3.MS Word软件太贵了,你的客户装的是开源WORD,如LibreOffice,OpenOffice.这样你就只能用Openxml生成的WORD文档,各种支持MS Word都能打开,避免客户机器上安装MS Word.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11374637.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存