Results 1 to 10 of 10
  1. #1
    Chris_X is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default Excel: How to sort a range?

    Hello, all. I have a bit of a problem.

    I'm using Eclipse to make up a project which involves sorting a certain range in an excel file. I am using JXL and JExcel. I have this snippet:

    import java.io.File;
    import java.io.IOException;
    import java.util.Locale;

    import jxl.*;
    import jxl.format.UnderlineStyle;
    import jxl.write.Formula;
    import jxl.write.Label;
    import jxl.write.Number;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableCell;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
    import jxl.Cell;
    import jxl.CellType;
    import jxl.Sheet;
    import jxl.Workbook;
    import java.util.Date;
    import jxl.read.biff.BiffException;
    import com.jniwrapper.win32.jexcel.*;

    public void sort (WritableSheet sheet) throws ExcelException{
    Application application = new Application();
    File xlsFile = new File("C:\\Workbook.xls");
    Workbook workbook = application.openWorkbook(xlsFile, true, "password");
    }

    And then I get the error of "Type mismatch: Cannot convert Workbook to Workbook"

    I am using the tutorial as shown in this page, but even if I do it word-by-word, I can't seem to get it right.

    JExcel Programmer's Guide

    A solution would be most appreciated.
    Last edited by Chris_X; 11-22-2010 at 11:48 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    What is Application?
    What class does openWorkbook return?

  3. #3
    Chris_X is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    According to the link...

    ------------

    Chapter 4. Excel Automation Basics

    All these classes reside in the com.jniwrapper.win32.jexcel package.
    4.1. Creating a New Application

    Your work with Excel starts with the creation of a new Excel application. The JExcel library includes the Application class that represents an Excel application and provides related functionality. A new application is created by calling the Application constructor:

    Application application = new Application();

    -----------

    I'm really a newbie about this, so perhaps you could help explain? Help is much appreciated.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    So what class does openWorkbook return?

    On top of that I've never used that architecture. I've only ever read in and written to a Sheet. I've never used Application.

    Right, looking up the API for it, you are using two different things. You should either be using the Application side of things (which is intended by the looks of it for doing "general Excel application tasks"), or using jxl, which is for reading and writing Excel files.

  5. #5
    Chris_X is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    According to the link you showed, openWorkbook would return an opened workbook...

    Other than that, I cannot check because at the last line, I always get the error I mentioned when I try to save.
    Last edited by Chris_X; 11-22-2010 at 12:18 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    The link I posted goes to the API for Application.
    openWorkbook() returns a com.jniwrapper.win32.jexcel Workbook, and not a jxl.Workbook. This is what I mean by the two packages not looking (to me) like they were intended to work together.

    ETA: You want the former, if you are working through that tutorial...since it's about JExcel and mimicing Excel functionality in Swing. Get rid of the jxl imports.
    Last edited by Tolls; 11-22-2010 at 12:30 PM.

  7. #7
    Chris_X is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    So, how do I cast it to a com.jniwrapper.win32.jexcel Workbook? I'd appreciate it if you could help me. I'm not quite the expert. Thanks.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    You can't.
    Don't mix the two.
    As the intro for Chapter 4 says:
    This chapter discusses the functionality of the following JExcel classes:

    * Application
    * Workbook
    * Worksheet
    * Window

    All these classes reside in the com.jniwrapper.win32.jexcel package.
    Do not use the jxl stuff.

  9. #9
    Chris_X is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    So I have to revamp my code with using JExcel only if I want to do it?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Well, you do have two different frameworks here.
    One intended to mimic Excel as a Swing component, and the other for reading and writing Excel files. I have no idea exactly which one you want to work with, but a JExcel Workbook is not a jxl Workbook.

Similar Threads

  1. In-Range adder
    By filem_funk in forum New To Java
    Replies: 1
    Last Post: 11-27-2009, 01:06 AM
  2. Range within an Array
    By End in forum New To Java
    Replies: 6
    Last Post: 04-18-2009, 06:53 PM
  3. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. Can I set a range in case statement?
    By christina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •