📄 myword.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using Word;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Data;
using System.Configuration;
using System.IO;
public class MyWord
{
//public static void MessageBox(int h, string m, string c, int type)
//{ }
Word.ApplicationClass app = new Word.ApplicationClass();
object optional = Missing.Value;
object visible = true;
object saveChanges = true;
object NOTsaveChanges = false;
object docreadonly = true;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
_Document doc = null;
// FillCtl f=new FillCtl ();
private string dotpath;
public string DotPath
{
get
{
return dotpath;
}
set
{
dotpath = value;
}
}
public Word._Document Doc
{
get
{
return doc;
}
}
private string GetData(string str)
{
if (str == null)
str = "";
else
str = str.Trim();
return str;
}
//******打开文件*******
#region ******打开文件*******
public bool OpenFile()
{
try
{
if (File.Exists(dotpath) == true)
{
object fileName = dotpath;
doc = app.Documents.Open(ref fileName, ref optional,
ref docreadonly, ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional, ref visible,
ref optional, ref optional, ref optional, ref optional);
return true;
}
return false;
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
}
#endregion
public Word.Tables GetTablesInDoc()
{
return doc.Tables;
}
public void WriteData(object[] marks,string[]values)
{
int i = 0;
foreach(object str in marks)
{
object o = str;
WriteData(ref o, values[i++]);
}
}
public void WriteData(string strMark, string txt)
{//向标签lbl中写入数据txt
try
{
object lbl = (object)strMark;
doc.Bookmarks.get_Item(ref lbl).Select();
doc.Application.Selection.TypeText(txt.Trim());
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
throw new Exception(e.Message);
}
}
//******写入数据*******
#region ******写入数据*******
public void WriteData(ref object lbl, string txt)
{//向标签lbl中写入数据txt
try
{
doc.Bookmarks.get_Item(ref lbl).Select();
doc.Application.Selection.TypeText(txt.Trim());
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
throw new Exception(e.Message);
}
}
public void WriteData(DataTable dt)
{//将DataTable的第一行数据顺序写入文档的标签
try
{
if (dt.Rows.Count >= 1)
{
object[] lbl = GetMark();
if (lbl.Length == dt.Columns.Count)
for (int i = 0; i < lbl.Length; i++)
WriteData(ref lbl[i], dt.Rows[0][i].ToString());
}
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
throw new Exception(e.Message);
}
}
#endregion
public string GetTextInTables(int tableIndex,int rowIndex,int colIndex)
{
Word.Tables tables = doc.Tables;
string temp = tables[tableIndex].Rows[rowIndex].Cells[colIndex].Range.Text.Trim();
return temp.Substring(0, temp.LastIndexOf('\a'));
}
public string GetTextInTables(int tableIndex, int CellIndex)
{
Word.Tables tables = doc.Tables;
string temp = tables[tableIndex].Range.Cells[CellIndex].Range.Text.Trim();
return temp.Substring(0, temp.LastIndexOf('\a'));
}
//******向表格写入数据*******
#region ******向表格写入数据*******
/**//// <summary>
///
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="beginRow">开始写入的起始行</param>
/// <param name="Cols">填充的列数</param>
/// <param name="TablesItem">表格在word中的序号,从1开始</param>
/// <returns></returns>
public bool FillTable(DataTable dt, int beginRow, int Cols, int TablesItem)
{
try
{
object row = 1;
//TablesItem = app.Application.ActiveDocument.Tables.Count;
int xb = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
app.Application.ActiveDocument.Tables[TablesItem].Cell(beginRow + i, 1).Select();
app.Application.Selection.Text = GetData(dt.Rows[xb][0].ToString());
for (int j = 2; j <= Cols; j++)
{
app.Application.ActiveDocument.Tables[TablesItem].Cell(beginRow + i, j).Select();
app.Application.Selection.Text = GetData(dt.Rows[xb][j - 1].ToString());
}
if (!i.Equals(dt.Rows.Count - 1))
{
app.Application.Selection.InsertRowsBelow(ref row);
}
xb++;
}
return true;
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
//MessageBox(0, e.Message + "\r\n" + e.StackTrace, "Error Message Box", 0);
return false;
}
}
#endregion
//*******将图片写入WORd中********
#region *******将图片写入WORd中********
public void FillImage(ref object lbl, string path)
{
try
{
doc.Bookmarks.get_Item(ref lbl).Select();
doc.Application.Selection.InlineShapes.AddPicture(path, ref originalFormat,
ref originalFormat, ref originalFormat);
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
throw new Exception(e.Message);
}
}
#endregion
// ******创建表格*******
#region ******创建表格*******
public bool CreateTable(int rownum, int colnum, string header)
{
try
{
char[] separtor1 = { ';' };
char[] separtor2 = { ',' };
string[] split1 = null;
string[] split2 = null;
split1 = header.Split(separtor1);
Word.Table table = doc.Tables.Add(app.Selection.Range, rownum, colnum,
ref optional, ref optional);
for (int i = 1; i <= split1.Length; i++)
{//添加每一行
split2 = split1[i - 1].Split(separtor2);
for (int j = 1; j <= split2.Length; j++)
{//添加每一列
table.Cell(i, j).Range.Text = split2[j - 1].Trim();
}
}
return true;
}
catch
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
//MessageBox(0, e.Message + "\r\n" + e.StackTrace, "Error Messagebox", 0);
return false;
}
}
public bool ContentTableAdd(DataTable dt)
{
try
{
app.Selection.TypeParagraph();
app.Selection.TypeParagraph();
app.Selection.TypeParagraph();
Word.Table table = doc.Tables.Add(app.Selection.Range, 4, 9,
ref optional, ref optional);
table.Cell(1, 1).Range.Text = dt.Rows[0]["content_id"].ToString();
return true;
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
//MessageBox(0, e.Message + "\r\n" + e.StackTrace, "Error Message Box", 0);
return false;
}
}
#endregion
//******删除表格*******
#region ******删除表格*******
public bool DelTable(int num)
{
try
{
doc.Tables[num].Delete();
return true;
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
//MessageBox(0, e.Message + "\r\n" + e.StackTrace, "Error Message Box", 0);
return false;
}
}
#endregion
//******文件另存为*******
#region ******文件另存为*******
public void SaveAs(ref object fileName)
{
try
{
doc.SaveAs(ref fileName, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional);
}
catch (System.Exception e)
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
throw new Exception(e.Message);
}
}
#endregion
//******关闭word*******
#region ******关闭word*******
public void Quit()
{
try
{
doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
}
public void AppQuit()
{
try
{
app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
}
catch (System.Exception e)
{
throw new Exception(e.Message);
}
}
#endregion
public object[] GetMark()
{
object j = null;
string str = "";
object[] result = new object[doc.Bookmarks.Count];
for (int i = 1; i <= doc.Bookmarks.Count; i++)
{
j = (object)i;
result[i - 1] = doc.Bookmarks.get_Item(ref j).Name;
str = str + doc.Bookmarks.get_Item(ref j).Name + ",";
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -