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)删除。
用户投稿
上一篇 2022年6月13日 12:09
下一篇 2022年6月13日 12:10

相关推荐

  • 闲鱼无货源怎么赚钱(闲鱼无货源卖什么好)

    如今电商平台开店,无货源模式已经成为大家最普遍的开店方式了,而其中闲鱼无货源就是不少人的首选。闲鱼无货源是一个很适合普通人操作的暴利项目,如果你没有知识,技能,经验,资源,就先从闲…

    2022年11月25日
  • 什么是推广cpa一篇文章带你看懂CPA推广渠道

    CPA渠道 CPA指的是按照指定的行为结算,可以是搜索,可以是注册,可以是激活,可以是搜索下载激活,可以是绑卡,实名认证,可以是付费,可以是浏览等等。甲乙双方可以根据自己的情况来定…

    2022年11月25日
  • 抖音直播带货有哪些方法技巧(抖音直播带货有哪些痛点)

    如今抖音这个短视频的变现能力越来越突显了,尤其是在平台上开通直播,更具有超强的带货属性,已经有越来越多的普通人加入到其中了。不过直播带货虽然很火,但是也不是每个人都能做好的,那么在…

    2022年11月24日
  • 白衬衫搭配什么裤子好看,女生衬衫穿法图片

    说起白衬衫和长裤的搭配组合,不知道大家有没有发现,虽然是很常见的造型,可不同年龄段惯用的穿搭方式却不相同,从而也穿出了不同的味道。简直是现在这个季节,时髦精们的必备造型之一~ 70…

    2022年11月24日
  • 明查|美国新冠后遗症患者中有16%症状严重以致无法工作?

    点击进入澎湃新闻全球事实核查平台 速览 – 网传数据比例无权威信源佐证,该比例有可能是结合了美国疾病防控中心和布鲁金斯学会的数据得出,但这两个机构的调研目的和样本都不同…

    2022年11月24日
  • 宽带测速软件(手机宽带测速软件)

    中国联通用户可登录中国联通网上营业厅,选择宽带宽带服务宽带测速,按页面指导进行测速,测速时建议您直连电脑,如测速结果无法达到签约速率,您可通过中国联通APP,“服务报障在线报障”进…

    2022年11月22日
  • 鬼的图片(鬼的图片 头像)

    今天小编给各位分享鬼的图片的知识,其中也会对鬼的图片 头像进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧! 鬼长什么样子呀?(发图片来) 鬼很可怕的,要裁图片…

    2022年11月22日
  • 马斯克凌晨一点半晒“代码审查”现场,编排他的段子比疯狂星期四还多

    梦晨 Pine 发自 凹非寺 量子位 | 公众号 QbitAI 每一个真正会写代码的人,请在下午2点到总部10层报到。 每一个真正会写代码的人,请在下午2点到总部10层报到。 马斯…

    2022年11月21日
  • 淘宝运营数据分析的3个指标解析(运营数据分析怎么做)

    我们知道淘宝运营工作中对于数据的分析与整理是很重要的,这些工作乍一听可能比较难,但是也有一些相关的技巧可以让我们能够有效的找出对我们有用的数据,这样我们也能够更加直观的看出我们店铺…

    2022年11月20日
  • 展字的各种写法图片(展笔顺什么意思)

    昨天我们讲解了上展下收,今天我们讲解上收下展,看过昨天文章内容的朋友,对今天所讲的内容应该不难理解。 结构八:上收下展。 简单来说就是上边部分的笔画收缩,下边部分笔画舒展。详细讲解…

    2022年11月19日

联系我们

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