--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.streaming.examples;
+
+import java.io.FileOutputStream;
+
+import org.apache.poi.xssf.streaming.SXSSFSheet;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+
+public class Outlining {
+
+ public static void main(String[] args) throws Exception {
+ Outlining o = new Outlining();
+ o.collapseRow();
+ }
+
+ private void collapseRow() throws Exception {
+ SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
+ SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");
+
+ int rowCount = 20;
+ for (int i = 0; i < rowCount; i++) {
+ sheet2.createRow(i);
+ }
+
+ sheet2.groupRow(4, 9);
+ sheet2.groupRow(11, 19);
+
+ sheet2.setRowGroupCollapsed(4, true);
+
+ FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
+ wb2.write(fileOut);
+ fileOut.close();
+ wb2.dispose();
+ }
+}
short _height=-1;
boolean _zHeight = false;
int _outlineLevel = 0; // Outlining level of the row, when outlining is on
-
+ // use Boolean to have a tri-state for on/off/undefined
+ Boolean _hidden;
+ Boolean _collapsed;
+
public SXSSFRow(SXSSFSheet sheet, int initialSize)
{
_sheet=sheet;
void setOutlineLevel(int level){
_outlineLevel = level;
}
+
+ public Boolean getHidden() {
+ return _hidden;
+ }
+
+ public void setHidden(Boolean hidden) {
+ this._hidden = hidden;
+ }
+
+ public Boolean getCollapsed() {
+ return _collapsed;
+ }
+ public void setCollapsed(Boolean collapsed) {
+ this._collapsed = collapsed;
+ }
//begin of interface implementation
public Iterator<Cell> iterator()
{
*/
public void setRowGroupCollapsed(int row, boolean collapse)
{
- //_sh.setRowGroupCollapsed(row, collapse);
- throw new RuntimeException("Not Implemented");
+ if (collapse) {
+ collapseRow(row);
+ } else {
+ //expandRow(rowIndex);
+ throw new RuntimeException("Not Implemented");
+ }
+ }
+
+ /**
+ * @param rowIndex the zero based row index to collapse
+ */
+ private void collapseRow(int rowIndex) {
+ SXSSFRow row = (SXSSFRow) getRow(rowIndex);
+ if(row == null) {
+ throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist.");
+ } else {
+ int startRow = findStartOfRowOutlineGroup(rowIndex);
+
+ // Hide all the columns until the end of the group
+ int lastRow = writeHidden(row, startRow, true);
+ SXSSFRow lastRowObj = (SXSSFRow) getRow(lastRow);
+ if (lastRowObj != null) {
+ lastRowObj.setCollapsed(true);
+ } else {
+ SXSSFRow newRow = (SXSSFRow) createRow(lastRow);
+ newRow.setCollapsed(true);
+ }
+ }
+ }
+
+ /**
+ * @param rowIndex the zero based row index to find from
+ */
+ private int findStartOfRowOutlineGroup(int rowIndex) {
+ // Find the start of the group.
+ Row row = getRow(rowIndex);
+ int level = ((SXSSFRow) row).getOutlineLevel();
+ if(level == 0) {
+ throw new IllegalArgumentException("Outline level is zero for the row (" + rowIndex + ").");
+ }
+ int currentRow = rowIndex;
+ while (getRow(currentRow) != null) {
+ if (((SXSSFRow) getRow(currentRow)).getOutlineLevel() < level)
+ return currentRow + 1;
+ currentRow--;
+ }
+ return currentRow + 1;
+ }
+
+ private int writeHidden(SXSSFRow xRow, int rowIndex, boolean hidden) {
+ int level = xRow.getOutlineLevel();
+ SXSSFRow currRow = (SXSSFRow) getRow(rowIndex);
+
+ while (currRow != null && currRow.getOutlineLevel() >= level) {
+ currRow.setHidden(hidden);
+ rowIndex++;
+ currRow = (SXSSFRow) getRow(rowIndex);
+ }
+ return rowIndex;
}
/**
\r
package org.apache.poi.xssf.streaming;\r
\r
+import java.io.BufferedWriter;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileWriter;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.Writer;\r
+import java.util.Iterator;\r
+\r
import org.apache.poi.ss.usermodel.Cell;\r
import org.apache.poi.ss.usermodel.CellStyle;\r
import org.apache.poi.ss.usermodel.FormulaError;\r
import org.apache.poi.ss.util.CellReference;\r
-import org.apache.xmlbeans.XmlCursor;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;\r
-\r
-import javax.xml.namespace.QName;\r
-import java.io.*;\r
-import java.util.Iterator;\r
\r
/**\r
* Initially copied from BigGridDemo "SpreadsheetWriter".\r
return _numberLastFlushedRow;\r
}\r
\r
+ @Override\r
protected void finalize() throws Throwable {\r
_fd.delete();\r
}\r
if (row.getOutlineLevel() != 0) {\r
_out.write(" outlineLevel=\"" + row.getOutlineLevel() + "\"");\r
}\r
+ if(row.getHidden() != null) {\r
+ _out.write(" hidden=\"" + (row.getHidden() ? "1" : "0") + "\"");\r
+ }\r
+ if(row.getCollapsed() != null) {\r
+ _out.write(" collapsed=\"" + (row.getCollapsed() ? "1" : "0") + "\"");\r
+ }\r
+ \r
_out.write(">\n");\r
this._rownum = rownum;\r
_rowContainedNullCells = false;\r
--- /dev/null
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.poi.xssf.streaming;
+
+import junit.framework.TestCase;
+
+public final class TestOutlining extends TestCase {
+ public void testSetRowGroupCollapsed() throws Exception {
+
+ SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
+ wb2.setCompressTempFiles(true);
+ SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");
+
+ int rowCount = 20;
+ for (int i = 0; i < rowCount; i++) {
+ sheet2.createRow(i);
+ }
+
+ sheet2.groupRow(4, 9);
+ sheet2.groupRow(11, 19);
+
+ sheet2.setRowGroupCollapsed(4, true);
+
+ SXSSFRow r = (SXSSFRow) sheet2.getRow(8);
+ assertTrue(r.getHidden());
+ r = (SXSSFRow) sheet2.getRow(10);
+ assertTrue(r.getCollapsed());
+ r = (SXSSFRow) sheet2.getRow(12);
+ assertNull(r.getHidden());
+ wb2.dispose();
+ }
+
+ public void testSetRowGroupCollapsedError() throws Exception {
+
+ SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
+ wb2.setCompressTempFiles(true);
+ SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");
+
+ int rowCount = 20;
+ for (int i = 0; i < rowCount; i++) {
+ sheet2.createRow(i);
+ }
+
+ sheet2.groupRow(4, 9);
+ sheet2.groupRow(11, 19);
+
+ try {
+ sheet2.setRowGroupCollapsed(3, true);
+ fail("Should fail with an exception");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("row (3)"));
+ }
+
+ try {
+ sheet2.setRowGroupCollapsed(10, true);
+ fail("Should fail with an exception");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("row (10)"));
+ }
+
+ try {
+ sheet2.setRowGroupCollapsed(0, true);
+ fail("Should fail with an exception");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains("row (0)"));
+ }
+
+ try {
+ sheet2.setRowGroupCollapsed(20, true);
+ fail("Should fail with an exception");
+ } catch (IllegalArgumentException e) {
+ assertTrue("Had: " + e.getMessage(),
+ e.getMessage().contains("Row does not exist"));
+ }
+
+ SXSSFRow r = (SXSSFRow) sheet2.getRow(8);
+ assertNotNull(r);
+ assertNull(r.getHidden());
+ r = (SXSSFRow) sheet2.getRow(10);
+ assertNull(r.getCollapsed());
+ r = (SXSSFRow) sheet2.getRow(12);
+ assertNull(r.getHidden());
+ wb2.dispose();
+ }
+}