您的位置 首页 > 职场江湖

【怎么把文字粘进word表格里】插入Word系列快捷表文本图片

插入表格图片和文本

本章介绍如何在表中插入图片和文本。

Word的数据只有段落和表格。Word中的表表示数据

Table

TableRow

TableCell段落RunText单元格内容/Text/Run/段落

段落Img/段落

/TableCell

/TableRow

/Table

代码为:

Private void button2 _ click(对象服务器、事件e)

{

string filename=@ ' hello word . docx '

using(wordprocessingdocument wd=word(文件名,word))

{

varmain docx=wd . addmaindocumentpart();

var docx=new Documen();

MainDocx。Document=docx

var body=main docx . document . appendchild(new documen());

//测试写表单

string[]header array=new string[]{ ' header 1 '' headerarray '' headerarray '' headerarray '' headerarray '' headerarray '"

list object[]rowlist=new list object[]();

for(int rowI=0;第20行;RowI)

{

object[]t=new object[];

for(int k=0;k;k)

{

If (k-1)

{

T[k]=' rowI '行' k '列;

}

If (k==-1)

{

String imgFile=' Te中选择另一种天花板类型。

stream stream=file . open read(imgfile);

T[k]=流;

}

}

低里(t);

}

#region添加文本和图片表

#region表

//表由Table、TableRow和TableCell三级结构组成。然后在文档中添加9行x 6列表

documen week table=body . append child(new documen());

#region表格样式

table properties tab props=new table properties(

new top border { val=new enumvaluebordervalues),size=4,color=' 4f81bd'},

New BottomBorder

{

Val=new EnumValueBorderValues)、

Size=4,

Color='4F81BD '

},

新LeftBorder

{

Val=new EnumValueBorderValues)、

Size=4,

Color='4F81BD '

},

新RightBorder

{

Val=new EnumValueBorderValues)、

Size=4,

Color='4F81BD '

},

New InsideHorizontalBorder

{

Val=new EnumValueBorderValues)、

Size=4,

Color='4F81BD '

},

n

ew InsideVerticalBorder

{

Val = new EnumValue<BorderValues>),

Size = 4,

Color = "4F81BD"

}

));

#endregion

weekTable.AppendChild(tabProps);

var tabWidth = new TableWidth { Width = "5000", Type = TableWid };

weekTable.AppendChild(tabWidth);

if (headerArray != null && > 0)

{

TableRow tabHeaderRow = weekTable.AppendChild(new TableRow());//设置列头行

foreach (var item in headerArray)

{

TableRowProperties tabRowProps = (new TableRowProperties(new TableRowHeight { Val = 600, HeightType = Heig }));

TableCell tabCell = (new TableCell());

Paragraph tabCellPara = (new Paragraph());

TableCellProperties tabCellProps = (new TableCellProperties(new TableCellWidth { Width = "10%", Type = TableWid }));

(new Run(new Text(item)));

}

}

//如果要 Word 能够正常打开文档,每个 TableCell 至少需包含一个空段落才行。

foreach (object[] rowArray in rowList)

{

TableRow tabRow = weekTable.AppendChild(new TableRow());

TableRowProperties tabRowProps = (new TableRowProperties(new TableRowHeight { Val = 1500, HeightType = Heig }));

//列宽可以通过设置单元格的宽度实现

foreach (object objCellValue in rowArray)

{

TableCell tabCell = (new TableCell());

TableCellProperties tabCellProps;

tabCellProps = (new TableCellProperties(new TableCellWidth { Width = "10%", Type = TableWid }));

if (objCellValue == null || objCellValue == DBNull.Value)

{

Paragraph tabCellPara = (new Paragraph());

(new Run(new Text("")));//这里写入单元格的值

}

if (objCellValue != null && objCellValue != DBNull.Value && objCellValue is string)

{

Paragraph tabCellPara = (new Paragraph());

(new Run(new Tex())));//这里写入单元格的值

}

if (objCellValue != null && objCellValue is Stream)

{

string tempRelationId = Guid.NewGuid().ToString().ToUpper();

AddImageToTableCell(tabCell, tempRelationId);

}

}

}

#endregion

#endregion

}

Proce(fileName);

}

public static void AddImageToTableCell(TableCell tableCell, string relationshipId)

{

var element =

new Drawing(

new Documen(

new Documen() { Cx = 990000L, Cy = 792000L },

new Documen()

{

LeftEdge = 0L,

TopEdge = 0L,

RightEdge = 0L,

BottomEdge = 0L

},

new Documen()

{

Id = (UInt32Value)1U,

Name = "Picture1",

Title = "图片标题"

},

new Documen(

new Documen() { NoChangeAspect = true }),

new Documen(

new DocumenData(

new Documen(

new Documen(

new Documen()

{

Id = (UInt32Value)0U,

Name = "New Bitmap Image.jpg"

},

new Documen()),

new Documen(

new Documen(

new DocumenExtensionList(

new DocumenExtension()

{

Uri =

"{28A0092B-C50C-407E-A947-70E740481C1C}"

})

)

{

Embed = relationshipId,

CompressionState =

DocumenCompressionValues.Print

},

new Documen(

new Documen())),

new Documen(

new Documen(

new Documen() { X = 0L, Y = 0L },

new Documen() { Cx = 990000L, Cy = 792000L }),

new Documen(

new Documen()

)

{ Preset = Documen }))

)

{ Uri = "; })

)

{

DistanceFromTop = (UInt32Value)0U,

DistanceFromBottom = (UInt32Value)0U,

DistanceFromLeft = (UInt32Value)0U,

DistanceFromRight = (UInt32Value)0U,

EditId = "50D07946"

});

// Append the reference to body, the element should be in a Run.

(new Paragraph(new Run(element)));

}

关于作者: luda

无忧经验小编鲁达,内容侵删请Email至wohenlihai#qq.com(#改为@)

热门推荐