public class WordUtility
{
private _Application wordApp = null;
private _Document wordDoc = null;
public _Application Application
{
get
{
return wordApp;
}
set
{
wordApp = value;
}
}
public _Document Document
{
get
{
return wordDoc;
}
set
{
wordDoc = value;
}
}
public void CreateNewDocument(string filePath)
{
killWinWordProcess();
wordApp = new Application();
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;
object missing = System.Reflection.Missing.Value;
object templateName = filePath;
wordDoc = wordApp.Documents.Add(ref templateName, ref missing,
ref missing, ref missing);
}
private void HideFile(String path)
{
try
{
FileAttributes fa = FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System;
File.SetAttributes(path, fa);
}
catch
{
}
}
public void SaveDocument(string filePath)
{
object fileName = filePath;
object format = WdSaveFormat.wdFormatDocument;
object miss = System.Reflection.Missing.Value;
wordDoc.SaveAs(ref fileName, ref format, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
public Bookmarks GetDocBookmarks()
{
return wordApp.ActiveDocument.Bookmarks;
}
public void SetText(object markName, String text)
{
try
{
Bookmark mark = wordDoc.Bookmarks.get_Item(ref markName);
if (mark != null)
{
mark.Range.Text = text;
}
}
catch
{
}
}
public bool InsertValue(string bookmark, string value)
{
object bkObj = bookmark;
if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
{
wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
wordApp.Selection.TypeText(value);
return true;
}
return false;
}
public Table InsertTable(string bookmark, int rows, int columns, float width)
{
object miss = System.Reflection.Missing.Value;
object oStart = bookmark;
Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
Table newTable = wordDoc.Tables.Add(range, rows, columns, ref miss, ref miss);
newTable.Borders.Enable = 1;
newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;
if (width != 0)
{
newTable.PreferredWidth = width;
}
newTable.AllowPageBreaks = false;
return newTable;
}
public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2)
{
table.Cell(row1, column1).Merge(table.Cell(row2, column2));
}
public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align, int Vertical)
{
switch (Align)
{
case -1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break;
case 0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break;
case 1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break;
}
switch (Vertical)
{
case -1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break;
case 0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break;
case 1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break;
}
}
public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size)
{
if (size != 0)
{
table.Range.Font.Size = Convert.ToSingle(size);
}
if (fontName != "")
{
table.Range.Font.Name = fontName;
}
}
public void UseBorder(int n, bool use)
{
if (use)
{
wordDoc.Content.Tables[n].Borders.Enable = 1;
}
else
{
wordDoc.Content.Tables[n].Borders.Enable = 2;
}
}
public void AddRow(int n)
{
object miss = System.Reflection.Missing.Value;
wordDoc.Content.Tables[n].Rows.Add(ref miss);
}
public void AddRow(Microsoft.Office.Interop.Word.Table table)
{
object miss = System.Reflection.Missing.Value;
table.Rows.Add(ref miss);
}
public void AddRow(int n, int rows)
{
object miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
for (int i = 0; i < rows; i++)
{
table.Rows.Add(ref miss);
}
}
public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string value)
{
table.Cell(row, column).Range.Text = value;
}
public void InsertCell(int n, int row, int column, string value)
{
wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;
}
public void InsertCell(int n, int row, int columns, string[] values)
{
Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
for (int i = 0; i < columns; i++)
{
table.Cell(row, i + 1).Range.Text = values[i];
}
}
public void InsertPicture(string bookmark, string picturePath, float width, float hight)
{
object miss = System.Reflection.Missing.Value;
object oStart = bookmark;
Object linkToFile = false;
Object saveWithDocument = true;
object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width;
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight;
}
public void InsertPicture(string bookmark, string picturePath)
{
object miss = System.Reflection.Missing.Value;
object oStart = bookmark;
Object linkToFile = false;
Object saveWithDocument = true;
object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
wordDoc.Bookmarks.get_Item(ref oStart).Select();
InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture(picturePath,
ref linkToFile, ref saveWithDocument, ref range);
if (bookmark == "局部图像")
{
inlineShape.Width = 180;
inlineShape.Height = 180;
}
else if (bookmark == "位置略图")
{
inlineShape.Width = 180;
inlineShape.Height = 180;
}
else
{
inlineShape.Width = 180;
inlineShape.Height = 120;
}
}
public void InsertText(string bookmark, string text)
{
object oStart = bookmark;
object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range);
wp.Format.SpaceBefore = 6;
wp.Range.Text = text;
wp.Format.SpaceAfter = 24;
wp.Range.InsertParagraphAfter();
wordDoc.Paragraphs.Last.Range.Text = "\n";
}
public void killWinWordProcess()
{
try
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (System.Diagnostics.Process process in processes)
{
bool b = process.MainWindowTitle == "";
if (process.MainWindowTitle == "")
{
process.Kill();
}
}
}
catch (Exception ex)
{
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义