簡體   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