在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

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

    表格插入圖片文字

    本章節(jié)講述如何在表格中插入圖片和文字,

    Word中的數(shù)據(jù)只有段落和表格;word中表格表示數(shù)據(jù)

    這是單元格內(nèi)容

    代碼如下:

    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 三級結(jié)構構成。那么,向文檔中添加一個 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 }));

    //列寬可以通過設置單元格的寬度實現(xiàn)

    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)));

    }

    鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權歸原作者所有,如有侵權請聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年6月13日 12:09
    下一篇 2022年6月13日 12:10

    相關推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時間:周一至周五,10:30-18:30,節(jié)假日休息