首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在JavaFx中嵌入Excel

在JavaFx中嵌入Excel
EN

Stack Overflow用户
提问于 2017-02-10 05:05:46
回答 1查看 3K关注 0票数 2

有没有允许在FX GUI中显示excel工作表/工作簿的库?它不需要被修改。

我正在使用apache POI来读写文档。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-12 23:52:00

作为我如何解决这个问题的最新进展。我使用controlsFX的SpreadsheetView来显示我使用poi库读取的excel工作表,所需的只是读取工作表并为视图构建数据,我添加了下面的代码。

代码语言:javascript
复制
/**
 * This class allows for the displaying of excel files within a window by
 * utilizing SpreadsheetView from controlsFX and the reading capabilities of     the POI library.
 * 
 * Only usable for .xlsx files
 *  
 * @author TM
 *
 */
public class ExcelView {

    /**
     * Path to Excel
     */
    private String filePath;

    private int sheetIndex;

    private boolean editible;

    private FileInputStream inStream;
    private XSSFWorkbook poiWorkbook;
    private XSSFSheet poiSheet;

    private SpreadsheetView theView;

    public ExcelView(String path, int sheetIndex ,boolean editable){
        filePath  =path;
        this.editible  =editable;
        this.sheetIndex  =sheetIndex;
    }

    public ExcelView(String path, int sheetIndex){
        filePath  =path;
        this.editible  =false;
        this.sheetIndex = sheetIndex;
    }

    private void initializeView() throws Exception{
        GridBase grid = excelToGrid();

        theView = new SpreadsheetView(grid);
        theView.setEditable(editible);
    }

    public SpreadsheetView getView() throws Exception{
        initializeView();
        return theView;
    }

    public void showInNewWindow(){
        Parent root;
        try {

        initializeView();

        root = theView;
        Stage stage = new Stage();
        stage.setTitle(new File(filePath).getName());
        stage.setScene(new Scene(root, 450, 450));

        stage.getIcons().addAll(ResourceLoader.getIcons("Excel.ico"));      

        stage.show();

    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Updates the values in the view. This may happen after the Excel file has been 
 * modified after the initial reading.
 * @throws Exception 
 */
public void updateView() throws Exception{
    GridBase newgrid = excelToGrid();

    theView.setGrid(newgrid);
}

/**
 * Creates a {@link GridBase} object from the excel file located at the path
 * @return
 * @throws Exception - when opening the file
 */
private GridBase excelToGrid() throws Exception{

    // Read the Excel document and collect the rows
    openBook();
    poiSheet = poiWorkbook.getSheetAt(sheetIndex);

    int[] size =  getSize();
    GridBase grid = new GridBase(size[0], size[1]);

    ObservableList<ObservableList<SpreadsheetCell>> rows = FXCollections.observableArrayList();

    Row poiRow;
    Cell cell;
    String value;
    FormulaEvaluator evaluator = poiWorkbook.getCreationHelper().createFormulaEvaluator();

    for (int row = 0; row < grid.getRowCount(); ++row) {
        final ObservableList<SpreadsheetCell> list = FXCollections.observableArrayList();
        poiRow = poiSheet.getRow(row);
        for (int column = 0; column < grid.getColumnCount(); ++column) {

            cell = poiRow.getCell(column);
            value = ExcelUtils.cellStringValue(evaluator,cell);

            list.add(SpreadsheetCellType.STRING.createCell(row, column, 1, 1,value));
        }
        rows.add(list);
    }
    grid.setRows(rows);

    closeBook();

    return grid;
}

/**
 * Calculates the number of rows and columns in the sheet by looping 
 * and reading all the things :) 
 * @return the size as int[{rows, cols}]
 */
private int[] getSize(){

    int numRows = 0;
    int numCols =0;

    int nullRowCounter = 0;
    int nullColCounter = 0;

    int maxNullRows = 6;
    int maxNullCols = 6;

    Row row;
    Cell cell;
    int localColCounter;

    while(true){

        row= poiSheet.getRow(numRows);
        numRows++;

        // Check row...
        if(row == null){
            nullRowCounter++;
        }
        else{
            nullRowCounter = 0;
            // If row not null, check columns...
            localColCounter = 0;
            while(true){
                cell = row.getCell(localColCounter);
                localColCounter++;
                if(cell==null){
                    nullColCounter++;
                }else{
                    nullColCounter = 0;
                }

                if(nullColCounter == maxNullCols){
                    // reached max null cells
                    localColCounter -= maxNullCols;

                    if(localColCounter >  numCols)
                        numCols = localColCounter;

                    break;
                    // go to next row...
                }

            }
        }

        if(nullRowCounter == maxNullRows){
            // reached max null rows
            numRows -= maxNullRows;

            break;

        }
    }
    return new int[]{numRows, numCols};

}

private void openBook() throws Exception{
    try {
        File myFile = new File(filePath);
        inStream = new FileInputStream(myFile);

        poiWorkbook = new XSSFWorkbook (inStream);

    }catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

private void closeBook() throws Exception{


    try {
        poiWorkbook.close();
        inStream.close();

    }catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42146856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档