C#在word文档中连续插入表格

本文档介绍如何使用C#操作Microsoft Office Interop Word来创建并连续插入多个表格。通过示例代码展示如何设置表格内容、样式,并在每三个表格后插入分页符。
摘要由CSDN通过智能技术生成
刚学习了c#操作word文档,遇到一个问题“如何在文档中连续插入多个表格”。经过多番查阅资料和尝试,终于将问题解决,现拿出来分享。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Web;
using System.Reflection;

namespace PackingListModel
{
    public partial class Form1 : Form
    {

        int everyBoxNumber = 0;//每箱数量

        public Form1()
        {
            InitializeComponent();
            textBox3.Text = "注:超过100箱,请分批次打印,以减少等待时间,同时可降低出错率!";
        }
        /// <summary>
        /// 创建word文档
        /// </summary>
        private void CreateWord()
        {
            Word.Application app = new Word.ApplicationClass();
            Word.Document doc = new Word.DocumentClass();
            try
            {
                //资产段号初始值
                string propertyBegin = "";
                string propertyEnd = "";
                propertyBegin = txtPropertyNumber2.Text;
                string lastEightStr = propertyBegin.Substring(propertyBegin.Length - 8);//后八位
                int eightNumber1 = Convert.ToInt32(lastEightStr);
                string beforeSection = propertyBegin.Remove(propertyBegin.Length - 8, 8);//前段字符串

                object dirc = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseStart;
                object tend = Microsoft.Office.Interop.Word.WdMovementType.wdMove;
                object character = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
                object oMissing = System.Reflection.Missing.Value;
                app = new Word.Application();//创建word应用程序
                doc = app.Documents.Add();//添加一个word文档
                object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;

                int startBox = Convert.ToInt32(textbox1.Text);//起始箱号
                int endBox = Convert.ToInt32(textBox2.Text);//终止箱号
                int mark = 1; //分页标识
                for (int i = startBox; i <= endBox; i++)
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值