1
我使用OpenOffice的UNO API通过作家文档中的所有文本进行迭代。迭代当前我使用XTextTable接口getCellNames()方法的文本表。我如何检测合并和拆分单元格。我想将表格导出为html,所以我应该计算colspan和rowspan。OpenOffice的通过TextTable迭代检测合并和拆分单元格(计算合并单元格和行跨度)
我将不胜感激任何建议...我出出主意:(
我使用OpenOffice的UNO API通过作家文档中的所有文本进行迭代。迭代当前我使用XTextTable接口getCellNames()方法的文本表。我如何检测合并和拆分单元格。我想将表格导出为html,所以我应该计算colspan和rowspan。OpenOffice的通过TextTable迭代检测合并和拆分单元格(计算合并单元格和行跨度)
我将不胜感激任何建议...我出出主意:(
最后,我找到了答案(它实际上是合并单元格的一部分)。这段代码是丑陋的,我知道,但有一天我会重构它;)
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;
}
}
在这里它是表剖析方法
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;
}
大流士嗨,凑巧我想在过去的两天,没有成功,同样的事情。 我用不同的方法尝试使用XTextTable.getCellNames和XTextTable.createCursorByCellName并用光标进行导航以找出单元格的邻域。但由于表格内部光标导航的行为非常奇怪(在我看来),我没有成功。 在你的代码中,你缺少GetTableColumnRelativeSum和GetTableRowSeperators方法,你是否也可以发布这些方法。 – mvladic 2011-03-04 15:00:25
@mvladic - 添加的代码所缺少的一部分...... – 2011-03-04 15:38:29
我不知道TableColumnSeparators是的TableRow财产,在文档中我只发现属性为TextTable http://api.openoffice.org/docs/common/的一部分REF/COM /阳光/明星/文本/ TextTable.html#TableColumnSeparators – mvladic 2011-03-04 15:45:01