private static Word.TableProperties GetTableProperties(Table table)
        {
            Word.TableWidth widthProps = new Word.TableWidth();

            switch (table.WidthUnits)
            {
                case ElementWidth.Absolute:
                    widthProps.Width = (table.Width * 20).ToString();
                    widthProps.Type = Word.TableWidthUnitValues.Dxa;
                    break;
                case ElementWidth.Percentage:
                    widthProps.Width = (table.Width * 50).ToString();
                    widthProps.Type = Word.TableWidthUnitValues.Pct;
                    break;
                default:
                    break;
            }

            Word.BorderValues border;

            switch (table.BorderType)
            {
                case TableBorders.None:
                    border = Word.BorderValues.None;
                    break;
                case TableBorders.Single:
                    border = Word.BorderValues.Single;
                    break;
                default:
                    border = Word.BorderValues.None;
                    break;
            }

            Word.TableProperties tableProps = new Word.TableProperties()
            {
                TableWidth = widthProps
                ,
                TableBorders = new Word.TableBorders()
                {

                    LeftBorder = new Word.LeftBorder()
                    {
                        Val = border
                    }
                    ,
                    RightBorder = new Word.RightBorder()
                    {
                        Val = border
                    }
                    ,
                    TopBorder = new Word.TopBorder()
                    {
                        Val = border
                    }
                    ,
                    BottomBorder = new Word.BottomBorder()
                    {
                        Val = border
                    }
                    ,
                    InsideHorizontalBorder = new Word.InsideHorizontalBorder()
                    {
                        Val = border
                    }
                    ,
                    InsideVerticalBorder = new Word.InsideVerticalBorder()
                    {
                        Val = border
                    }

                }
            };
            return tableProps;
        }
        void ImportProjectsAndMilestones(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile spreadsheetFileName)
        {
            ArrayList cellText = new ArrayList();

            // Create a Word table.
            Word.Table tbl = new Word.Table();
            Word.TableProperties tblPr = new Word.TableProperties();
            Word.TableStyle tblStyle = new Word.TableStyle();
            tblStyle.Val = "LightShading-Accent1";
            tblPr.AppendChild(tblStyle);

            Word.TableWidth tblW = new Word.TableWidth();
            tblW.Width = "5000";
            tblW.Type = Word.TableWidthUnitValues.Pct;
            tblPr.Append(tblW);
            tbl.AppendChild(tblPr);
            byte[] byteArray = spreadsheetFileName.OpenBinary();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);

                using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true))
                {
                    WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
                    WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet, "Sheet1");

                    Excel.SheetData sheetData =
                       worksheetPart.Worksheet.GetFirstChild<Excel.SheetData>();

                    foreach (Excel.Row r in sheetData)
                    {
                        foreach (Excel.Cell c in r)
                        {
                            cellText.Add(XLGetCellValue(c, workbookPart));
                        }
                        Word.TableRow tr = CreateRow(cellText);
                        tbl.Append(tr);
                        cellText = new ArrayList();
                    }
                }
            }
            // Swap out the content control for the SmartArt.
            OpenXmlElement parent = sdt.Parent;
            parent.InsertAfter(tbl, sdt);
            sdt.Remove();
        }
Example #3
0
        public Wordprocessing.Table CreateTable(int columnsCount)
        {
            Wordprocessing.Table table1 = new Wordprocessing.Table();
            WorkbookPart workbookPart = _spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = worksheetPart.Worksheet.Elements<DocumentFormat.OpenXml.Spreadsheet.SheetData>().First();
            //Задание свойств таблицы
            Wordprocessing.TableProperties tableProperties1 = new Wordprocessing.TableProperties();
            Wordprocessing.TableStyle tableStyle1 = new Wordprocessing.TableStyle() { Val = "TableGrid" };
            Wordprocessing.TableWidth tableWidth1 = new Wordprocessing.TableWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
            Wordprocessing.TableBorders tableBorders1 = new Wordprocessing.TableBorders();
            Wordprocessing.TopBorder topBorder1 = new Wordprocessing.TopBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U };
            Wordprocessing.LeftBorder leftBorder1 = new Wordprocessing.LeftBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U };
            Wordprocessing.BottomBorder bottomBorder1 = new Wordprocessing.BottomBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U };
            Wordprocessing.RightBorder rightBorder1 = new Wordprocessing.RightBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U };
            Wordprocessing.InsideHorizontalBorder insideHorizontalBorder1 = new Wordprocessing.InsideHorizontalBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U };
            Wordprocessing.InsideVerticalBorder insideVerticalBorder1 = new Wordprocessing.InsideVerticalBorder() { Val = Wordprocessing.BorderValues.Single, Color = "000000", Size = (int)4U, Space = (int)0U };
            tableBorders1.Append(topBorder1);
            tableBorders1.Append(leftBorder1);
            tableBorders1.Append(bottomBorder1);
            tableBorders1.Append(rightBorder1);
            tableBorders1.Append(insideHorizontalBorder1);
            tableBorders1.Append(insideVerticalBorder1);
            Wordprocessing.TableLook tableLook1 = new Wordprocessing.TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true };
            tableProperties1.Append(tableStyle1);
            tableProperties1.Append(tableWidth1);
            tableProperties1.Append(tableBorders1);
            tableProperties1.Append(tableLook1);
            table1.Append(tableProperties1);

            //Создание структуры таблицы
            Wordprocessing.TableGrid tableGrid1 = new Wordprocessing.TableGrid();
            Wordprocessing.GridColumn gridColumn = new Wordprocessing.GridColumn() { Width = "9571" };
            tableGrid1.Append(gridColumn);
            table1.Append(tableGrid1);

                //Добавление информации из Excel
                int j = 0;
                foreach (Spreadsheet.Row r in sheetData.Elements<Spreadsheet.Row>())
                {
                    j = 0;
                    Wordprocessing.TableRow tableRow1 = new Wordprocessing.TableRow();
                    foreach (Spreadsheet.Cell c in r.Elements<Spreadsheet.Cell>())
                    {
                        j++;
                        string value = c.InnerText;
                        if (c.DataType!= null && c.DataType.Value == CellValues.SharedString)
                        {
                                 var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                                 if (stringTable != null) value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
                        }

                        TableRowExtension.AddCell(tableRow1,value);
                    }
                    for (int i=j; i < columnsCount; i++) TableRowExtension.AddCell(tableRow1, "");
                    table1.Append(tableRow1);
                }

                Wordprocessing.Table table2 = doptable(table1);
                Wordprocessing.Table table3 = doptable2(table2);

             return table3;
        }