Example usage for org.apache.poi.hssf.usermodel HSSFSheet getColumnWidth

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet getColumnWidth

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFSheet getColumnWidth.

Prototype

@Override
public int getColumnWidth(int columnIndex) 

Source Link

Document

get the width (in units of 1/256th of a character width )

Usage

From source file:poi.hssf.view.SViewerPanel.java

License:Apache License

protected JComponent makeSheetView(HSSFSheet sheet) {
    JTable sheetView = new JTable(new SVTableModel(sheet));
    sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer);
    if (allowEdits)
        sheetView.setDefaultEditor(HSSFCell.class, cellEditor);
    JTableHeader header = sheetView.getTableHeader();
    //Dont allow column reordering
    header.setReorderingAllowed(false);/*from  ww  w.j av a2 s. c  o m*/
    //Only allow column resizing if editing is allowed
    header.setResizingAllowed(allowEdits);

    //Set the columns the correct size
    TableColumnModel columns = sheetView.getColumnModel();
    for (int i = 0; i < columns.getColumnCount(); i++) {
        TableColumn column = columns.getColumn(i);
        int width = sheet.getColumnWidth(i);
        //256 is because the width is in 256ths of a character
        column.setPreferredWidth(width / 256 * magicCharFactor);
    }

    //Set the rows to the correct size
    int rows = sheet.getPhysicalNumberOfRows();
    Insets insets = cellRenderer.getInsets();
    //Need to include the insets in the calculation of the row height to use.
    int extraHeight = insets.bottom + insets.top;
    for (int i = 0; i < rows; i++) {
        HSSFRow row = sheet.getRow(i);
        if (row == null) {
            sheetView.setRowHeight(i, (int) sheet.getDefaultRowHeightInPoints() + extraHeight);
        } else {
            sheetView.setRowHeight(i, (int) row.getHeightInPoints() + extraHeight);
        }
    }

    //Add the row header to the sheet
    SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight);
    JScrollPane scroll = new JScrollPane(sheetView);
    scroll.setRowHeaderView(rowHeader);
    return scroll;
}

From source file:poi.hssf.view.SVSheetTable.java

License:Apache License

public SVSheetTable(HSSFSheet sheet) {
    super(new SVTableModel(sheet));
    this.sheet = sheet;

    setIntercellSpacing(new Dimension(0, 0));
    setAutoResizeMode(AUTO_RESIZE_OFF);/*from  ww  w  . j a  va  2  s  .  co  m*/
    JTableHeader header = getTableHeader();
    header.setDefaultRenderer(new HeaderCellRenderer());
    pendingPaintings = new PendingPaintings(this);

    //Set the columns the correct size
    TableColumnModel columns = getColumnModel();
    for (int i = 0; i < columns.getColumnCount(); i++) {
        TableColumn column = columns.getColumn(i);
        int width = sheet.getColumnWidth(i);
        //256 is because the width is in 256ths of a character
        column.setPreferredWidth(width / 256 * magicCharFactor);
    }

    Toolkit t = getToolkit();
    int res = t.getScreenResolution();
    TableModel model = getModel();
    for (int i = 0; i < model.getRowCount(); i++) {
        Row row = sheet.getRow(i - sheet.getFirstRowNum());
        if (row != null) {
            short h = row.getHeight();
            int height = Math.round(Math.max(1, h / (res / 70 * 20) + 3));
            System.out.printf("%d: %d (%d @ %d)%n", i, height, h, res);
            setRowHeight(i, height);
        }
    }

    addHierarchyListener(new HierarchyListener() {
        public void hierarchyChanged(HierarchyEvent e) {
            if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0) {
                Container changedParent = e.getChangedParent();
                if (changedParent instanceof JViewport) {
                    Container grandparent = changedParent.getParent();
                    if (grandparent instanceof JScrollPane) {
                        JScrollPane jScrollPane = (JScrollPane) grandparent;
                        setupScroll(jScrollPane);
                    }
                }
            }
        }
    });
}

From source file:ro.nextreports.engine.exporter.util.XlsUtil.java

License:Apache License

/**
 * Copy a sheet to another sheet at a specific (row, column) position
 * /*  ww w .j  av a 2  s. c  o  m*/
 * @param parentSheet the sheet to copy into
 * @param parentSheetRow the row inside parentSheet where we start to copy
 * @param parentSheetColumn the column inside parentSheet where we start to copy
 * @param sheet the sheet that is copied
 * @param copyStyle true to copy the style
 * @return column number
 */
public static int copyToSheet(HSSFSheet parentSheet, int parentSheetRow, int parentSheetColumn, HSSFSheet sheet,
        boolean copyStyle) {
    int maxColumnNum = 0;
    Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;
    for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
        HSSFRow srcRow = sheet.getRow(i);
        HSSFRow destRow;
        // subreport is not the first cell in a row
        if ((parentSheetColumn > 0) && (i == sheet.getFirstRowNum())) {
            destRow = parentSheet.getRow(parentSheetRow);
        } else {
            destRow = parentSheet.getRow(parentSheetRow + i);
            if (destRow == null) {
                destRow = parentSheet.createRow(parentSheetRow + i);
            }
        }
        if (srcRow != null) {
            XlsUtil.copyRow(sheet, parentSheet, parentSheetRow, parentSheetColumn, srcRow, destRow, styleMap);
            if (srcRow.getLastCellNum() > maxColumnNum) {
                maxColumnNum = srcRow.getLastCellNum();
            }
        }
    }
    for (int i = 0; i <= maxColumnNum; i++) {
        parentSheet.setColumnWidth(i, sheet.getColumnWidth(i));
    }
    return maxColumnNum;
}