Пример #1
0
        public TableRow Replace(TableRow tableRow, Dictionary <string, string> ReplaceItems)
        {
            TableRow targetTableRow = (TableRow)tableRow.Clone();

            foreach (KeyValuePair <string, string> keyValuePair in ReplaceItems)
            {
                string SearchString  = keyValuePair.Key;
                string ReplaceString = keyValuePair.Value.Replace("\r\n", "\n");  // 解決換行問題

                #region 字串替代
                foreach (Paragraph para in targetTableRow.Descendants <Paragraph>())
                {
                    if (para.InnerText.Contains(SearchString))
                    {
                        Run newRun = (Run)para.Descendants <Run>().First(r => r.InnerText.Contains("#")).Clone();

                        newRun.Descendants <Text>().First().Text = para.InnerText.Replace(SearchString, ReplaceString);

                        // 處理換行
                        newRun.InnerXml = newRun.InnerXml.Replace("\r\n", "</w:t><w:br/><w:t>");

                        // 處理\t轉成Tab
                        newRun.InnerXml = newRun.InnerXml.Replace("\t", "   ");

                        para.RemoveAllChildren <Run>();
                        para.AppendChild <Run>(newRun);
                    }
                }
                #endregion
            }

            return(targetTableRow);
        }
Пример #2
0
        private void AddCells()
        {
            var tableCells = tableRow.Descendants <DocumentFormat.OpenXml.Wordprocessing.TableCell>().ToArray();

            cells = new TableCell[table.NumberOfColumns];

            if (tableCells.Count() > 0)
            {
                for (int i = 0; i < table.NumberOfColumns; i++)
                {
                    cells[i] = new TableCell(this, tableCells[i]);
                }
            }
            else
            {
                for (int i = 0; i < table.NumberOfColumns; i++)
                {
                    var tableCell = tableRow.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.TableCell());

                    var tableCellProperties = tableCell.GetOrCreate <TableCellProperties>(true);
                    var tableCellWidth      = tableCellProperties.GetOrCreate <TableCellWidth>();
                    tableCellWidth.Width = table.ColumnWidths[i];
                    tableCellWidth.Type  = TableWidthUnitValues.Dxa;

                    var cell = new TableCell(this, tableCell);
                    cell.AddParagraph();

                    cells[i] = cell;
                }
            }
        }
Пример #3
0
 private static void ModifyWordRowTextContent(OXW.TableRow headerRowTemplate, string txt)
 {
     if (null != headerRowTemplate)
     {
         var cells = headerRowTemplate.Descendants <OXW.TableCell>();
         if (null != cells)
         {
             foreach (var cell in cells)
             {
                 ModifyWordCellTextContent(cell, txt);
             }
         }
     }
 }
Пример #4
0
        private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXW.Table)
            {
                OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table;

                OXW.TableRow headerRowTemplate  = table.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow;
                OXW.TableRow contentRowTemplate = table.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow;

                #region Column number management
                OXW.TableGrid tablegrid = table.Descendants <OXW.TableGrid>().FirstOrDefault();
                if (null != tablegrid)
                {
                    List <OXW.GridColumn> columns = tablegrid.Descendants <OXW.GridColumn>().ToList();
                    if (null != columns && content.NbColumns != columns.Count)
                    {
                        if (content.NbColumns < columns.Count)
                        {
                            for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--)
                            {
                                tablegrid.RemoveChild <OXW.GridColumn>(columns[i]);
                            }
                        }
                        else
                        {
                            for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++)
                            {
                                tablegrid.AppendChild <OXW.GridColumn>(new OXW.GridColumn()
                                {
                                    Width = "1000"
                                });
                            }
                        }
                    }
                }
                #endregion Column number management

                ModifyWordRowTextContent(headerRowTemplate, string.Empty);
                ModifyWordRowTextContent(contentRowTemplate, string.Empty);

                int idx   = 0;
                int nbrow = 0;
                List <OXW.TableCell> headerCells  = headerRowTemplate.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList();
                List <OXW.TableCell> contentCells = contentRowTemplate.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList();
                headerRowTemplate.RemoveAllChildren <OXW.TableCell>();
                OXW.TableRow row = headerRowTemplate;
                int          headerCellsCount  = headerCells.Count;
                int          contentCellsCount = headerCells.Count;

                table.RemoveAllChildren <OXW.TableRow>();
                foreach (var item in content.Data)
                {
                    if (null != item)
                    {
                        OXW.TableCell cell = null;
                        if (content.HasColumnHeaders && 0 == nbrow)
                        {
                            cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell;
                        }
                        else
                        {
                            cell = contentCells[idx % contentCellsCount].CloneNode(true) as OXW.TableCell;
                        }
                        ModifyWordCellTextContent(cell, item);
                        row.Append(cell);
                    }

                    idx = ++idx % content.NbColumns;
                    if (0 == idx)
                    {
                        if (null != row)
                        {
                            table.Append(row);
                            nbrow++;
                        }
                        row = contentRowTemplate.CloneNode(true) as OXW.TableRow;
                        row.RemoveAllChildren <OXW.TableCell>();
                    }
                }
                var blockSdt = block.Ancestors <OXW.SdtBlock>().First();
                blockSdt.Parent.ReplaceChild(table, blockSdt);
            }
            else
            {
                LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", null != block ? block.GetType().ToString() : "null");
            }
        }
Пример #5
0
        private static void UpdateWordBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXW.Table)
            {
                OXW.Table table = ((OXW.Table)block).CloneNode(true) as OXW.Table;

                OXW.TableRow headerRowTemplate  = table?.Descendants <OXW.TableRow>().First().CloneNode(true) as OXW.TableRow;
                OXW.TableRow contentRowTemplate = table?.Descendants <OXW.TableRow>().Skip(1).First().CloneNode(true) as OXW.TableRow;

                #region Column number management
                OXW.TableGrid         tablegrid = table?.Descendants <OXW.TableGrid>().FirstOrDefault();
                List <OXW.GridColumn> columns   = tablegrid?.Descendants <OXW.GridColumn>().ToList();
                if (columns != null && content.NbColumns != columns.Count)
                {
                    if (content.NbColumns < columns.Count)
                    {
                        for (int i = columns.Count - 1, lim = content.NbColumns - 1; i > lim; i--)
                        {
                            tablegrid.RemoveChild(columns[i]);
                        }
                    }
                    else
                    {
                        for (int i = 0, lim = content.NbColumns - columns.Count; i < lim; i++)
                        {
                            tablegrid.AppendChild(new OXW.GridColumn()
                            {
                                Width = "200"
                            });
                        }
                    }
                }

                #endregion Column number management

                ModifyWordRowTextContent(headerRowTemplate, string.Empty, string.Empty, string.Empty);
                ModifyWordRowTextContent(contentRowTemplate, string.Empty, string.Empty, string.Empty);

                int idx   = 0;
                int nbrow = 0;
                List <OXW.TableCell> headerCells  = headerRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList();
                List <OXW.TableCell> contentCells = contentRowTemplate?.Descendants <OXW.TableCell>().Select(_ => _.CloneNode(true) as OXW.TableCell).ToList();
                headerRowTemplate?.RemoveAllChildren <OXW.TableCell>();
                OXW.TableRow row = headerRowTemplate;
                if (headerCells != null)
                {
                    int headerCellsCount  = headerCells.Count;
                    int contentCellsCount = headerCells.Count;

                    table.RemoveAllChildren <OXW.TableRow>();
                    for (int i = 0; i < content.Data.Count(); i++)
                    {
                        var item = content.Data.ToArray()[i];
                        if (null != item)
                        {
                            OXW.TableCell cell;
                            if (content.HasColumnHeaders && 0 == nbrow)
                            {
                                cell = headerCells[idx % headerCellsCount].CloneNode(true) as OXW.TableCell;
                            }
                            else
                            {
                                cell = contentCells?[idx % contentCellsCount].CloneNode(true) as OXW.TableCell;
                            }

                            string txtColor = string.Empty;
                            string effect   = string.Empty;
                            if (content.HasCellsAttributes())
                            {
                                CellAttributes attributes = content.CellsAttributes.FirstOrDefault(a => a.Index == i);
                                if (attributes != null)
                                {
                                    OXW.TableCellProperties tcp = new OXW.TableCellProperties(
                                        new OXW.TableCellWidth {
                                        Type = OXW.TableWidthUnitValues.Auto,
                                    }
                                        );
                                    // Create the Shading object
                                    OXW.Shading shading =
                                        new OXW.Shading()
                                    {
                                        Color = "auto",
                                        Fill  = ColorTranslator.ToHtml(attributes.BackgroundColor),
                                        Val   = OXW.ShadingPatternValues.Clear
                                    };
                                    // Add the Shading object to the TableCellProperties object
                                    tcp.Append(shading);
                                    // Add the TableCellProperties object to the TableCell object
                                    cell?.Append(tcp);

                                    txtColor = $"{attributes.FontColor.R:X2}{attributes.FontColor.G:X2}{attributes.FontColor.B:X2}";
                                    effect   = attributes.Effect;
                                }
                            }

                            ModifyWordCellTextContent(cell, item, txtColor, effect);
                            row?.Append(cell);
                        }

                        idx = ++idx % content.NbColumns;
                        if (0 != idx)
                        {
                            continue;
                        }
                        if (null != row)
                        {
                            table.Append(row);
                            nbrow++;
                        }
                        row = contentRowTemplate?.CloneNode(true) as OXW.TableRow;
                        row?.RemoveAllChildren <OXW.TableCell>();
                    }
                }
                var blockSdtAncestor = block.Ancestors <OXW.SdtBlock>();
                if (0 != blockSdtAncestor.ToList().Count)
                {
                    // case table is in a content control
                    var blockStd = block.Ancestors <OXW.SdtBlock>().First();
                    blockStd.Parent.ReplaceChild(table, blockStd);
                }
                else
                {
                    // case table is directly in the document
                    var blockStd = block;
                    blockStd.Parent.ReplaceChild(table, blockStd);
                }
            }
            else
            {
                LogHelper.Instance.LogErrorFormat("Impossible to load data in Table block with a block source of type \"{0}\"", block?.GetType().ToString() ?? "null");
            }
        }