C# Aspose.Word 操作word文档【三】
2018-01-18 18:19
483 查看
第三种方法我也是从网上找到的,借鉴下了,然后自己整理出的!1、建好word 模板
你没有看错,第三种是只有这样的表头,然后动态生成数据!
2、网上找到的代码: static Aspose.Words.Tables.Cell CreateCell(string value, Document doc)
{
Aspose.Words.Tables.Cell c1 = new Aspose.Words.Tables.Cell(doc);
Aspose.Words.Paragraph p = new Paragraph(doc);
p.AppendChild(new Run(doc, value));
c1.AppendChild(p);
return c1;
}
static Aspose.Words.Tables.Row CreateRow(int columnCount, string[] columnValues, Document doc)
{
Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc);
for (int i = 0; i < columnCount; i++)
{
if (columnValues.Length > i)
{
4000
var cell = CreateCell(columnValues[i], doc);
r2.Cells.Add(cell);
}
else
{
var cell = CreateCell("", doc);
r2.Cells.Add(cell);
}
}
return r2;
}
3、调用网上的代码,结合自己代码: string tempFile = Path.GetFullPath("resource/templete/项目建议书模板.doc").ToString();
Document doc = new Document(tempFile);
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); //获取word中所有表格table
Aspose.Words.Tables.Table table = allTables[4] as Aspose.Words.Tables.Table;//拿到第5个表格 这里你word中是第几个表格就写第几个表格
for (int i = 0; i < dt3.Rows.Count; i++) //dt3 为数据源
{
var row = CreateRow(13, (new string[] { (dt3.Rows.Count-i).ToString(), dt3.Rows[i]["SBBianHao"].ToString(), dt3.Rows[i]["ZiChanBianHao"].ToString(), dt3.Rows[i]["SBMingCheng"].ToString(),dt3.Rows[i]["SuoShuZhanXian"].ToString()
,dt3.Rows[i]["SBLeiXing"].ToString(),dt3.Rows[i]["DianYaDengJi"].ToString(),dt3.Rows[i]["SBXingHao"].ToString(),dt3.Rows[i]["TouYunRiQi"].ToString(),dt3.Rows[i]["ShuLiang"].ToString(),
dt3.Rows[i]["JiLiangDanWei"].ToString(),dt3.Rows[i]["ChuZhiJianYi"].ToString(),dt3.Rows[i]["BeiZhu"].ToString()}), doc); //创建一行
table.Rows.Insert(1, row); //将此行插入第一行的上方
}
4、效果图:
你没有看错,第三种是只有这样的表头,然后动态生成数据!
2、网上找到的代码: static Aspose.Words.Tables.Cell CreateCell(string value, Document doc)
{
Aspose.Words.Tables.Cell c1 = new Aspose.Words.Tables.Cell(doc);
Aspose.Words.Paragraph p = new Paragraph(doc);
p.AppendChild(new Run(doc, value));
c1.AppendChild(p);
return c1;
}
static Aspose.Words.Tables.Row CreateRow(int columnCount, string[] columnValues, Document doc)
{
Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc);
for (int i = 0; i < columnCount; i++)
{
if (columnValues.Length > i)
{
4000
var cell = CreateCell(columnValues[i], doc);
r2.Cells.Add(cell);
}
else
{
var cell = CreateCell("", doc);
r2.Cells.Add(cell);
}
}
return r2;
}
3、调用网上的代码,结合自己代码: string tempFile = Path.GetFullPath("resource/templete/项目建议书模板.doc").ToString();
Document doc = new Document(tempFile);
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); //获取word中所有表格table
Aspose.Words.Tables.Table table = allTables[4] as Aspose.Words.Tables.Table;//拿到第5个表格 这里你word中是第几个表格就写第几个表格
for (int i = 0; i < dt3.Rows.Count; i++) //dt3 为数据源
{
var row = CreateRow(13, (new string[] { (dt3.Rows.Count-i).ToString(), dt3.Rows[i]["SBBianHao"].ToString(), dt3.Rows[i]["ZiChanBianHao"].ToString(), dt3.Rows[i]["SBMingCheng"].ToString(),dt3.Rows[i]["SuoShuZhanXian"].ToString()
,dt3.Rows[i]["SBLeiXing"].ToString(),dt3.Rows[i]["DianYaDengJi"].ToString(),dt3.Rows[i]["SBXingHao"].ToString(),dt3.Rows[i]["TouYunRiQi"].ToString(),dt3.Rows[i]["ShuLiang"].ToString(),
dt3.Rows[i]["JiLiangDanWei"].ToString(),dt3.Rows[i]["ChuZhiJianYi"].ToString(),dt3.Rows[i]["BeiZhu"].ToString()}), doc); //创建一行
table.Rows.Insert(1, row); //将此行插入第一行的上方
}
4、效果图:
相关文章推荐
- C# Aspose.Word 操作word文档【二】
- C# Aspose.Word 操作word文档【一】
- C# Aspose.Word 操作word文档【一】
- C# Aspose.Word 操作word文档【二】
- C# Aspose.Word 操作word文档【三】
- C# Aspose.Word 操作word文档【四】
- C# Aspose.Word 操作word文档【五】
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C#通过COM组件对WORD文档操作
- C#对Word文档的创建、插入表格、设置样式等操作实例
- C# 操作word文档
- C#操作Word文档(Office 2007)
- c#操作word文档之简历导出
- C# 操作Word文档
- 利用Aspose.Words处理Word文档之间的转换和内容操作
- C# 操作Word文档(转)
- C#操作Word/excel文档
- C#操作Word文档(Office 2007)
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
- C#操作word文档(三)