0

I have a requirement where I will be opening a word document (A template from Avery's website), the template is using word tables.

The most manageable way to handle the data would be to add nested tables (Which word does allow). I know that the OpenXML standard also allows for it, but when I use the OpenXML.DocumentFormat it seems to add all my data into a single cell. When I use straight text with no Sub-tables it works fine and I can traverse all of the cells in the template, but when I append a nested table it puts all my data in the first cell (Of the template).

I realize I probably can pull this off with interop but word being installed on the box that hosts the service in not an option, also I would rather stay away from bookmarking or any type of modification of the base document.

        public void GenerateLabels(Stream stream, List<ItemValue> items, int offset)
    {
        using (MemoryStream docStream = new MemoryStream())
        {
            docStream.Write(LabelTemplates.Avery22806PrinttotheEdgeSquareLabels, 0, LabelTemplates.Avery22806PrinttotheEdgeSquareLabels.Length);
            long cellCount = 0;
            int parcount = 0;
            using (WordprocessingDocument document = WordprocessingDocument.Open(docStream, true))
            {
                Body body = document.MainDocumentPart.Document.Body;
                foreach (Table table in body.Descendants<Table>())
                {
                    long rowCount = 0;
                    foreach (TableRow row in table.Descendants<TableRow>())
                    {
                        if (rowCount % 2 == 0)
                        {
                            int processCount = 0;
                            foreach (TableCell cell in row.Descendants<TableCell>())
                            {
                                if (items.Count() <= parcount)
                                {
                                    break;
                                }
                                if (processCount % 2 == 0 && cellCount >= offset)
                                {

                                    cell.Append(CreateBarcodeLabel(items[parcount], document));
                                    cellCount++;
                                    parcount++;
                                }
                                processCount++;
                            }
                        }
                        rowCount++;
                    }
                }

                stream.Write(docStream.ToArray(), 0, docStream.ToArray().Length);
                document.SaveAs("C:\\labels\\labels1.docx");

            }
        }
    }
    private Paragraph CreateBarcodeLabel(ItemValue par, WordprocessingDocument document)
    {
        Table table = new Table();
        List<TableRow> rows = new List<TableRow>();
        rows.Add(new TableRow());
        TableGrid headerRow = new TableGrid(new GridColumn(), new GridColumn());
        TableCell imageCell = new TableCell();
        imageCell.AppendChild(new Paragraph(new Run(new Text("Image Goes here!"))));

        rows[0].Append(new TableCell(new Paragraph(new Run(new Text(par.Catalog.CatalogExternRef)))),imageCell);
        rows.Add(new TableRow());
        TableCell locatorCell = rows[1].AppendChild(new TableCell());
        locatorCell.Append(new Paragraph(new Run(new Text(par.LocatorString))));

        foreach (TableRow row in rows)
        {
            table.AppendChild(row);
        }

        return new Paragraph(new Run(table));
    }

Edit: To clarify, the document opens fine, the formatting is just hosed. it is pushing all the data into one table cell.

Brian K. Burge
  • 114
  • 1
  • 10
  • 1
    Did you try to do it in Word, save the file, then open it in the "OpenXml Productivity Tool" (downloadable from Microsoft.com)? If you reflect over the code for the document, it should show you the way. That tool is the solution to *many* OpenXml problems – Flydog57 Oct 17 '18 at 21:03
  • Does the information in this discussion help: https://stackoverflow.com/q/4485225/3077495? There's also http://openxmldeveloper.org/discussions/development_tools/f/17/p/5368/172693.aspx – Cindy Meister Oct 18 '18 at 09:09

0 Answers0