刚学习了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++)