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

3
我在我的视图中有一个带有五列的SWT表格。
我想要做的是将该表格中的数据导出为Excel电子表格。我想要实现以下几点:
1. 表格的列标题应显示在电子表格中。 2. 所有数据应具有我选择的字体。 3. 所有电子表格单元格都应具有我选择的宽度。 4. 所有单元格格式都应为“文本”。
请问有谁可以指引我正确的方向吗?谢谢。
3个回答

1

我使用了Apache POI将TableViewer的内容转储到Excel工作簿中,并取得了良好的结果。

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

请注意,我的列名存储在一个名为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);
    }
}

0

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


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接