예제 #1
0
        public void UpdateTable(ReportTable rt, string SearchString)
        {
            Body body = objWordDocument.MainDocumentPart.Document.Body;

            foreach (var table in body.Elements <Table>())
            {
                foreach (var tableRow in table.Elements <TableRow>())
                {
                    var ttr = false;
                    foreach (var tableCell in tableRow.Elements <TableCell>())
                    {
                        var t = tableCell.InnerText;
                        if (t.IndexOf(SearchString) >= 0)
                        {
                            ttr = true;
                            break;
                        }
                    }
                    if (ttr)
                    {
                        table.RemoveChild(tableRow);
                        SetTableParagraph(rt, table);
                    }
                }
            }
        }
예제 #2
0
        public void CreateOpenXMLFile(string filePath, bool IsAddHead = false)
        {
            using (WordprocessingDocument objWordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
            {
                MainDocumentPart objMainDocumentPart = objWordDocument.AddMainDocumentPart();
                objMainDocumentPart.Document = new Document(new Body());
                Body objBody = objMainDocumentPart.Document.Body;
                //创建一些需要用到的样式,如标题3,标题4,在OpenXml里面,这些样式都要自己来创建的
                //ReportExport.CreateParagraphStyle(objWordDocument);
                SectionProperties sectionProperties = new SectionProperties();
                PageSize          pageSize          = new PageSize();
                PageMargin        pageMargin        = new PageMargin();
                Columns           columns           = new Columns()
                {
                    Space = "220"
                };                                                //720
                DocGrid docGrid = new DocGrid()
                {
                    LinePitch = 100
                };                                                  //360
                //创建页面的大小,页距,页面方向一些基本的设置,如A4,B4,Letter,
                //GetPageSetting(PageSize,PageMargin);

                //在这里填充各个Paragraph,与Table,页面上第一级元素就是段落,表格.
                objBody.Append(new Paragraph());
                ReportTable rt = new ReportTable();
                rt.Column = 3;
                rt.Value  = new List <List <string> >();
                rt.Value.Add(new List <string>()
                {
                    "1", "2", "3"
                });
                rt.Value.Add(new List <string>()
                {
                    "2", "3", "4"
                });
                rt.Value.Add(new List <string>()
                {
                    "3", "4", "5"
                });
                rt.Value.Add(new List <string>()
                {
                    "4", "5", "6"
                });
                objBody.Append(GetParagraph(rt, 600));
                objBody.Append(new Paragraph());

                //我会告诉你这里的顺序很重要吗?下面才是把上面那些设置放到Word里去.(大家可以试试把这下面的代码放上面,会不会出现打开openxml文件有误,因为内容有误)
                sectionProperties.Append(pageSize, pageMargin, columns, docGrid);
                objBody.Append(sectionProperties);

                //如果有页眉,在这里添加页眉.
                if (IsAddHead)
                {
                    //添加页面,如果有图片,这个图片和上面添加在objBody方式有点不一样,这里搞了好久.
                    //ReportExport.AddHeader(objMainDocumentPart, image);
                }
                objMainDocumentPart.Document.Save();
            }
        }
예제 #3
0
        public void InsertTable(ReportTable rt, string SearchString)
        {
            Body body = objWordDocument.MainDocumentPart.Document.Body;

            foreach (var paragraph in body.Elements <Paragraph>())
            {
                if (paragraph.InnerText.IndexOf(SearchString) >= 0)
                {
                    var t = paragraph.InnerText;
                    paragraph.Append(GetParagraph(rt, 1000));
                }
            }
        }
예제 #4
0
        private Table GetParagraph(ReportTable reportTable, Int32Value width)
        {
            Table           table   = new Table();
            TableProperties tblProp = new TableProperties(
                new TableBorders(
                    new TopBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4
            },
                    new BottomBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4
            },
                    new LeftBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4
            },
                    new RightBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4
            },
                    new InsideHorizontalBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4
            },
                    new InsideVerticalBorder()
            {
                Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 4
            }
                    )
                );

            tblProp.TableWidth = new TableWidth()
            {
                Width = "0", Type = TableWidthUnitValues.Auto
            };
            TableGrid  tableGrid1  = new TableGrid();
            GridColumn gridColumn1 = new GridColumn()
            {
                Width = width.ToString()
            };
            GridColumn gridColumn2 = new GridColumn()
            {
                Width = width.ToString()
            };
            GridColumn gridColumn3 = new GridColumn()
            {
                Width = width.ToString()
            };

            tableGrid1.Append(gridColumn1);
            tableGrid1.Append(gridColumn2);
            tableGrid1.Append(gridColumn3);

            table.Append(tblProp);
            table.Append(tableGrid1);

            int count = reportTable.Value.Count;
            int cols  = reportTable.Column;
            int j     = 0;

            foreach (List <string> strs in reportTable.Value)
            {
                TableRow row = new TableRow();
                for (int i = 0; i < cols; i++)
                {
                    TableCell           cell = new TableCell();
                    TableCellProperties tableCellProperties = new TableCellProperties();
                    TableCellMargin     margin = new TableCellMargin();
                    margin.LeftMargin = new LeftMargin()
                    {
                        Width = "100", Type = TableWidthUnitValues.Dxa
                    };
                    margin.RightMargin = new RightMargin()
                    {
                        Width = "100", Type = TableWidthUnitValues.Dxa
                    };
                    tableCellProperties.Append(margin);
                    Paragraph par = new Paragraph();
                    Run       run = new Run();
                    if (j == 0 && reportTable.IsHaveColumn)
                    {
                        Shading shading1 = new Shading()
                        {
                            Val = ShadingPatternValues.Clear, Color = "auto", Fill = "AEAAAA", ThemeFill = ThemeColorValues.Background2, ThemeFillShade = "BF"
                        };
                        RunProperties rPr = new RunProperties();
                        tableCellProperties.Append(shading1);
                        rPr.Append(new Bold());
                        run.Append(rPr);
                    }
                    if (strs.Count != cols && i >= strs.Count - 1)
                    {
                        HorizontalMerge verticalMerge = new HorizontalMerge();
                        if (i == strs.Count - 1)
                        {
                            RunProperties rPr = new RunProperties();
                            rPr.Append(new Bold());
                            run.Append(rPr);
                            verticalMerge.Val = MergedCellValues.Restart;
                            run.Append(CreateText(strs[i]));
                        }
                        else
                        {
                            verticalMerge.Val = MergedCellValues.Continue;
                        }
                        tableCellProperties.Append(verticalMerge);
                    }
                    else
                    {
                        run.Append(CreateText(strs[i]));
                    }
                    par.Append(run);
                    cell.Append(tableCellProperties);
                    cell.Append(par);
                    row.Append(cell);
                }
                j++;
                table.Append(row);
            }

            return(table);
        }
예제 #5
0
        private void SetTableParagraph(ReportTable reportTable, Table table)
        {
            int count = reportTable.Value.Count;
            int cols  = reportTable.Column;
            int j     = 0;

            foreach (List <string> strs in reportTable.Value)
            {
                TableRow row = new TableRow();
                for (int i = 0; i < cols; i++)
                {
                    TableCell           cell = new TableCell();
                    TableCellProperties tableCellProperties = new TableCellProperties();
                    TableCellMargin     margin = new TableCellMargin();
                    margin.LeftMargin = new LeftMargin()
                    {
                        Width = "100", Type = TableWidthUnitValues.Dxa
                    };
                    margin.RightMargin = new RightMargin()
                    {
                        Width = "100", Type = TableWidthUnitValues.Dxa
                    };
                    tableCellProperties.Append(margin);
                    Paragraph par = new Paragraph();
                    Run       run = new Run();
                    if (j == 0 && reportTable.IsHaveColumn)
                    {
                        RunProperties rPr = new RunProperties();
                        rPr.Append(new Bold());
                        run.Append(rPr);
                    }
                    if (strs.Count != cols && i >= strs.Count - 1)
                    {
                        HorizontalMerge verticalMerge = new HorizontalMerge();
                        if (i == strs.Count - 1)
                        {
                            RunProperties rPr = new RunProperties();
                            rPr.Append(new Bold());
                            run.Append(rPr);
                            verticalMerge.Val = MergedCellValues.Restart;
                            run.Append(CreateText(strs[i]));
                        }
                        else
                        {
                            verticalMerge.Val = MergedCellValues.Continue;
                        }
                        tableCellProperties.Append(verticalMerge);
                    }
                    else
                    {
                        run.Append(CreateText(strs[i]));
                    }
                    par.Append(run);
                    cell.Append(tableCellProperties);
                    cell.Append(par);
                    row.Append(cell);
                }
                j++;
                table.Append(row);
            }
        }
예제 #6
0
        public static void ReadWordByOpenXml(string path, string SearchString)
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
            {
                Body body = doc.MainDocumentPart.Document.Body;

                foreach (var inst in body.Elements <OpenXmlElement>())
                {
                    if (inst.InnerText.IndexOf(SearchString) > 0)
                    {
                        var t = inst.InnerText;
                        //inst.Parent.
                    }
                }
                foreach (var paragraph in body.Elements <Paragraph>())
                {
                    var t = paragraph.InnerText;
                    if (t.IndexOf("«TableCycle") >= 0)
                    {
                        var         paragraphText = t.Replace("«TableCycle", "");
                        ReportTable rt            = new ReportTable();
                        rt.IsHaveColumn = true;
                        rt.Column       = 3;
                        rt.Value        = new List <List <string> >();
                        rt.Value.Add(new List <string>()
                        {
                            "事件名称", "起始时间", "终止时间"
                        });
                        rt.Value.Add(new List <string>()
                        {
                            "2", "3", "4"
                        });
                        rt.Value.Add(new List <string>()
                        {
                            "3", "4", "5"
                        });
                        rt.Value.Add(new List <string>()
                        {
                            "4", "5", "6"
                        });

                        paragraph.Append(GetParagraph(rt, 1000));
                    }
                    if (t.IndexOf("«Chart") >= 0)
                    {
                        var           picturePath = "doc/ww.png";
                        string        picType     = picturePath.Split('.').Last();
                        ImagePartType imagePartType;
                        ImagePart     imagePart = null;
                        // 通过后缀名判断图片类型, true 表示忽视大小写
                        if (Enum.TryParse <ImagePartType>(picType, true, out imagePartType))
                        {
                            imagePart = doc.MainDocumentPart.AddImagePart(imagePartType);
                        }
                        try
                        {
                            imagePart.FeedData(File.Open(picturePath, FileMode.Open));
                        }
                        catch (Exception er)
                        {
                        }
                        var relationshipId = doc.MainDocumentPart.GetIdOfPart(imagePart);
                        AddImageToBody(paragraph, relationshipId);
                    }
                }

                foreach (var table in body.Elements <Table>())
                {
                    foreach (var tableRow in table.Elements <TableRow>())
                    {
                        var ttr = false;
                        foreach (var tableCell in tableRow.Elements <TableCell>())
                        {
                            var t = tableCell.InnerText;
                            if (t.IndexOf("«TableEvent") >= 0)
                            {
                                ttr = true;
                                break;
                            }
                        }
                        if (ttr)
                        {
                            table.RemoveChild(tableRow);
                            ReportTable rt = new ReportTable();
                            rt.IsHaveColumn = false;
                            rt.Column       = 3;
                            rt.Value        = new List <List <string> >();
                            rt.Value.Add(new List <string>()
                            {
                                "1", "2", "3"
                            });
                            rt.Value.Add(new List <string>()
                            {
                                "2", "3", "4"
                            });
                            rt.Value.Add(new List <string>()
                            {
                                "3", "4", "5"
                            });
                            rt.Value.Add(new List <string>()
                            {
                                "4", "5", "6"
                            });
                            SetTableParagraph(rt, table);
                        }
                    }
                }
                foreach (var table in body.Elements <Table>())
                {
                    foreach (var tableRow in table.Elements <TableRow>())
                    {
                        Console.Write(tableRow.InnerText);
                    }
                }
                foreach (var table in body.Elements <Table>())
                {
                    Console.Write(table.InnerText);
                }
            }
        }