C#操作Word之2003版處理簡析
作者:TTNC_Lena 
  C#操作Word之2003版處理簡析主要向你介紹了C#操作Word對于2003版的處理實例應(yīng)用,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。
 C#操作Word之2003版處理簡析,帶制圖功能:
- 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
 - ...{
 - /**//// <summary>
 - /// 功能描述:操作word文件
 - /// 作者:--
 - /// 使用說明:在工程中添加Word 11.0對象庫的引用。該模塊在office2003基礎(chǔ)上開發(fā)。
 - /// </summary>
 - 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
 - /**//// <summary>
 - /// word文件名 ,C#操作Word之2003版
 - /// </summary>
 - public string WordFileName
 - ...{
 - get ...{ return strwordfilename; }
 - }
 - /**//// <summary>
 - /// word文件路徑
 - /// </summary>
 - public string WordFilePath
 - ...{
 - get ...{ return strwordfilepath; }
 - }
 - #endregion
 - /**//// <summary>
 - /// 構(gòu)造word對象
 - /// </summary>
 - public C_Word() ...{ }
 - /**//// <summary>
 - /// 構(gòu)造word對象 ,C#操作Word之2003版
 - /// </summary>
 - /// <param name="strfullfilepath">word文件全路徑</param>
 - public C_Word(string strfullfilepath)
 - ...{
 - WordApp.Visible = false;
 - strwordfilename = Path.GetFileName(strfullfilepath);
 - strwordfilepath = Path.GetDirectoryName(strfullfilepath);
 - CreateWordFile(strfullfilepath);
 - }
 - /**//// <summary>
 - /// 構(gòu)造word對象
 - /// </summary>
 - /// <param name="strfullfilepath">word文件全路徑</param>
 - /// <param name="overwrite">是否覆蓋現(xiàn)有word文件</param>
 - 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);
 - }
 - }
 - /**//// <summary>
 - /// 打開word文件
 - /// </summary>
 - /// <param name="strfilepath">文件路徑</param>
 - 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);
 - }
 - /**//// <summary>
 - /// 新建word文件
 - /// </summary>
 - /// <param name="strfullfilepath">word文件路徑</param>
 - 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;
 - }
 - }
 - /**//// <summary>
 - /// 從模版創(chuàng)建word文件 ,C#操作Word之2003版
 - /// </summary>
 - /// <param name="strdotpath">word模版路徑</param>
 - /// <param name="strdocpath">word文件路徑</param>
 - 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版
 - /**//// <summary>
 - /// 向word結(jié)尾插入1行數(shù)據(jù),不會換行
 - /// </summary>
 - /// <param name="strline">插入的字符串</param>
 - 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();
 - }
 - /**//// <summary>
 - /// 將書簽更新成字符串
 - /// </summary>
 - /// <param name="bookmarkName">書簽名</param>
 - /// <param name="newText">更新字符串</param>
 - 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();
 - }
 - /**//// <summary>
 - /// 將書簽更新成字符串,C#操作Word之2003版
 - /// </summary>
 - /// <param name="bookmark">word書簽</param>
 - /// <param name="newText">更新字符串</param>
 - 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();
 - }
 - /**//// <summary>
 - /// 更改某表的某個單元格的內(nèi)容
 - /// </summary>
 - /// <param name="tableID">table id</param>
 - /// <param name="lineID">行id</param>
 - /// <param name="columnID">列id</param>
 - /// <param name="context">更新字符串</param>
 - 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();
 - }
 - /**//// <summary>
 - /// 插入圖表 ,C#操作Word之2003版
 - /// </summary>
 - /// <param name="strbookmark">書簽名</param>
 - /// <param name="dtsheet">圖表數(shù)據(jù)源datatable</param>
 - /// <param name="xlcharttype">圖表格式</param>
 - 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;
 - //根據(jù)Y軸來畫圖表
 - //改變圖表格式
 - wrdChart.ChartType = xlcharttype;
 - axis = (Graph.Axis)wrdChart.Axes(1, 1);//設(shè)置X軸的屬性
 - wrdChart.Application.DataSheet.Cells.Clear();
 - //清空表格的初始數(shù)據(jù)
 - //填充圖表,起始的行號和列號都是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++)//填充數(shù)據(jù)
 - ...{
 - 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();
 - }
 - /**//// <summary>
 - /// 清空文檔
 - /// </summary>
 - public void Clear()
 - ...{
 - object Unit = (int)Word.WdUnits.wdCharacter;
 - object Count = 1;
 - WordApp.Selection.WholeStory();
 - WordApp.Selection.Delete(ref Unit, ref Count);
 - this.Save();
 - }
 - /**//// <summary>
 - /// 另存為
 - /// </summary>
 - /// <param name="strFileName">目標(biāo)文件路徑</param>
 - 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);
 - }
 - /**//// <summary>
 - /// 轉(zhuǎn)到指定的標(biāo)簽處
 - /// </summary>
 - /// <param name="strBookMarkName">標(biāo)簽名</param>
 - 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);
 - }
 - /**//// <summary>
 - /// 刪除指定的文本
 - /// </summary>
 - /// <param name="text">被刪除的文本</param>
 - 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();
 - }
 - /**//// <summary>
 - /// 保存當(dāng)前word文檔
 - /// </summary>
 - private void Save()
 - ...{
 - WordApp.ActiveDocument.Save();
 - }
 - /**//// <summary>
 - /// 關(guān)閉Word文件,釋放對象;C#操作Word之2003版
 - ///最后一定要調(diào)用此函數(shù),否則會引起異常
 - /// </summary>
 - 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版的相關(guān)處理就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡 
                    來源:
                    CSDN博客
 














 
 
 
 
 
 
 