Count number of columns in an OpenDocument Spreadsheet (ODS file)

1.7k views Asked by At

I have created a very simple application in C# which reads an OpenDocument Spreadsheet file using DotNetZipLib and the XmlDocument class. This has been relativity straightforward since formatting and styles are not relevant for my application.

The format includes several elements of interest to this question <table:table-column>, <table:table-row>, <table:table-cell> and <table:covered-table-cell>. The number of column elements does not necessarily correspond to the actual number of columns within the spreadsheet even when column repetition is considered. Likewise each row element contains a differing number of cell elements.

As stated in the OpenDocument specification I have taken into consideration the fact that rows, columns and cells may be repeated. This is working great since the data is being read into the correct cells of my data format.

With my current understanding of the specification it seems that the only way to count the number of columns in the spreadsheet is to enumerate through each row and count the number of cells. Whilst this is relatively easily, it would be convenient to know the column count before filling my data structure.

Is there a way to efficiently determine the number of columns in the spreadsheet without having to consider each row individually?

2

There are 2 answers

0
Lea Hayes On BEST ANSWER

I have come to the realisation that to determine the total number of columns in an OpenDocument spreadsheet, you must first read each row whilst keeping a running count:

int maximumLength = 0;
while (IsReadingRows) {
    var row = ReadNextRow();
    rowList.Add(row);
    maximumLength = Math.Max(maximumLength, row.Length);
}

After rows have been read, and the maximum length is known, add empty cells to each of the read rows:

foreach (var row in rowList)
    while (row.Length < maximumLength)
        row.AddCell();
1
imacat On

I would not suggest you to read and manipulate OpenDocument XML directly. You are suggested to use OpenOffice UNO API or ODF toolkit instead. If you can run OpenOffice on that machine, using OpenOffice UNO API is easier. If you cannot run OpenOffice on that machine, you could use ODF toolkit https://incubator.apache.org/odftoolkit/ .

OpenOffice UNO API with C#: Use OpenOffice Uno CLI with C# to create a spreadsheet

With UNO API I use queryContentCells from XCellRangesQuery for this. http://www.openoffice.org/api/docs/common/ref/com/sun/star/sheet/XCellRangesQuery.html#queryContentCells