Word系列之快捷快速表格插入文字图片

表格插入图片文字

本章节讲述如何在表格中插入图片和文字,

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

这是单元格内容

代码如下:

private void button2_Click(object sender, EventArgs e)

{

string fileName = @”HelloWord.docx”;

using (WordprocessingDocument wd = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))

{

var mainDocx = wd.AddMainDocumentPart();

var docx = new DocumentFormat.OpenXml.Wordprocessing.Document();

mainDocx.Document = docx;

var body = mainDocx.Document.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Body());

//测试写入表格

string[] headerArray = new string[] { “表头1”, “表头2”, “表头3”, “表头4”, “表头5”, “表头6”, “表头7”, “表头8”, “表头9”, “表头10” };

List rowList = new List();

for (int rowI = 0; rowI < 20; rowI++)

{

object[] t = new object[headerArray.Length];

for (int k = 0; k < headerArray.Length; k++)

{

if (k < headerArray.Length – 1)

{

t[k] = “第” + rowI + “行第” + k + “列”;

}

if (k == headerArray.Length – 1)

{

string imgFile = “Test_1.jpg”;

Stream stream = File.OpenRead(imgFile);

t[k] = stream;

}

}

rowList.Add(t);

}

#region 添加文字和图片表格

#region 表格

//表格由 Table、TableRow、TableCell 三级结构构成。那么,向文档中添加一个 9 行 x 6 列的表格

DocumentFormat.OpenXml.Wordprocessing.Table weekTable = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Table());

#region 表格样式

TableProperties tabProps = new TableProperties(new TableBorders(

new TopBorder { Val = new EnumValue(BorderValues.Single), Size = 4, Color = “4F81BD” },

new BottomBorder

{

Val = new EnumValue(BorderValues.Single),

Size = 4,

Color = “4F81BD”

},

new LeftBorder

{

Val = new EnumValue(BorderValues.Single),

Size = 4,

Color = “4F81BD”

},

new RightBorder

{

Val = new EnumValue(BorderValues.Single),

Size = 4,

Color = “4F81BD”

},

new InsideHorizontalBorder

{

Val = new EnumValue(BorderValues.Single),

Size = 4,

Color = “4F81BD”

},

new InsideVerticalBorder

{

Val = new EnumValue(BorderValues.Single),

Size = 4,

Color = “4F81BD”

}

));

#endregion

weekTable.AppendChild(tabProps);

var tabWidth = new TableWidth { Width = “5000”, Type = TableWidthUnitValues.Pct };

weekTable.AppendChild(tabWidth);

if (headerArray != null && headerArray.Length > 0)

{

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

foreach (var item in headerArray)

{

TableRowProperties tabRowProps = tabHeaderRow.AppendChild(new TableRowProperties(new TableRowHeight { Val = 600, HeightType = HeightRuleValues.Exact }));

TableCell tabCell = tabHeaderRow.AppendChild(new TableCell());

Paragraph tabCellPara = tabCell.AppendChild(new Paragraph());

TableCellProperties tabCellProps = tabCell.AppendChild(new TableCellProperties(new TableCellWidth { Width = “10%”, Type = TableWidthUnitValues.Pct }));

tabCellPara.AppendChild(new Run(new Text(item)));

}

}

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

foreach (object[] rowArray in rowList)

{

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

TableRowProperties tabRowProps = tabRow.AppendChild(new TableRowProperties(new TableRowHeight { Val = 1500, HeightType = HeightRuleValues.Exact }));

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

foreach (object objCellValue in rowArray)

{

TableCell tabCell = tabRow.AppendChild(new TableCell());

TableCellProperties tabCellProps;

tabCellProps = tabCell.AppendChild(new TableCellProperties(new TableCellWidth { Width = “10%”, Type = TableWidthUnitValues.Pct }));

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

{

Paragraph tabCellPara = tabCell.AppendChild(new Paragraph());

tabCellPara.AppendChild(new Run(new Text(“”)));//这里写入单元格的值

}

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

{

Paragraph tabCellPara = tabCell.AppendChild(new Paragraph());

tabCellPara.AppendChild(new Run(new Text(objCellValue.ToString())));//这里写入单元格的值

}

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

{

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

AddImageToTableCell(tabCell, tempRelationId);

}

}

}

#endregion

#endregion

}

Process.Start(fileName);

}

public static void AddImageToTableCell(TableCell tableCell, string relationshipId)

{

var element =

new Drawing(

new DocumentFormat.OpenXml.Drawing.Wordprocessing.Inline(

new DocumentFormat.OpenXml.Drawing.Wordprocessing.Extent() { Cx = 990000L, Cy = 792000L },

new DocumentFormat.OpenXml.Drawing.Wordprocessing.EffectExtent()

{

LeftEdge = 0L,

TopEdge = 0L,

RightEdge = 0L,

BottomEdge = 0L

},

new DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties()

{

Id = (UInt32Value)1U,

Name = “Picture1”,

Title = “图片标题

},

new DocumentFormat.OpenXml.Drawing.Wordprocessing.NonVisualGraphicFrameDrawingProperties(

new DocumentFormat.OpenXml.Drawing.GraphicFrameLocks() { NoChangeAspect = true }),

new DocumentFormat.OpenXml.Drawing.Graphic(

new DocumentFormat.OpenXml.Drawing.GraphicData(

new DocumentFormat.OpenXml.Drawing.Pictures.Picture(

new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureProperties(

new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualDrawingProperties()

{

Id = (UInt32Value)0U,

Name = “New Bitmap Image.jpg”

},

new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureDrawingProperties()),

new DocumentFormat.OpenXml.Drawing.Pictures.BlipFill(

new DocumentFormat.OpenXml.Drawing.Blip(

new DocumentFormat.OpenXml.Drawing.BlipExtensionList(

new DocumentFormat.OpenXml.Drawing.BlipExtension()

{

Uri =

“{28A0092B-C50C-407E-A947-70E740481C1C}”

})

)

{

Embed = relationshipId,

CompressionState =

DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print

},

new DocumentFormat.OpenXml.Drawing.Stretch(

new DocumentFormat.OpenXml.Drawing.FillRectangle())),

new DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties(

new DocumentFormat.OpenXml.Drawing.Transform2D(

new DocumentFormat.OpenXml.Drawing.Offset() { X = 0L, Y = 0L },

new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 990000L, Cy = 792000L }),

new DocumentFormat.OpenXml.Drawing.PresetGeometry(

new DocumentFormat.OpenXml.Drawing.AdjustValueList()

)

{ Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle }))

)

{ Uri = “http://schemas.openxmlformats.org/drawingml/2006/picture” })

)

{

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.

tableCell.AppendChild(new Paragraph(new Run(element)));

}

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月13日
下一篇 2022年6月13日

相关推荐

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息