明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C# word 指定页

Posted on   且行且思  阅读(4647)  评论(1编辑  收藏  举报
复制代码
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(11).Range.Text = "产品详细信息表";
            newTable.Cell(
11).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 = trueobject 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();
        }


    }
}
复制代码
编辑推荐:
· 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 公司如何让留住技术人员??
点击右上角即可分享
微信分享提示