2010-03-31 4 views
1

J'utilise OpenOffice uno api pour parcourir tout le texte du document writer. Pour itérer sur des tables de texte, j'utilise la méthode getCellNames() de l'interface XTextTable. Comment je pouvais détecter les cellules fusionnées et divisées. Je veux exporter la table en html, donc je devrais calculer colspan et rowspan.OpenOffice iterating via TextTable détecter les cellules fusionnées et fractionnées (calculer colspan et rowspan)

Je vous serais reconnaissant des suggestions ... Je suis d'idées :(

Répondre

2

Enfin, j'ai la réponse (en fait colspan partie). Ce code est laid, je sais, mais un jour je vais refactoriser il;)

public class Cell 
{ 
    private List<string> _text = new List<string>(); 
    public List<string> Text 
    { 
     get { return _text; } 
     set { _text = value; } 
    } 
    public int ColSpan { get; set; } 
    public int RowSpan { get; set; } 

    public Cell(int colSpan, int rowSpan, List<string> text) 
    { 
     ColSpan = colSpan; 
     RowSpan = rowSpan; 
     _text = text; 
    } 

    public Cell(int colSpan, int rowSpan) 
    { 
     ColSpan = colSpan; 
     RowSpan = rowSpan; 
    } 
} 

et voilà la méthode d'analyse de table

public static List<List<Cell>> ParseTable(XTextTable table) 
    { 
     XTableRows rows = table.getRows() as XTableRows; 
     int rowCount = rows.getCount(); 
     int sum = GetTableColumnRelativeSum(table); 

     // Temprorary store for column count of each row 
     int[] colCounts = new int[rowCount]; 

     List<List<int>> matrix = new List<List<int>>(rowCount); 
     for (int i = 0; i < rowCount; i++) 
      matrix.Add(new List<int>()); 

     // Get column count for each row 
     int maxColCount = 0; 
     for (int rowNo = 0; rowNo < rowCount; rowNo++) 
     { 
      TableColumnSeparator[] sep = GetTableRowSeperators(rows, rowNo); 
      colCounts[rowNo] = sep.Length + 1; 

      if (maxColCount < colCounts[rowNo]) 
       maxColCount = colCounts[rowNo]; 

      for (int j = 0; j < sep.Length; j++) 
       matrix[rowNo].Add(sep[j].Position); 

      matrix[rowNo].Add(sum); 
     } 

     int[] curIndex = new int[rowCount]; 
     List<List<Cell>> results = new List<List<Cell>>(rowCount); 
     for (int i = 0; i < rowCount; i++) 
      results.Add(new List<Cell>()); 

     int curMinSep = matrix[0][0]; 
     do 
     { 
      curMinSep = matrix[0][curIndex[0]]; 
      for (int i = 0; i < rowCount; i++) 
       if (curMinSep > matrix[i][curIndex[i]]) curMinSep = matrix[i][curIndex[i]]; 

      for (int rowNo = 0; rowNo < rowCount; rowNo++) 
      { 
       int col = curIndex[rowNo]; 
       int lastIdx = results[rowNo].Count - 1; 

       if (curMinSep == matrix[rowNo][col]) 
       { 
        if (colCounts[rowNo] > col + 1) curIndex[rowNo] = col + 1; 

        if (results[rowNo].Count > 0 && 
         results[rowNo][lastIdx].Text.Count < 1 && 
         results[rowNo][lastIdx].ColSpan > 0) 
        { 
         results[rowNo][lastIdx].ColSpan++; 
         results[rowNo][lastIdx].Text = GetCellText(table, rowNo, col); 
        } 
        else 
        { 
         results[rowNo].Add(new Cell(0, 0, GetCellText(table, rowNo, col))); 
        } 
       } 
       else 
       { 
        if (results[rowNo].Count > 0 && 
         results[rowNo][lastIdx].Text.Count < 1) 
        { 
         results[rowNo][lastIdx].ColSpan++; 
        } 
        else 
        { 
         results[rowNo].Add(new Cell(1, 0)); 
        } 
       } 
      } 
     } while (curMinSep < sum); 

     return results; 
    } 

    public static short GetTableColumnRelativeSum(XTextTable rows) 
    { 
     XPropertySet xPropertySet = rows as XPropertySet; 
     short sum = (short)xPropertySet.getPropertyValue("TableColumnRelativeSum").Value; 
     return sum; 
    } 

    public static TableColumnSeparator[] GetTableRowSeperators(XTableRows rows, int rowNo) 
    { 
     XPropertySet rowProperties = rows.getByIndex(rowNo).Value as XPropertySet; 
     TableColumnSeparator[] sep = null; 
     sep = rowProperties.getPropertyValue("TableColumnSeparators").Value as TableColumnSeparator[]; 
     return sep; 
    } 
+0

Salut Darius, coincidently J'essaie la même chose pour les deux derniers jours sans succès. J'ai essayé avec une approche différente en utilisant XTextTable.getCellNames et XTextTable.createCursorByCellName et en naviguant avec le curseur pour trouver les quartiers de la cellule. Mais en raison de très étrange (à mon avis) le comportement de la navigation du curseur à l'intérieur de la table, je n'ai pas réussi. Dans votre code il vous manque la méthode GetTableColumnRelativeSum et GetTableRowSeperators, pourriez-vous publier ces méthodes aussi. – mvladic

+0

@mvladic - partie manquante ajoutée du code ... –

+0

Je ne savais pas que TableColumnSeparators est la propriété TableRow, dans la documentation je trouve seulement cette propriété à faire partie de TextTable http://api.openoffice.org/docs/common/ ref/com/dim/star/text/TextTable.html # TableColumnSeparators – mvladic

Questions connexes