gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:59 28 4
gpt4 key购买 nike

我的 View 上有一个包含五列的 SWT 表。

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

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

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

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

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

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

最佳答案

我使用 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);
}
}

关于java - 将 SWT 表数据导出到 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13988909/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com