首先是引用
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Tables;
每页贴图处理
Document doc = new Document(open.FileName);//读取word文件
Table TableMain = doc.GetChildNodes(NodeType.Table,true)[0] as Table;//以表格方式读取数据
Document DocSetOut=null;// = new Document();创建空文件,如果实例化将多出一个空页面
Image img = Image.FromFile("d:\\223.JPG");
Aspose.Words.Layout.LayoutCollector layoutCollector = new Aspose.Words.Layout.LayoutCollector(doc);//获取页面设置属性
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);//分页完成
for (int i = 0; i < doc.PageCount; i++)
{
int PageC = i+1;
Document onePage = splitter.GetDocumentOfPage(PageC);//获取单个页面
DocumentBuilder buidone = new DocumentBuilder(onePage);//创建页面写入
Shape shape = new Shape(onePage,ShapeType.Image);//创建操作框以及类型
shape.ImageData.SetImage(img);
shape.Width = 50;//设置宽和高
shape.Height =50;
shape.WrapType = WrapType.Square;//嵌入方式
buidone.MoveToParagraph(0,0);//写入文档位置
buidone.InsertNode(shape);//写入文档
if (DocSetOut == null)
{
DocSetOut = onePage;
}
else
{
DocSetOut.AppendDocument(onePage, ImportFormatMode.KeepDifferentStyles);
}
}
DocSetOut.Save("e:\\testSquare.docx");
关于表操作
获取到word中的表格:
//打开word文档,fileName是路径地址,需要扩展名
Aspose.Words.Document doc = new Document(fileName);
//获取word文档中的第一个表格
var table0 = doc.GetChildNodes(NodeType.Table, true)[0] as Aspose.Words.Tables.Table;
给某个单元格赋值
//获取table中的某个单元格,从0开始
Cell lshCell = table0.Rows[13].Cells[1];
//将单元格中的第一个段落移除
lshCell.FirstParagraph.Remove();
//新建一个段落
Paragraph p = new Paragraph(doc);
//设置一个string的值
string value = "新建一个string的值";
//把设置的值赋给之前新建的段落
p.AppendChild(new Run(doc, value));
//将此段落加到单元格内
lshCell.AppendChild(p);
获取具体的某个单元格并获取其中的值
Cell cell = table0.Rows[3].Cells[1];
//用GetText()的方法来获取cell中的值
string cbfbm = cell.GetText();
cbfbm = cbfbm.Replace("\a", "");
cbfbm = cbfbm.Replace("\r", "");
另一种 设置图片的方式(与上面的方式雷同):
DocumentBuilder buidone = new DocumentBuilder(Doc);
Shape shape = buidone.InsertImage(ImgBit, ImgBit.Width, ImgBit.Height);//new Shape(Doc, ShapeType.Image);
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Left;//
shape.WrapType = WrapType.Square;
使用foreach的反射方式获取到想要的数据
if (doc.GetChildNodes(NodeType.Table, true).Count > 0)
{
Table Tb = doc.GetChildNodes(NodeType.Table, true)[0] as Table;
foreach (Row oneRow in Tb.Rows)
{
for (int i = 0; i < oneRow.Cells.Count; i++)
{
string cellValue = oneRow.Cells[i].GetText();
cellValue = cellValue.Replace("\a", "");
cellValue = cellValue.Replace("\r", "");
var dtype = Model.GetType();
foreach (PropertyInfo onePro in dtype.GetProperties())
{
string DESStr = "Nothing";
// DescriptionAttribute[] des = (DescriptionAttribute[])onePro.GetCustomAttributes(typeof(DescriptionAttribute), false);// 属性值
if ((DescriptionAttribute)Attribute.GetCustomAttribute(onePro, typeof(DescriptionAttribute)) != null) //当描述属性没有时,直接返回名称
{
DESStr = ((DescriptionAttribute)Attribute.GetCustomAttribute(onePro, typeof(DescriptionAttribute))).Description;
}
cellValue = cellValue.Replace(" ", "");//去除字段中的空格
if (cellValue.Contains(DESStr)&&cellValue.Length<40)
{
string CellNextText = oneRow.Cells[i + 1].GetText();
CellNextText = CellNextText.Replace("\a", "");
CellNextText = CellNextText.Replace("\r", "");
onePro.SetValue(Model, CellNextText, null);
}
}
}
}
}