Word中根据表头动态增加表格行,然后往表格里写数据,以及在Word中添加书签,然后替换书签值
2016-11-08 16:54
841 查看
在Word中,对于已经有了固定的表头的表格,但是不知道表格具体需要多少行时,我们可以根据需要动态的增加,以及单元格 的拆分,合并等操作。以下 是具体的例子代码(对于没有表头的表格,参照于我的另一篇博客):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word;
using System.Windows;
using C_STP_WPF_Building.entity;
namespace C_STP_WPF_Building
{
public class Report
{
private _Application wordApp = null; //代表当前应用程序
public _Document wordDoc = null;//代表当前的word文档
private Table table = null;//代表word文档中的表格
private Range allRange;
public _Application Application
{
get
{
return wordApp;
}
set
{
wordApp = value;
}
}
public _Document Document
{
get
{
return wordDoc;
}
set
{
wordDoc = value;
}
}
//通过模板打开文档
public void CreateNewDocument(string filePath)
{
try
{
killWinWordProcess();//杀死word进程
wordApp = new Word.Application();
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;
object missing = System.Reflection.Missing.Value;
object templateName = filePath;
wordDoc = wordApp.Documents.Open(ref templateName, 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);
table = wordDoc.Tables[15];//文档中的表格,如果是第一个表格Tables[1].
allRange = wordDoc.Range(missing, missing);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
//该类表示向Word表格中的第n个表格中添加num行,然后向数组中的数据写入表格中
//n为表格序号
//num为要插入几行数据
//rp为插入数据的数组
//包括插入表格,表格合并,表格拆分,插入数据
public void AddRow10(int n, int num, ResultParameter[] rp, Carproportion[] cararray, Variabless[] variablessarray)
{
object miss = System.Reflection.Missing.Value;
Word.Table table = wordDoc.Content.Tables
;//获取第几个表格
int row = variablessarray.Length * cararray.Length;
for (int i = 0; i < row; i++)
{
table.Rows.Add(ref miss);//插入表格行,插入一行
}
int resultcount = -1;
for (int j = 2; j < row + 2; j = j + 2 * cararray.Length)
{
int i = j;
int nu = j / (2 * cararray.Length);
int carcount = -1;
table.Cell(j, 1).Select();//纵向合并单元格 序号部分
object moveUnit1 = Word.WdUnits.wdLine;
object moveCount1 = 2 * cararray.Length - 1;
object moveExtend1 = Word.WdMovementType.wdExtend;
wordApp.Selection.MoveDown(ref moveUnit1, ref moveCount1, ref moveExtend1);
wordApp.Selection.Cells.Merge();
//下面序号赋值
table.Cell(j, 1).Range.Text = (nu + 1).ToString();
table.Cell(j, 2).Select(); //纵向合并单元格 隧道名称部分
object moveUnit2 = Word.WdUnits.wdLine;
object moveCount2 = 2 * cararray.Length - 1;
object moveExtend2 = Word.WdMovementType.wdExtend;
wordApp.Selection.MoveDown(ref moveUnit2, ref moveCount2, ref moveExtend2);
wordApp.Selection.Cells.Merge();
//下面隧道名称赋值
table.Cell(j, 2).Range.Text = variablessarray[nu * 2].uname.ToString();
for (int z = 0; z < cararray.Length; z++)
{
try
{
table.Cell(i, 3).Select();
}
catch { }
object moveUnit3 = Word.WdUnits.wdLine;
object moveCount3 = 1;
object moveExtend3 = Word.WdMovementType.wdExtend;
wordApp.Selection.MoveDown(ref moveUnit3, ref moveCount3, ref moveExtend3);
wordApp.Selection.Cells.Merge();
table.Cell(i, 3).Split(1, 2); //拆分单元格
table.Cell(i, 4).Split(2, 1);
carcount++;
table.Cell(i, 3).Range.Text = cararray[carcount].targetyear.ToString();
table.Cell(i, 4).Range.Text = "左幅";
table.Cell(i + 1, 4).Range.Text = "右幅";
//下面赋值
resultcount++;
if (rp[resultcount].vt != 80)
{
table.Cell(i, 5).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 5).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 70)
{
table.Cell(i, 6).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 6).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 60)
{
table.Cell(i, 7).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 7).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 50)
{
table.Cell(i, 8).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 8).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 40)
{
table.Cell(i, 9).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 9).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 30)
{
table.Cell(i, 10).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 10).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 20)
{
table.Cell(i, 11).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 11).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 10)
{
table.Cell(i, 12).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 12).Range.Text = rp[resultcount].qvi.ToString();
}
//下一列
resultcount++;
if (rp[resultcount].vt != 80)
{
table.Cell(i + 1, 5).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 5).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 70)
{
table.Cell(i + 1, 6).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 6).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 60)
{
table.Cell(i + 1, 7).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 7).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 50)
{
table.Cell(i + 1, 8).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 8).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 40)
{
table.Cell(i + 1, 9).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 9).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 30)
{
table.Cell(i + 1, 10).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 10).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 20)
{
table.Cell(i + 1, 11).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 11).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 10)
{
table.Cell(i + 1, 12).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 12).Range.Text = rp[resultcount].qvi.ToString();
}
i = i + 2;
}
}
}
//在Word中定义好书签的名称,然后用以下代码实现替换书签处
public void InsertNum(double a,double b,double c,double d,double e,double f,double g,double h)
{
object oStart1 = "fan_speed1";//word中的书签名
object oStart2 = "free_number";//word中的书签名
object oStart3 = "r_fan_speed";//word中的书签名
object oStart4 = "l_fan_speed";//word中的书签名
object oStart5 = "Ar";//word中的书签名
object oStart6 = "vc";//word中的书签名
object oStart7 = "r_Vn";//word中的书签名
object oStart8 = "l_Vn";//word中的书签名
Range range1 = wordDoc.Bookmarks.get_Item(ref oStart1).Range;//表格插入位置
Range range2 = wordDoc.Bookmarks.get_Item(ref oStart2).Range;//表格插入位置
Range range3 = wordDoc.Bookmarks.get_Item(ref oStart3).Range;//表格插入位置
Range range4 = wordDoc.Bookmarks.get_Item(ref oStart4).Range;//表格插入位置
Range range5 = wordDoc.Bookmarks.get_Item(ref oStart5).Range;//表格插入位置
Range range6 = wordDoc.Bookmarks.get_Item(ref oStart6).Range;//表格插入位置
Range range7 = wordDoc.Bookmarks.get_Item(ref oStart7).Range;//表格插入位置
Range range8 = wordDoc.Bookmarks.get_Item(ref oStart8).Range;//表格插入位置
range1.Text = Convert.ToString(a);//往书签处写文字
range2.Text = Convert.ToString(b);//往书签处写文字
range3.Text = Convert.ToString(c);//往书签处写文字
range4.Text = Convert.ToString(d);//往书签处写文字
range5.Text = Convert.ToString(e);//往书签处写文字
range6.Text = Convert.ToString(f);//往书签处写文字
range7.Text = Convert.ToString(g);//往书签处写文字
range8.Text = Convert.ToString(h);//往书签处写文字
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word;
using System.Windows;
using C_STP_WPF_Building.entity;
namespace C_STP_WPF_Building
{
public class Report
{
private _Application wordApp = null; //代表当前应用程序
public _Document wordDoc = null;//代表当前的word文档
private Table table = null;//代表word文档中的表格
private Range allRange;
public _Application Application
{
get
{
return wordApp;
}
set
{
wordApp = value;
}
}
public _Document Document
{
get
{
return wordDoc;
}
set
{
wordDoc = value;
}
}
//通过模板打开文档
public void CreateNewDocument(string filePath)
{
try
{
killWinWordProcess();//杀死word进程
wordApp = new Word.Application();
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;
object missing = System.Reflection.Missing.Value;
object templateName = filePath;
wordDoc = wordApp.Documents.Open(ref templateName, 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);
table = wordDoc.Tables[15];//文档中的表格,如果是第一个表格Tables[1].
allRange = wordDoc.Range(missing, missing);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
//该类表示向Word表格中的第n个表格中添加num行,然后向数组中的数据写入表格中
//n为表格序号
//num为要插入几行数据
//rp为插入数据的数组
//包括插入表格,表格合并,表格拆分,插入数据
public void AddRow10(int n, int num, ResultParameter[] rp, Carproportion[] cararray, Variabless[] variablessarray)
{
object miss = System.Reflection.Missing.Value;
Word.Table table = wordDoc.Content.Tables
;//获取第几个表格
int row = variablessarray.Length * cararray.Length;
for (int i = 0; i < row; i++)
{
table.Rows.Add(ref miss);//插入表格行,插入一行
}
int resultcount = -1;
for (int j = 2; j < row + 2; j = j + 2 * cararray.Length)
{
int i = j;
int nu = j / (2 * cararray.Length);
int carcount = -1;
table.Cell(j, 1).Select();//纵向合并单元格 序号部分
object moveUnit1 = Word.WdUnits.wdLine;
object moveCount1 = 2 * cararray.Length - 1;
object moveExtend1 = Word.WdMovementType.wdExtend;
wordApp.Selection.MoveDown(ref moveUnit1, ref moveCount1, ref moveExtend1);
wordApp.Selection.Cells.Merge();
//下面序号赋值
table.Cell(j, 1).Range.Text = (nu + 1).ToString();
table.Cell(j, 2).Select(); //纵向合并单元格 隧道名称部分
object moveUnit2 = Word.WdUnits.wdLine;
object moveCount2 = 2 * cararray.Length - 1;
object moveExtend2 = Word.WdMovementType.wdExtend;
wordApp.Selection.MoveDown(ref moveUnit2, ref moveCount2, ref moveExtend2);
wordApp.Selection.Cells.Merge();
//下面隧道名称赋值
table.Cell(j, 2).Range.Text = variablessarray[nu * 2].uname.ToString();
for (int z = 0; z < cararray.Length; z++)
{
try
{
table.Cell(i, 3).Select();
}
catch { }
object moveUnit3 = Word.WdUnits.wdLine;
object moveCount3 = 1;
object moveExtend3 = Word.WdMovementType.wdExtend;
wordApp.Selection.MoveDown(ref moveUnit3, ref moveCount3, ref moveExtend3);
wordApp.Selection.Cells.Merge();
table.Cell(i, 3).Split(1, 2); //拆分单元格
table.Cell(i, 4).Split(2, 1);
carcount++;
table.Cell(i, 3).Range.Text = cararray[carcount].targetyear.ToString();
table.Cell(i, 4).Range.Text = "左幅";
table.Cell(i + 1, 4).Range.Text = "右幅";
//下面赋值
resultcount++;
if (rp[resultcount].vt != 80)
{
table.Cell(i, 5).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 5).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 70)
{
table.Cell(i, 6).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 6).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 60)
{
table.Cell(i, 7).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 7).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 50)
{
table.Cell(i, 8).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 8).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 40)
{
table.Cell(i, 9).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 9).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 30)
{
table.Cell(i, 10).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 10).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 20)
{
table.Cell(i, 11).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 11).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 10)
{
table.Cell(i, 12).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i, 12).Range.Text = rp[resultcount].qvi.ToString();
}
//下一列
resultcount++;
if (rp[resultcount].vt != 80)
{
table.Cell(i + 1, 5).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 5).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 70)
{
table.Cell(i + 1, 6).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 6).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 60)
{
table.Cell(i + 1, 7).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 7).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 50)
{
table.Cell(i + 1, 8).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 8).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 40)
{
table.Cell(i + 1, 9).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 9).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 30)
{
table.Cell(i + 1, 10).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 10).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 20)
{
table.Cell(i + 1, 11).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 11).Range.Text = rp[resultcount].qvi.ToString();
}
resultcount++;
if (rp[resultcount].vt != 10)
{
table.Cell(i + 1, 12).Range.Text = "-";
resultcount--;
}
else
{
table.Cell(i + 1, 12).Range.Text = rp[resultcount].qvi.ToString();
}
i = i + 2;
}
}
}
//在Word中定义好书签的名称,然后用以下代码实现替换书签处
public void InsertNum(double a,double b,double c,double d,double e,double f,double g,double h)
{
object oStart1 = "fan_speed1";//word中的书签名
object oStart2 = "free_number";//word中的书签名
object oStart3 = "r_fan_speed";//word中的书签名
object oStart4 = "l_fan_speed";//word中的书签名
object oStart5 = "Ar";//word中的书签名
object oStart6 = "vc";//word中的书签名
object oStart7 = "r_Vn";//word中的书签名
object oStart8 = "l_Vn";//word中的书签名
Range range1 = wordDoc.Bookmarks.get_Item(ref oStart1).Range;//表格插入位置
Range range2 = wordDoc.Bookmarks.get_Item(ref oStart2).Range;//表格插入位置
Range range3 = wordDoc.Bookmarks.get_Item(ref oStart3).Range;//表格插入位置
Range range4 = wordDoc.Bookmarks.get_Item(ref oStart4).Range;//表格插入位置
Range range5 = wordDoc.Bookmarks.get_Item(ref oStart5).Range;//表格插入位置
Range range6 = wordDoc.Bookmarks.get_Item(ref oStart6).Range;//表格插入位置
Range range7 = wordDoc.Bookmarks.get_Item(ref oStart7).Range;//表格插入位置
Range range8 = wordDoc.Bookmarks.get_Item(ref oStart8).Range;//表格插入位置
range1.Text = Convert.ToString(a);//往书签处写文字
range2.Text = Convert.ToString(b);//往书签处写文字
range3.Text = Convert.ToString(c);//往书签处写文字
range4.Text = Convert.ToString(d);//往书签处写文字
range5.Text = Convert.ToString(e);//往书签处写文字
range6.Text = Convert.ToString(f);//往书签处写文字
range7.Text = Convert.ToString(g);//往书签处写文字
range8.Text = Convert.ToString(h);//往书签处写文字
}
相关文章推荐
- jquery 实现表格内的tr与td,动态添加与删除. 以及序号增加与减少,大多用在Form里
- jquery 基本讲解和表格添加数据替换表格以及清空
- 使用jQuery动态创建一个表格,根据用户数输入的内容添加一行数据
- dropdownlist的动态绑定数据然后怎么添加默认项“请选择”以及如何清空成“请选择”
- easyui tree datagrid动态添加表头和表格数据,动态弹出框,修改和删除按钮
- 使用jQuery动态创建一个表格,根据用户数输入的内容添加一行数据
- 使用jQuery动态创建一个表格(根据用户输入的内容添加一行数据,并且能逐行删除)
- 应用jQuery实现表格数据的动态添加与统计
- 用C#动态生成Word文档并将数据填入Word表格中
- VBScript生成Word,并添加表格及书签
- javascript动态添加表格数据行(ASP后台数据库保存例子)
- 用Aspose.Words for .NET动态生成word文档中的数据表格
- 应用jQuery实现表格数据的动态添加与统计
- [C#]用C#动态生成Word文档并将数据填入Word表格中
- 推荐.NET使用Aspose.Words动态生成word文档数据表格(附Aspose.Words for NET 6.5 破解版下载)
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)
- 【转载自某园友】用Aspose.Words for .NET动态生成word文档中的数据表格
- 本人改编的“javascript动态添加表格数据行,ASP后台数据库保存例子”
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)