2012-12-21 121 views
3

我在我的View上有一个SWT表格,其中有五列。将SWT表格数据导出到Excel电子表格

我想要做的是将该表中的数据导出为Excel电子表格。我想要跟随发生。

  1. 表中的列标题应出现在电子表格中。

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

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

  4. 所有的单元格格式应该是'文本'。

有人能指点我朝着正确的方向吗?谢谢。

回答

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); 
    } 
} 
0

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

相关问题