private void ProcessPositionTable(Table table, PublicServant currentDeclarant) { var rows = table.Descendants <TableRow>().ToList(); var cells = rows[0].Descendants <TableCell>().ToList(); currentDeclarant.Occupation = cells[1].InnerText; }
private void ProcessIncomeTable(Table table, PublicServant person) { var rows = table.Descendants <TableRow>().ToList().Skip(1); foreach (var row in rows) { var cells = row.Descendants <TableCell>().ToList(); var incomeType = cells[1].InnerText.OnlyRussianLowercase(); if (incomeType.StartsWith("декларированныйгодовойдоход")) { var incomeRaw = cells[2].InnerText.Trim(); if (incomeRaw == "") { continue; } person.DeclaredYearlyIncomeRaw = incomeRaw; person.DeclaredYearlyIncome = DataHelper.ParseDeclaredIncome(incomeRaw, false); } else if (DataHelper.IsRelativeInfo(incomeType)) { var incomeRaw = cells[2].InnerText.Trim(); var relationType = DataHelper.ParseRelationType(incomeType, false); var relative = new Relative(); relative.RelationType = relationType; relative.DeclaredYearlyIncomeRaw = incomeRaw; relative.DeclaredYearlyIncome = DataHelper.ParseDeclaredIncome(incomeRaw, false); person.AddRelative(relative); } } }
private void BuildRows() { var tableRows = table.Descendants <DocumentFormat.OpenXml.Wordprocessing.TableRow>(); rows = new List <TableRow>(); foreach (var row in tableRows) { rows.Add(new TableRow(this, row)); } }
private void ParseVehicleTable(Table table, PublicServant person) { var rows = table.Descendants <TableRow>().ToList().Skip(1); string currentVehicleType = ""; foreach (var row in rows) { var cells = row.Descendants <TableCell>().ToList(); var text = cells[0].InnerText; var gridSpan = cells[0].TableCellProperties.GetFirstChild <GridSpan>(); var mergedColsCount = (gridSpan == null) ? 1 : (int)gridSpan.Val; if (mergedColsCount > 1) { currentVehicleType = text; continue; } var textStr = cells[1].InnerText; if (textStr.OnlyRussianLowercase() == "неимеет") { continue; } var ownerStr = cells[2].InnerText; var owners = ownerStr.Split(",").ToList(); foreach (var owner in owners) { var vehicle = new Vehicle(textStr, currentVehicleType); var relationType = DataHelper.ParseRelationType(owner, false); if (DataHelper.IsRelativeInfo(owner)) { var relative = GetPersonRelative(person, relationType); relative.Vehicles.Add(vehicle); } else { person.Vehicles.Add(vehicle); } } } }
public void Test08DownloadWordExportedFile() { long reportId = GetReportByAlias("test:managerReport"); ExportDataInfo res = PostExportRequest(string.Format(@"data/v2/report/export/{0}/word", reportId), HttpStatusCode.OK, new ReportParameters()); Assert.IsNotNullOrEmpty(res.FileHash, "The exported file has been saved to the database."); using ( PlatformHttpRequest request = new PlatformHttpRequest( string.Format(@"data/v2/report/export/download/{0}/sample/word", res.FileHash), PlatformHttpMethod.Get)) { HttpWebResponse response = request.GetResponse(); Assert.IsTrue(response.StatusCode == HttpStatusCode.OK, "We have a {0} returned, expected {1}", response.StatusCode, HttpStatusCode.OK); Assert.AreEqual(response.ContentType, "text/html"); using (Stream stream = response.GetResponseStream()) { Assert.IsNotNull(stream); using (FileStream fileStream = File.Create(WordFile)) { CopyToFile(stream, fileStream); fileStream.Position = 0; using (WordprocessingDocument doc = WordprocessingDocument.Open(fileStream, false)) { Body body = doc.MainDocumentPart.Document.Body; DocumentFormat.OpenXml.Wordprocessing.Table table = body.Descendants <DocumentFormat.OpenXml.Wordprocessing.Table>().FirstOrDefault(); Assert.IsNotNull(table, "The word document hasn't got a table"); var rows = table.Descendants <TableRow>(); Assert.Greater(rows.Count(), 0, "There are no rows in the table"); //Get first row. TableRow row = rows.First(); TableCell cell = row.GetFirstChild <TableCell>(); Assert.AreEqual("Name", cell.FirstChild.InnerText); //Get second row TableRow row1 = rows.Skip(1).First(); TableCell cell1 = row1.GetFirstChild <TableCell>(); Assert.AreEqual("Glenn Uidam", cell1.FirstChild.InnerText); } } } } }
private static void full_table( DocumentFormat.OpenXml.Packaging.WordprocessingDocument document, DocumentFormat.OpenXml.Wordprocessing.Table table, Dictionary <String, String> marks) { //int cell_count = 0; foreach (var cell in table.Descendants <TableCell>()) { //Console.WriteLine(" " + ++cell_count + " cell value: " + cell); foreach (var item in cell) { if (item.GetType() == typeof(DocumentFormat.OpenXml.Wordprocessing.Table)) { full_table(document, (DocumentFormat.OpenXml.Wordprocessing.Table)item, marks); } else if (item.GetType() == typeof(DocumentFormat.OpenXml.Wordprocessing.Paragraph)) { foreach (var mrk in marks) { if (cell.InnerText.Contains(labelText)) { ImagePart imagePart = document.MainDocumentPart.AddImagePart(ImagePartType.Jpeg); using (FileStream stream = new FileStream(imageFile, FileMode.Open)) { imagePart.FeedData(stream); } cell.RemoveAllChildren(); addImageToCell(cell, document.MainDocumentPart.GetIdOfPart(imagePart)); } else if (cell.InnerText.Contains(mrk.Key)) { String str = cell.InnerText; ((DocumentFormat.OpenXml.Wordprocessing.Paragraph)item).RemoveAllChildren(); ((DocumentFormat.OpenXml.Wordprocessing.Paragraph)item).Append(new Run(new Text(str.Replace(mrk.Key, mrk.Value)))); } } } } } }
private void ParseRealEstateTable(Table table, PublicServant person, string ownTypeByColumn) { var rows = table.Descendants <TableRow>().ToList().Skip(1); string currentRealEstateType = ""; foreach (var row in rows) { var cells = row.Descendants <TableCell>().ToList(); var gridSpan = cells[0].TableCellProperties.GetFirstChild <GridSpan>(); var mergedColsCount = (gridSpan == null) ? 1 : (int)gridSpan.Val; var text = cells[0].InnerText; if (mergedColsCount > 1) { currentRealEstateType = text; continue; } var textStr = cells[1].InnerText; if (textStr.OnlyRussianLowercase() == "неимеет") { continue; } var areaStr = cells[2].InnerText; var countryStr = cells[3].InnerText; var ownerStr = cells[4].InnerText; var owners = ownerStr.Split(",").ToList(); var shares = owners.Where(x => x.Contains(" доля") || x.Contains(" доли")).ToList(); owners = owners.Where(x => !(x.Contains(" доля") || x.Contains(" доли"))).ToList(); if (shares.Count != owners.Count && shares.Count > 0) { throw new SmartParserException("shares.Count != owners.Count in SovetFederaciiDocxScheme"); } if (shares.Count < owners.Count) { shares = Enumerable.Repeat <string>(null, owners.Count - shares.Count).ToList(); } var zippedOwners = owners.Zip(shares); foreach (var pair in zippedOwners) { var owner = pair.First; var share = pair.Second; var realEstateProperty = new RealEstateProperty { Text = textStr, square = DataHelper.ParseSquare(areaStr), type_raw = currentRealEstateType, square_raw = ParserBase.NormalizeRawDecimalForTest(areaStr), country_raw = DataHelper.ParseCountry(countryStr), own_type_by_column = ownTypeByColumn }; if (share != default) { realEstateProperty.own_type_raw = share; } var relationType = DataHelper.ParseRelationType(owner, false); if (DataHelper.IsRelativeInfo(owner)) { var relative = GetPersonRelative(person, relationType); relative.RealEstateProperties.Add(realEstateProperty); } else { person.RealEstateProperties.Add(realEstateProperty); } } } }
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"); } }
public void InsertTableRow(Table table, TableRow tableRow, int rowIndex) { //add new row to table, after last row in table table.Descendants <TableRow>().ElementAt(rowIndex).InsertAfterSelf(tableRow); }
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"); } }