简体   繁体   English

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

[英]Exporting SWT Table data to a Excel spreadsheet

I have a SWT Table on my View with five column. 我的视图中有一个SWT表,有五列。

What I want to do is export the data from that table as an Excel spreadsheet. 我想要做的是将该表中的数据导出为Excel电子表格。 I want to followings to happen. 我想要发生以下事情。

  1. The column headers from the Table should appear on the spreadsheet. 表格中的列标题应显示在电子表格中。

  2. All data should have a font of my choosing. 所有数据都应该是我选择的字体。

  3. All spreadsheet cells should have a width of my choosing. 所有电子表格单元格都应该具有我选择的宽度。

  4. All the cell format should be 'text'. 所有单元格格式都应为“文本”。

Can someone please point me towards the right direction? 有人可以指出我正确的方向吗? Thank you. 谢谢。

You can use any of these frameworks to create an Excel sheet with Java Application, depending on your requirement and complexity of the application. 您可以使用任何这些框架来创建带有Java Application的Excel工作表,具体取决于您的要求和应用程序的复杂性。 1. http://poi.apache.org/spreadsheet/index.html 2. http://extentech.com/estore/product_detail.jsp?product_group_id=1 1. http://poi.apache.org/spreadsheet/index.html 2. http://extentech.com/estore/product_detail.jsp?product_group_id=1

I've used Apache POI to dump TableViewer contents into an Excel workbook with good results. 我使用Apache POI将TableViewer内容转储到Excel工作簿中,效果很好。

Here's a sample method that creates an XSSFWorkbook containing the contents of a given TableViewer. 这是一个示例方法,用于创建包含给定TableViewer内容的XSSFWorkbook。 It contains examples of styling cells and autosizing column widths. 它包含样式单元格和自动调整列宽的示例。

Note that I have my column names in an array of Strings called tableColumnNames, as they're used elsewhere in my code and it's convenient to just use that array, but you could farm those from the TableViewer as well. 请注意,我的列名称在一个名为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;
}

Once you've got the XSSFWorkbook, you can dump it to a file like this: 获得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);
    }
}

I would use OpenOffice SDK to create spreadsheets programmatically. 我会使用OpenOffice SDK以编程方式创建电子表格。 However it must be installed and the service must be running while the user is working with the view. 但是,必须安装它,并且在用户使用视图时必须运行该服务。 I don't know whether it is acceptable in your case. 我不知道你的情况是否可以接受。

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

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