C#操作Word之2003版处理简析,带制图功能:

成都创新互联2013年至今,是专业互联网技术服务公司,拥有项目网站设计、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元项城做网站,已为上家服务,为项城各地企业和个人服务,联系电话:028-86922220
- using System;
 - using System.IO;
 - using System.Data;
 - using System.Collections.Generic;
 - using System.Text;
 - using System.Reflection;
 - using Microsoft.Office.Core;
 - using Word = Microsoft.Office.Interop.Word;
 - using Graph = Microsoft.Office.Interop.Graph;
 - //C#操作Word之2003版
 - namespace NWord
 - ...{
 - /**////
 - /// 功能描述:操作word文件
 - /// 作者:--
 - /// 使用说明:在工程中添加Word 11.0对象库的引用。该模块在office2003基础上开发。
 - ///
 - public class C_Word
 - ...{
 - Variables#region Variables
 - private string strwordfilename;//文件名
 - private string strwordfilepath;//文件路径
 - private bool wordvisible = false;//Word文件操作是否可见
 - Word._Application WordApp = new Word.Application();
 - Word._Document WordDoc;
 - object missing = System.Reflection.Missing.Value;
 - object oEndOfDoc = "\endofdoc";
 - #endregion
 - Properties#region Properties
 - /**////
 - /// word文件名 ,C#操作Word之2003版
 - ///
 - public string WordFileName
 - ...{
 - get ...{ return strwordfilename; }
 - }
 - /**////
 - /// word文件路径
 - ///
 - public string WordFilePath
 - ...{
 - get ...{ return strwordfilepath; }
 - }
 - #endregion
 - /**////
 - /// 构造word对象
 - ///
 - public C_Word() ...{ }
 - /**////
 - /// 构造word对象 ,C#操作Word之2003版
 - ///
 - /// word文件全路径
 - public C_Word(string strfullfilepath)
 - ...{
 - WordApp.Visible = false;
 - strwordfilename = Path.GetFileName(strfullfilepath);
 - strwordfilepath = Path.GetDirectoryName(strfullfilepath);
 - CreateWordFile(strfullfilepath);
 - }
 - /**////
 - /// 构造word对象
 - ///
 - /// word文件全路径
 - /// 是否覆盖现有word文件
 - public C_Word(string strfullfilepath, bool overwrite)
 - ...{
 - strwordfilename = Path.GetFileName(strfullfilepath);
 - strwordfilepath = Path.GetDirectoryName(strfullfilepath);
 - WordApp.Visible = wordvisible;
 - if (overwrite || !File.Exists(strfullfilepath))
 - ...{
 - CreateWordFile(strfullfilepath);
 - }
 - else
 - ...{
 - this.Open(strfullfilepath);
 - }
 - }
 - /**////
 - /// 打开word文件
 - ///
 - /// 文件路径
 - public void Open(string strfilepath)
 - ...{
 - object filepath = strfilepath;
 - object wrdvisible = wordvisible;
 - //WordApp.Documents.Add(ref filepath, ref missing,
 - ref missing, ref wrdvisible);
 - //C#操作Word之2003版
 - WordApp.Documents.Open(ref filepath, ref missing,
 - ref missing, ref missing, ref missing
 - , ref missing, ref missing, ref missing, ref missing
 - , ref missing, ref missing, ref wrdvisible, ref missing
 - , ref missing, ref missing, ref missing);
 - WordDoc = WordApp.Documents.Open(ref filepath,
 - ref missing, ref wrdvisible, ref missing,
 - ref missing, ref missing, ref missing, ref missing,
 - ref missing, ref missing, ref missing, ref wrdvisible,
 - ref missing, ref missing, ref missing, ref missing);
 - }
 - /**////
 - /// 新建word文件
 - ///
 - /// word文件路径
 - private void CreateWordFile(string strfullfilepath)
 - ...{
 - object ofilename = strfullfilepath;
 - WordDoc = WordApp.Documents.Add(ref missing,
 - ref missing, ref missing, ref missing);
 - //验证路径,C#操作Word之2003版
 - if (!Directory.Exists(Path.GetDirectoryName(
 - strfullfilepath))) return;
 - if (Path.GetExtension(strfullfilepath).
 - ToLower() != ".doc") return;
 - try
 - ...{
 - if (File.Exists(strfullfilepath)) File.Delete(strfullfilepath);
 - WordApp.ActiveDocument.SaveAs(ref ofilename,
 - 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);
 - return;
 - }
 - catch
 - ...{
 - return;
 - }
 - }
 - /**////
 - /// 从模版创建word文件 ,C#操作Word之2003版
 - ///
 - /// word模版路径
 - /// word文件路径
 - public void CreateFileFromDot(string strdotpath,string strdocpath)
 - ...{
 - object filepath = strdotpath;
 - object owordfile = strdocpath;
 - if (File.Exists(strdocpath))//删除已存在的文件
 - ...{
 - File.Delete(strdocpath);
 - }
 - WordApp.Documents.Add(ref filepath, ref missing, ref missing, ref missing);
 - WordApp.ActiveDocument.SaveAs(ref owordfile,
 - 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);
 - this.Open(strdocpath);
 - strwordfilepath = Path.GetDirectoryName(strdotpath);
 - strwordfilename = Path.GetFileName(strdocpath);
 - }
 - //C#操作Word之2003版
 - /**////
 - /// 向word结尾插入1行数据,不会换行
 - ///
 - /// 插入的字符串
 - public void WriteLine(string strline)
 - ...{
 - object fileName = WordFilePath + WordFileName;
 - object bvisible = true;
 - Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
 - wrdRng.InsertAfter(strline);
 - this.Save();
 - }
 - /**////
 - /// 将书签更新成字符串
 - ///
 - /// 书签名
 - /// 更新字符串
 - public void UpdateBookmark(string bookmarkName, string newText)
 - ...{
 - object name = bookmarkName;
 - Word.Range rng = WordApp.ActiveDocument.Bookmarks.
 - get_Item(ref name).Range;
 - rng.Text = newText;
 - object range = rng;
 - WordApp.ActiveDocument.Bookmarks.Add(bookmarkName, ref range);
 - this.Save();
 - }
 - /**////
 - /// 将书签更新成字符串,C#操作Word之2003版
 - ///
 - /// word书签
 - /// 更新字符串
 - public void UpdateBookmark(Word.Bookmark bookmark, string newText)
 - ...{
 - object rng = bookmark.Range;
 - string bookmarkName = bookmark.Name;
 - bookmark.Range.Text = newText;
 - WordApp.ActiveDocument.Bookmarks.Add(bookmarkName, ref rng);
 - this.Save();
 - }
 - /**////
 - /// 更改某表的某个单元格的内容
 - ///
 - /// table id
 - /// 行id
 - /// 列id
 - /// 更新字符串
 - public void UpdateTableContent(int tableID,
 - int lineID, int columnID, string context)
 - ...{
 - Word.Table tbl = WordApp.ActiveDocument.Tables[tableID];
 - tbl.Cell(lineID, columnID).Range.Text = context;
 - this.Save();
 - }
 - /**////
 - /// 插入图表 ,C#操作Word之2003版
 - ///
 - /// 书签名
 - /// 图表数据源datatable
 - /// 图表格式
 - public void InsertChart(string strbookmark,
 - DataTable dtsheet,Graph.XlChartType xlcharttype)
 - ...{
 - int i, j;
 - Graph.Chart wrdChart;
 - Graph.Axis axis;
 - object oClassType = "MSGraph.Chart.8";
 - object bookmark = strbookmark;
 - //在指定的书签位置插入图表
 - Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref bookmark).Range;
 - //初始化一张图表
 - wrdChart = (Graph.Chart)wrdRng.InlineShapes.
 - AddOLEObject(ref oClassType, ref missing,
 - ref missing, ref missing, ref missing,
 - ref missing, ref missing, ref missing).OLEFormat.Object;
 - //wrdChart.Application.Visible = false;
 - wrdChart.Application.PlotBy = Graph.XlRowCol.xlColumns;
 - //根据Y轴来画图表
 - //改变图表格式
 - wrdChart.ChartType = xlcharttype;
 - axis = (Graph.Axis)wrdChart.Axes(1, 1);//设置X轴的属性
 - wrdChart.Application.DataSheet.Cells.Clear();
 - //清空表格的初始数据
 - //填充图表,起始的行号和列号都是1
 - for (i = 0; i < dtsheet.Columns.Count; i++)//初始化列名
 - ...{
 - wrdChart.Application.DataSheet.Cells[1, i + 1] =
 - dtsheet.Columns[i].ColumnName;
 - }
 - for (i = 0; i < dtsheet.Rows.Count; i++)//填充数据
 - ...{
 - for (j = 0; j < dtsheet.Columns.Count; j++)
 - ...{
 - wrdChart.Application.DataSheet.Cells[i + 2, j + 1] =
 - dtsheet.Rows[i][j].ToString().Replace("9999999", "100ys");
 - }
 - }
 - //axis.MaximumScale = 1;//X轴最大刻度
 - //axis.MajorUnit = 0.1;
 - //C#操作Word之2003版
 - wrdChart.Legend.Delete();
 - wrdChart.Width = 500;
 - //wrdChart.Height = 666;
 - //oShape.Height = oWord.InchesToPoints(3.57f);
 - //更新图表并保存退出
 - wrdChart.Application.Update();
 - wrdChart.Application.Quit();
 - this.Save();
 - }
 - /**////
 - /// 清空文档
 - ///
 - public void Clear()
 - ...{
 - object Unit = (int)Word.WdUnits.wdCharacter;
 - object Count = 1;
 - WordApp.Selection.WholeStory();
 - WordApp.Selection.Delete(ref Unit, ref Count);
 - this.Save();
 - }
 - /**////
 - /// 另存为
 - ///
 - /// 目标文件路径
 - public void SaveAs(string strFileName)
 - ...{
 - object fileName = strFileName;
 - WordApp.ActiveDocument.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 GotoBookMark(string strBookMarkName)
 - ...{
 - object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
 - object NameBookMark = strBookMarkName;
 - WordApp.Selection.GoTo(ref Bookmark,
 - ref missing, ref missing, ref NameBookMark);
 - }
 - /**////
 - /// 删除指定的文本
 - ///
 - /// 被删除的文本
 - public void DeleteText(string text)
 - ...{
 - object findText = text;
 - object replaceWith = "";
 - object replaceAll = Word.WdReplace.wdReplaceAll;
 - this.WordApp.Selection.Find.ClearFormatting();
 - this.WordApp.Selection.Find.Replacement.ClearFormatting();
 - this.WordApp.Selection.Find.Replacement.Text = "";
 - this.WordApp.Selection.Find.Execute(ref findText,
 - ref missing, ref missing, ref missing,
 - ref missing, ref missing, ref missing,
 - ref missing, ref missing, ref replaceWith,
 - ref replaceAll, ref missing, ref missing,
 - ref missing, ref missing);
 - object unit = (int)Word.WdUnits.wdCharacter;
 - object count = 1;
 - this.WordApp.Selection.Delete(ref unit, ref count);
 - this.Save();
 - }
 - /**////
 - /// 保存当前word文档
 - ///
 - private void Save()
 - ...{
 - WordApp.ActiveDocument.Save();
 - }
 - /**////
 - /// 关闭Word文件,释放对象;C#操作Word之2003版
 - ///最后一定要调用此函数,否则会引起异常
 - ///
 - public void Close()
 - ...{
 - try
 - ...{
 - WordApp.Application.Quit(ref missing,
 - ref missing, ref missing);
 - if (WordApp != null)
 - ...{
 - WordApp = null;
 - }
 - }
 - catch
 - ...{ }
 - finally
 - ...{
 - GC.Collect();
 - GC.WaitForPendingFinalizers();
 - GC.Collect();
 - GC.WaitForPendingFinalizers();
 - }
 - }
 - }
 - }
 
C#操作Word之2003版的相关处理就向你介绍到这里,希望对你了解和学习C#操作Word有所帮助。