Java to create, edit, delete Excel named ranges

Excel named range, i.e. a range of cells for the specified name, a reference to a range of cells, such as may be used in the formula refer to the specified area named formulas operation. When you create a named range, can be created for the entire workbook that workbook.getNameRanges () add (), the name of the entire region workbook valid when referring to; Or be created for the specified worksheet that sheet.getNames ( ) .add (), named in reference to the area where the only valid worksheet. In addition, Excel already named area may rename, resetting cell reference region set to hide or display (note here that the named area set name hide or show, not named range of cells, when you set Hide name, can protect the purpose of reference data sources), and delete files. The method of operation will now be described by code examples.

Runtime environment: the Java, IDEA, jdk1.8.0, without having to install Microsoft Excel

Tools: as Free Spire.XLS for the Java (free version)

Jar acquisition and import: by downloading the official website , and extract the jar file lib folder under the import java program.

Import jar reference to the following effects:

 

Java code examples

1. Create a named range

   1.1 Global Named Range

Import com.spire.xls *. ;
 Import com.spire.xls.core.INamedRange; 

public  class NamedRange1 {
     public  static  void main (String [] args) {
         // create an instance, the test document loading 
        the Workbook WB = new new the Workbook () ; 
        wb.loadFromFile ( "test.xlsx" ); 

        // get the first one worksheet 
        worksheet sheet1 = wb.getWorksheets () gET (0. );
         // create a global name 
        . INamedRange namedRange1 = wb.getNameRanges () add ( "Range1, and" ); 
        namedRange1.setRefersToRange (sheet1.getCellRange ( "C2: a C3" ));
        NamedRange2 INamedRange = wb.getNameRanges () the Add ( "Range2." ); 
        NamedRange2.setRefersToRange (sheet1.getCellRange ( "C4: C5" )); 

        // get the first sheet 2, the formula in cell referenced named range 
        Worksheet = wb.getWorksheets Sheet2 () GET (. 1. ); 
        sheet2.getCellRange ( "Bl") setFormula ( "the SUM = (Range1, and, Range2)." ); 

       // save file 
        wb.saveToFile ( "NamadRange1.xlsx" , ExcelVersion .Version2013); 
    } 
}

Global named ranges to create the effect of:

 

1.2 local area named

Import com.spire.xls *. ;
 Import com.spire.xls.core.INamedRange; 

public  class NamedRange2 {
     public  static  void main (String [] args) {
         // create an instance, and load test documentation 
        Workbook wb = new new Workbook ( ); 
        wb.loadFromFile ( "test.xlsx" ); 

        // get the first one worksheet 
        worksheet sheet = wb.getWorksheets () gET (0. ); 

        // create a local named range 
        INamedRange namedRange1 = sheet.getNames (). the Add ( "Range1, and" ); 
        namedRange1.setRefersToRange (sheet.getCellRange ( "C2: a C3" ));
        NamedRange2 INamedRange = sheet.getNames () the Add ( "Range2." ); 
        NamedRange2.setRefersToRange (sheet.getCellRange ( "C4: C5" )); 

        // references a named area in the formula 
        . Sheet.getCellRange ( "C6") setFormula ( "the SUM = (Range1, and, Range2)" ); 

        // save file 
        wb.saveToFile ( "NamedRange2.xlsx" , ExcelVersion.Version2013); 
    } 
}

Local named ranges to create the effect of:

 

2. To edit an existing named range

Import com.spire.xls *. ;
 Import com.spire.xls.core.INamedRange; 

public  class ModifyNamedRange {
     public  static  void main (String [] args) {
         // create an instance, the test document loading 
        the Workbook WB = new new the Workbook () ; 
        wb.loadFromFile ( "NamedRange1.xlsx" ); 

        // get the worksheet 
        worksheet sheet = wb.getWorksheets () gET (0. );
         // get the global named range 
        INamedRange namedRange = wb.getNameRanges () getByName ( "Range1. " );
         // Get the local area name (name of the local area related operations)
         //NamedRange1 = sheet.getNames INamedRange () the getByName ( "Range1, and");. 
        NamedRange.setName ( "NewRange"); // modified region name 
        namedRange.setRefersToRange (sheet.getCellRange ( "C2")); // modify cell reference regional 
        namedRange.setVisible ( false ); // hide the name of a named range
         // wb.getNameRanges () the Remove ( "Range2");. // delete the named area 

        // save the document 
        wb.saveToFile ( "ModifyNamedRange.xlsx" ); 
    } 
}

Editing effects:

 

(This article End)

Guess you like

Origin www.cnblogs.com/Yesi/p/12517045.html