using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Web;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Net;
namespace WindowsApplication1
{
public partial class Word : Form
{
public Word()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
object oFileName = @"C:\ddddddddd.doc";
object oReadOnly = false;
object oMissing = System.Reflection.Missing.Value;
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oWord;
Microsoft.Office.Interop.Word.Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;//只是为了方便观察
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//MessageBox.Show(oDoc.Tables.Count.ToString());
Table newTable = oDoc.Tables[1];
//设置表格样式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;
//填充表格内容
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
//删除行
newTable.Rows[newTable.Rows.Count].Delete();
//移动到下一页
Microsoft.Office.Interop.Word.WdGoToItem goPage = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
oWord.Selection.GoToNext(goPage);
//插入图片
string FileName = @"C:\dsdddd.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = oWord.Application.Selection.Range;
oWord.Selection.Document.Shapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Anchor);
oWord.Selection.TypeText("我们的村村sfsfsdfdddddddddd");
object filename=@"C:\eeeeee.doc";
//文件保存
oDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing);
oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
oWord.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void OpenWord_Click(object sender, EventArgs e)
{
Word.Application wordApp = new Word.ApplicationClass(); try
{
// Word程序可见
wordApp.Visible = true; object missing = System.Reflection.Missing.Value; object fileName = @"C:\a.doc"; // 打开Word文档
wordApp.Documents.Open(
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
); Word.Range range1, range2; //// 取得总页数
//int pageNumber = wordApp.ActiveDocument.ComputeStatistics(
// Word.WdStatistic.wdStatisticPages,
// ref missing
// ); // 跳转种类
object objWhat = Word.WdGoToItem.wdGoToPage;
// 跳转位置
object objWhich = Word.WdGoToDirection.wdGoToLast; // 转向最后一页
wordApp.Selection.GoTo(ref objWhat, ref objWhich, ref missing, ref missing); // Range取得
range1 = wordApp.Selection.Range;
range2 = wordApp.ActiveDocument.Range(ref missing, ref missing); object start = range1.Start;
object end = range2.End; // 删除最后一页
wordApp.ActiveDocument.Range(ref start, ref end).Delete(ref missing, ref missing); // 跳转至最前
objWhich = Word.WdGoToDirection.wdGoToFirst;
wordApp.Selection.GoTo(ref objWhat, ref objWhich, ref missing, ref missing); wordApp.ActiveDocument.Save(); wordApp.Quit(ref missing, ref missing, ref missing);
}
finally
{
if (null != wordApp)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
}
}
}
//自定义变量
private Microsoft.Office.Interop.Word.ApplicationClass WordApp = null; //word
private Microsoft.Office.Interop.Word.Document document = null;//document
private object missing = null;//缺省值
/// <summary>
/// 将光标移动至文件头部
/// </summary>
public void moveStart()
{
object story = Microsoft.Office.Interop.Word.WdUnits.wdStory;
WordApp.Selection.HomeKey(ref story, ref missing);//向上移到顶
}
//移动到下一页
private void movePage(int count)
{
for (int i = 0; i < count; i++)
{
Microsoft.Office.Interop.Word.WdGoToItem goPage = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
WordApp.Selection.GoToNext(goPage);
}
}
/// <summary>
/// 插入文本
/// </summary>
/// <param name="text"></param>
public void insertText(string text)
{
WordApp.Selection.TypeText(text);
}
/// <summary>
/// 删除文本
/// </summary>
/// <param name="count"></param>
public void deleteText(object count)
{
WordApp.Selection.Delete(ref missing, ref count);
}
/// <summary>
/// 选择所有图形
/// </summary>
public void sel()
{
document.Shapes.SelectAll();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Web;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Net;
namespace WindowsApplication1
{
public partial class Word : Form
{
public Word()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
object oFileName = @"C:\ddddddddd.doc";
object oReadOnly = false;
object oMissing = System.Reflection.Missing.Value;
object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oWord;
Microsoft.Office.Interop.Word.Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;//只是为了方便观察
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//MessageBox.Show(oDoc.Tables.Count.ToString());
Table newTable = oDoc.Tables[1];
//设置表格样式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;
//填充表格内容
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
//删除行
newTable.Rows[newTable.Rows.Count].Delete();
//移动到下一页
Microsoft.Office.Interop.Word.WdGoToItem goPage = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
oWord.Selection.GoToNext(goPage);
//插入图片
string FileName = @"C:\dsdddd.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = oWord.Application.Selection.Range;
oWord.Selection.Document.Shapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Anchor);
oWord.Selection.TypeText("我们的村村sfsfsdfdddddddddd");
object filename=@"C:\eeeeee.doc";
//文件保存
oDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing);
oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
oWord.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void OpenWord_Click(object sender, EventArgs e)
{
Word.Application wordApp = new Word.ApplicationClass(); try
{
// Word程序可见
wordApp.Visible = true; object missing = System.Reflection.Missing.Value; object fileName = @"C:\a.doc"; // 打开Word文档
wordApp.Documents.Open(
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
); Word.Range range1, range2; //// 取得总页数
//int pageNumber = wordApp.ActiveDocument.ComputeStatistics(
// Word.WdStatistic.wdStatisticPages,
// ref missing
// ); // 跳转种类
object objWhat = Word.WdGoToItem.wdGoToPage;
// 跳转位置
object objWhich = Word.WdGoToDirection.wdGoToLast; // 转向最后一页
wordApp.Selection.GoTo(ref objWhat, ref objWhich, ref missing, ref missing); // Range取得
range1 = wordApp.Selection.Range;
range2 = wordApp.ActiveDocument.Range(ref missing, ref missing); object start = range1.Start;
object end = range2.End; // 删除最后一页
wordApp.ActiveDocument.Range(ref start, ref end).Delete(ref missing, ref missing); // 跳转至最前
objWhich = Word.WdGoToDirection.wdGoToFirst;
wordApp.Selection.GoTo(ref objWhat, ref objWhich, ref missing, ref missing); wordApp.ActiveDocument.Save(); wordApp.Quit(ref missing, ref missing, ref missing);
}
finally
{
if (null != wordApp)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
}
}
}
//自定义变量
private Microsoft.Office.Interop.Word.ApplicationClass WordApp = null; //word
private Microsoft.Office.Interop.Word.Document document = null;//document
private object missing = null;//缺省值
/// <summary>
/// 将光标移动至文件头部
/// </summary>
public void moveStart()
{
object story = Microsoft.Office.Interop.Word.WdUnits.wdStory;
WordApp.Selection.HomeKey(ref story, ref missing);//向上移到顶
}
//移动到下一页
private void movePage(int count)
{
for (int i = 0; i < count; i++)
{
Microsoft.Office.Interop.Word.WdGoToItem goPage = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
WordApp.Selection.GoToNext(goPage);
}
}
/// <summary>
/// 插入文本
/// </summary>
/// <param name="text"></param>
public void insertText(string text)
{
WordApp.Selection.TypeText(text);
}
/// <summary>
/// 删除文本
/// </summary>
/// <param name="count"></param>
public void deleteText(object count)
{
WordApp.Selection.Delete(ref missing, ref count);
}
/// <summary>
/// 选择所有图形
/// </summary>
public void sel()
{
document.Shapes.SelectAll();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2006-07-30 公司如何让留住技术人员??