JExcel

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
JExcel
DeveloperTeamDev
Stable release
1.7
Repository
  • {{URL|example.com|optional display text}}Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
Written inJava
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    Operating systemCross-platform
    TypeAPI to access Microsoft Excel format
    LicenseTeamDev[1]
    Websitehttps://www.teamdev.com/jexcel

    JExcel is a library (API) to read, write, display, and modify Excel files with .xls or .xlsx formats. API can be embedded with Java Swing and AWT. [2][3][4]

    JExcel support is discontinued as of May 31, 2020.[5]

    Some features

    [edit | edit source]

    Some main features are as follows:

    • Automate Excel application, workbooks, spreadsheets, etc.
    • Embed workbooks in a Java Swing application as ordinary Swing component
    • Add event listeners to workbooks and spreadsheets
    • Add event handlers to handle the behavior of workbook and spreadsheet events
    • Add native peers to develop custom functionality.[2][3][4]

    Usage

    [edit | edit source]

    Primary usage is handling Excel files through its API.

    Example

    [edit | edit source]

    Sample code for reading/writing workbook attributes, setting password, and saving MS Excel 2003 format, might look like as follows:

    import com.jniwrapper.win32.jexcel.Application;
    import com.jniwrapper.win32.jexcel.FileFormat;
    import com.jniwrapper.win32.jexcel.GenericWorkbook;
    import com.jniwrapper.win32.jexcel.Workbook;
    
    import java.io.File;
    
    /**
     * This sample shows how to read/modify workbook attributes, how to save workbook in Excel 2003 format,
     * and how to reopen workbook.
     *
     * The sample works with MS Excel in non-embedded mode.
     */
    public class WorkbookSample
    {
        public static void main(String[] args) throws Exception
        {
            //Start MS Excel application, crate workbook and make it visible.
            // Application starts invisible and without any workbooks
            Application application = new Application();
            Workbook workbook = application.createWorkbook("Custom title");
    
            printWorkbookAttributes(workbook);
    
            modifyWorkbookAttributes(workbook);
    
            File newFile = new File("Workbook.xls");
            //Save workbook in Excel 2003, to save in Excel 2007 format use FileFormat.OPENXMLWORKBOOK
            // format specificator and *.xlsx extension
            workbook.saveAs(newFile, FileFormat.WORKBOOKNORMAL, true);
    
            File workbookCopy = new File("WorkbookCopy.xls");
            workbook.saveCopyAs(workbookCopy);
    
            //Close workbook saving changes
            workbook.close(true);
    
            //Reopening the workbook
            workbook = application.openWorkbook(newFile, true, "xxx001");
    
            printWorkbookAttributes(workbook);
    
            //Perform cleanup after yourself and close the MS Excel application forcing it to quit
            application.close(true);
        }
    
        /**
         * Prints workbook attributes to console
         * @param workbook - workbook to print information about
         */
        public static void printWorkbookAttributes(GenericWorkbook workbook)
        {
            String fileName = workbook.getFile().getAbsolutePath();
            String name = workbook.getWorkbookName();
            String title = workbook.getTitle();
            String author = workbook.getAuthor();
    
            System.out.println("\n[Workbook Information]");
            System.out.println("File path: " + fileName);
            System.out.println("Name: " + name);
            System.out.println("Title: " + title);
            System.out.println("Author: " + author);
    
            if (workbook.hasPassword())
            {
                System.out.println("The workbook is protected with a password");
            }
            else
            {
                System.out.println("The workbook is not protected with a password");
            }
            if (workbook.isReadOnly())
            {
                System.out.println("Read only mode");
            }
        }
    
        /**
         * Modify workbook title, author and set password
         * @param workbook - workbook to modify attributes
         */
        public static void modifyWorkbookAttributes(GenericWorkbook workbook)
        {
            workbook.setTitle("X-files");
            workbook.setPassword("xxx001");
            workbook.setAuthor("Agent Smith");
        }
    }
    

    [6]

    See also

    [edit | edit source]

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ a b Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    3. ^ a b Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    4. ^ a b Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    5. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    6. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    [edit | edit source]
    • Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value). – the official JExcel page.
    • Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value). - the JExcel Support website containing documentation, release notes and examples.