繁体   English   中英

将SWT表数据导出到Excel电子表格

[英]Exporting SWT Table data to a Excel spreadsheet

我的视图中有一个SWT表,有五列。

我想要做的是将该表中的数据导出为Excel电子表格。 我想要发生以下事情。

  1. 表格中的列标题应显示在电子表格中。

  2. 所有数据都应该是我选择的字体。

  3. 所有电子表格单元格都应该具有我选择的宽度。

  4. 所有单元格格式都应为“文本”。

有人可以指出我正确的方向吗? 谢谢。

您可以使用任何这些框架来创建带有Java Application的Excel工作表,具体取决于您的要求和应用程序的复杂性。 1. http://poi.apache.org/spreadsheet/index.html 2. http://extentech.com/estore/product_detail.jsp?product_group_id=1

我使用Apache POI将TableViewer内容转储到Excel工作簿中,效果很好。

这是一个示例方法,用于创建包含给定TableViewer内容的XSSFWorkbook。 它包含样式单元格和自动调整列宽的示例。

请注意,我的列名称在一个名为tableColumnNames的字符串数组中,因为它们在我的代码中的其他地方使用,并且使用该数组很方便,但您也可以从TableViewer中获取这些数组。

private XSSFWorkbook createWorkbookFromTable(TableViewer table) {
    // create a workbook
    XSSFWorkbook wb = new XSSFWorkbook();

    // add a worksheet
    XSSFSheet sheet = wb.createSheet("My Table Data");

    // shade the background of the header row
    XSSFCellStyle headerStyle = wb.createCellStyle();
    headerStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
    headerStyle.setBorderTop(CellStyle.BORDER_THIN);
    headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
    headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
    headerStyle.setBorderRight(CellStyle.BORDER_THIN);
    headerStyle.setAlignment(HorizontalAlignment.CENTER);

    // add header row
    Table table = table.getTable();
    TableColumn[] columns = table.getColumns();
    int rowIndex = 0;
    int cellIndex = 0;
    XSSFRow header = sheet.createRow((short) rowIndex++);
    for (TableColumn column : columns) {
        String columnName = column.getText();
        XSSFCell cell = header.createCell(cellIndex++);
        cell.setCellValue(column.getText());
        cell.setCellStyle(headerStyle);
    }

    // add data rows
    TableItem[] items = table.getTable().getItems();
    for (TableItem item : items) {
        // create a new row
        XSSFRow row = sheet.createRow((short) rowIndex++);
        cellIndex = 0;

        for (int i = 0; i < columns.length; i++) {
            // create a new cell
            String columnName = tableColumnNames[i];
            XSSFCell cell = row.createCell(cellIndex++);

            // set the horizontal alignment (default to RIGHT)
            XSSFCellStyle cellStyle = wb.createCellStyle();
            ha = HorizontalAlignment.RIGHT;
            cellStyle.setAlignment(ha);
            cell.setCellStyle(cellStyle);

            // set the cell's value
            String text = item.getText(i);
            cell.setCellValue(text);
        }
    }

    // autofit the columns
    for (int i = 0; i < columns.length; i++) {
        sheet.autoSizeColumn((short) i);
    }

    return wb;
}

获得XSSFWorkbook后,可以将其转储到如下文件中:

public void dumpWorkbookToAFile(XSSFWorkbook wb, String filename) {
    try {
        FileOutputStream fos = new FileOutputStream(filename);
        wb.write(fos);
        fos.close();
        MessageDialog.openInformation(shell,
            "Save Workbook Successful",
            "Workbook saved to the file:\n\n" + filename);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        String msg = ioe.getMessage();
        MessageDialog.openError(shell, 
            "Save Workbook Failed",
            "Could not save workbook to the file:\n\n" + msg);
    }
}

我会使用OpenOffice SDK以编程方式创建电子表格。 但是,必须安装它,并且在用户使用视图时必须运行该服务。 我不知道你的情况是否可以接受。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM