在做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了。