]> source.dussan.org Git - poi.git/commitdiff
add excel Workbook.setUseR1C1CellReferences(boolean)
authorPJ Fanning <fanningpj@apache.org>
Tue, 1 Feb 2022 11:06:37 +0000 (11:06 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 1 Feb 2022 11:06:37 +0000 (11:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897652 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
poi/src/main/java/org/apache/poi/ss/usermodel/Workbook.java

index 584a1f3f2378b45c7001154e42dca25a3b128c08..3c392cc63cdb84580aa5800f10327a2a958a7caf 100644 (file)
@@ -1359,4 +1359,9 @@ public class SXSSFWorkbook implements Workbook {
     public Boolean usesR1C1CellReferences() {
         return getXSSFWorkbook().usesR1C1CellReferences();
     }
+
+    @Override
+    public void setUseR1C1CellReferences(boolean useR1C1CellReferences) {
+        getXSSFWorkbook().setUseR1C1CellReferences(useR1C1CellReferences);
+    }
 }
index 47e19e29569d9786937c745b99dd7d29295ff155..0fd6eb56a957883ac250b80e5f3c73524f09c04a 100644 (file)
@@ -97,24 +97,7 @@ import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCache;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCaches;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheets;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookProtection;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
 
 /**
  * High level representation of a SpreadsheetML workbook.  This is the first object most users
@@ -1580,6 +1563,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
         return null;
     }
 
+    @Override
+    public void setUseR1C1CellReferences(boolean useR1C1CellReferences) {
+        CTCalcPr calcPr = getCTWorkbook().getCalcPr();
+        if (calcPr == null) calcPr = getCTWorkbook().addNewCalcPr();
+        STRefMode.Enum refMode = useR1C1CellReferences ? R_1_C_1 : A_1;
+        calcPr.setRefMode(refMode);
+    }
+
     private static String getReferencePrintArea(String sheetName, int startC, int endC, int startR, int endR) {
         //windows excel example: Sheet1!$C$3:$E$4
         CellReference colRef = new CellReference(sheetName, startR, startC, true, true);
index 86cd7440fdfe940e562a10c692915bb36357487c..139e19e2fee4fc1fe3079c40ce2bde42292681a3 100644 (file)
@@ -1769,6 +1769,34 @@ public final class HSSFWorkbook extends POIDocument implements Workbook {
         return null;
     }
 
+    @Override
+    public void setUseR1C1CellReferences(boolean useR1C1CellReferences) {
+        for (HSSFSheet hssfSheet : _sheets) {
+
+            InternalSheet internalSheet = hssfSheet.getSheet();
+
+            List<RecordBase> records = internalSheet.getRecords();
+
+            RefModeRecord refModeRecord = null;
+            for (RecordBase record : records) {
+                if (record instanceof RefModeRecord) refModeRecord = (RefModeRecord)record;
+            }
+            if (useR1C1CellReferences) {
+                if (refModeRecord == null) {
+                    refModeRecord = new RefModeRecord();
+                    records.add(records.size() - 1, refModeRecord);
+                }
+                refModeRecord.setMode(RefModeRecord.USE_R1C1_MODE);
+            } else {
+                if (refModeRecord == null) {
+                    refModeRecord = new RefModeRecord();
+                    records.add(records.size() - 1, refModeRecord);
+                }
+                refModeRecord.setMode(RefModeRecord.USE_A1_MODE);
+            }
+        }
+    }
+
     int getNameIndex(String name) {
 
         for (int k = 0; k < names.size(); k++) {
index 041d7a785f1ccf89161cf1b1b30ff953152d731b..3851ed581e20418d38e23d3cc1cae7b03bc79bcf 100644 (file)
@@ -638,4 +638,10 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
      * @since POI 5.2.1
      */
     Boolean usesR1C1CellReferences();
+
+    /**
+     * @param useR1C1CellReferences set to true if you want to configure workbook to use R1C1 cell references (as opposed to A1 cell references).
+     * @since POI 5.2.1
+     */
+    void setUseR1C1CellReferences(boolean useR1C1CellReferences);
 }