在做Java界面程序时,碰到这样的问题,界面上的Table如何到成Excel文件,这样更具有实用性,因此自己上网搜了下,自己根据当时的需求改写了一下,只需要提供一个Table,和文件保存路径就能将界面上的表格转化成.xls文件

测试代码如下:
1.操作Excel需要的jar包: jxl.jar
https://download.csdn.net/download/qq_62719995/86245450
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class testCreateExcel {
protected Shell shell;
private Table table;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
testCreateExcel window = new testCreateExcel();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setBounds(48, 39, 341, 153);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
tblclmnNewColumn.setWidth(100);
tblclmnNewColumn.setText("\u59D3\u540D");
TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
tblclmnNewColumn_1.setWidth(100);
tblclmnNewColumn_1.setText("\u5E74\u9F84");
TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
tblclmnNewColumn_2.setWidth(100);
tblclmnNewColumn_2.setText("\u7C4D\u8D2F");
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText(new String[] {"\u5F20\u4E09", "19", "\u6E56\u5357"});
TableItem tableItem_1 = new TableItem(table, SWT.NONE);
tableItem_1.setText(new String[] {"\u674E\u56DB", "20", "\u56DB\u5DDD"});
TableItem tableItem_2 = new TableItem(table, SWT.NONE);
tableItem_2.setText(new String[] {"\u738B\u4E94", "18", "\u5E7F\u4E1C"});
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
FileDialog dialog = new FileDialog(shell, SWT.SAVE);//文件保存对话框
dialog.setFilterExtensions(new String[] { ".xls" });//文件类型过滤器
String path = dialog.open();//得到路径
createExcel(table, path); //创建Excel表
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
btnNewButton.setBounds(48, 213, 98, 30);
btnNewButton.setText("\u5BFC\u51FAExcel");
}
//创建Excel表 传table 和 保存路径
private static void createExcel(Table table,String path) throws WriteException,IOException{
if(table==null || path==null ||path.equals("")) {
return;
}
FileOutputStream fos = new FileOutputStream(path);
int cols = table.getColumnCount();
int rows = table.getItemCount();
// 创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(fos);
// 创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
// 写入列名
for (int i = 0; i < cols; i++) {
Label Lcol = new Label(i, 0, table.getColumn(i).getText());
sheet.addCell(Lcol);
}
// 写入内容
TableItem[] tItem = table.getItems();
for (int i = 1; i <= rows; i++) {
for (int j = 0; j < cols; j++) {
Label item = new Label(j, i, tItem[i - 1].getText(j));
sheet.addCell(item);
}
}
// 把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
fos.close();
}
}
类似的,这个createExcel(Table table,String path)方法改写用去数据库数据导出,只是不在需要SWT的table了。