您的位置:首页 > 编程语言 > C#

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

2016-11-28 15:40 417 查看
刚学习了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++)
{
int rows = 9;//表格行数
int cols = 1;//表格列数

object line = Word.WdUnits.wdLine;
app.Selection.MoveDown(ref line, oMissing, oMissing);
app.Selection.TypeParagraph();//换行
Word.Range range = app.Selection.Range;
Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);

//int tableFocus = (i - 1) * 9 + 1;   //注意:连续插入的处理重点就是下面这两句
int tableFocus = 1;//焦点定位
table.Cell(tableFocus, 1).Range.Select();  //获取焦点
object count = 1;

//设置表格的字体大小粗细
table.Range.Font.Size = 10;
table.Range.Font.Bold = 0;

//第一行
table.Cell(tableFocus, 1).Range.Text = txtCompany.Text;
//table.Cell(tableFocus, 1).Range.Font.Name = "Bold";
table.Cell(tableFocus, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
//第二行
table.Cell(tableFocus+1, 1).Range.Text = txtList.Text;
table.Cell(tableFocus + 1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
//table.Cell(tableFocus + 1, 1).Range.Font.Name = "Bold";

//第三行
table.Cell(tableFocus + 2, 1).Range.Text = " 客户名称" + ":" + txtCustomer2.Text;

//第四行
table.Cell(tableFocus + 3, 1).Range.Text = txtModel1.Text + ":" + txtModel2.Text;
//第五行分成四列
table.Cell(tableFocus+4, 1).Split(1, 4);//第五行分成四列
table.Cell(tableFocus + 4, 1).Range.Text = txtSpecification1.Text;
float wd1 = table.Cell(1,1).Width;
table.Cell(tableFocus + 4, 1).Width = wd1/6;
table.Cell(tableFocus + 4, 2).Range.Text = txtSpecification2.Text;
table.Cell(tableFocus + 4, 2).Width = wd1/3;
table.Cell(tableFocus + 4, 3).Range.Text = txtDate1.Text;
table.Cell(tableFocus + 4, 4).Range.Text = txtDate2.Text;

//第六行分成六列
table.Cell(tableFocus + 5, 1).Split(1, 6);
table.Cell(tableFocus + 5, 1).Range.Text = txtBatch1.Text;
table.Cell(tableFocus + 5, 2).Range.Text = txtBatch2.Text;
//table.Cell(tableFocus + 5, 2).Width = wd1 / 12;
table.Cell(tableFocus + 5, 3).Range.Text = txtTotalBox1.Text;
table.Cell(tableFocus + 5, 4).Range.Text = txtTotalBox2.Text;
table.Cell(tableFocus + 5, 5).Range.Text = txtTotalNumber1.Text;
table.Cell(tableFocus + 5, 6).Range.Text = txtTotalNumber2.Text;
//table.Cell(tableFocus + 5, 6).Width = wd1 * 1 / 4;

//第七行分成六列
table.Cell(tableFocus + 6, 1).Split(1, 6);
table.Cell(tableFocus + 6, 1).Range.Text = txtThisBox1.Text;
//txtThisBox2.Text = i.ToString();
table.Cell(tableFocus + 6, 2).Range.Text = i.ToString();
table.Cell(tableFocus + 6, 3).Range.Text = "本箱数量(只)";
table.Cell(tableFocus + 6, 4).Range.Text = txtThisNumber2.Text;
table.Cell(tableFocus + 6, 5).Range.Text = txtContract1.Text;
table.Cell(tableFocus + 6, 6).Range.Text = txtContract2.Text;

//第八行分成四列
table.Cell(tableFocus + 7, 1).Split(1, 4);
table.Cell(tableFocus + 7, 1).Range.Text = txtProductCode1.Text;
table.Cell(tableFocus + 7, 2).Range.Text = txtProductCode2.Text;
table.Cell(tableFocus + 7, 3).Range.Text = txtOrder1.Text;
table.Cell(tableFocus + 7, 4).Range.Text = txtOrder2.Text;

#region 计算资产段号
int begin = eightNumber1 + (i - 1) * everyBoxNumber;
int end = eightNumber1 + i * everyBoxNumber - 1;
propertyBegin = beforeSection + begin.ToString().PadLeft(8,'0');
propertyEnd = beforeSection + end.ToString().PadLeft(8,'0');
#endregion
//第九行分成两列
table.Cell(tableFocus + 8, 1).Split(1, 2);
table.Cell(tableFocus + 8, 1).Range.Text = txtPropertyNumber1.Text;
table.Cell(tableFocus + 8, 2).Range.Text = propertyBegin+"至"+propertyEnd;//资产段号文本框
//propertyBegin = propertyEnd_temp;//下一表格的初始值
table.Cell(tableFocus + 8, 1).Width = table.Cell(8, 1).Width;
table.Cell(tableFocus + 8, 2).Width = table.Cell(8, 1).Width * 3;

//设置表格样式
table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

//下一表格光标定位
object dummy = System.Reflection.Missing.Value;
object what = Word.WdGoToItem.wdGoToLine;
object which = Word.WdGoToDirection.wdGoToLast;
object which1 = Word.WdGoToDirection.wdGoToFirst;
object count2 = tableFocus+1;
doc.Application.Selection.GoTo(ref what, ref which, ref count2, ref dummy);
//每三个表格插入一个分页符
if (mark % 3 == 0)
{
doc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);
}
mark++;
}

//导出文件
string fileName = "";
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
//saveDialog.DefaultExt = "doc";
saveDialog.Filter = "Word文件|*.doc";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //取消返回
Microsoft.Office.Interop.Word.Application docApp = new Microsoft.Office.Interop.Word.Application();
if (docApp == null)
{
MessageBox.Show("无法创建Word对象,您的电脑可能未安装office word");
return;
}
if (saveFileName != "")
{
try
{
doc.Saved = true;
doc.SaveAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (doc != null)
{
doc.Close();//关闭文档
}
if (app != null)
{
app.Quit();//退出应用程序
}
}

}

private void button1_Click_1(object sender, EventArgs e)
{
CreateWord();
}

//计算箱号
private void txtThisNumber2_TextChanged(object sender, EventArgs e)
{
try
{
int totalNumber = Convert.ToInt32(txtTotalNumber2.Text);//总产品数
everyBoxNumber = Convert.ToInt32(txtThisNumber2.Text);//每箱产品数
int boxCount = totalNumber / everyBoxNumber;//总箱数
int remainder = totalNumber % everyBoxNumber;//余数
if (remainder != 0)
{
boxCount = boxCount + 1;//余下的产品加一箱
}
textBox4.Text = remainder.ToString();
txtTotalBox2.Text = boxCount.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: