本人是新兵,想用eclipse做个简单的导入EXCEL程序。先将EXCEL里的信息读取到窗体上的table里并可以修改table展现的内容。最后导入到数据库中。
目前有几个疑问:1.该如何读取EXCEL的内容并通过table把EXCEL内容展现出来;2.如果修改table里的内容;
下面将我的原代码贴出来,希望高手能指点下。import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;public class CardMain extends ApplicationWindow
{
//声明控件
private Text text;
private Table table;
public CardMain()
{
super(null);
//设置窗体的最小化等按钮
setShellStyle(SWT.CLOSE | SWT.MIN | SWT.TITLE);
createActions();
addStatusLine();
} //创建窗体上所有的控件
protected Control createContents(Composite parent)
{
Composite container = new Composite(parent, SWT.NONE);
container.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));

Button button = new Button(container, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
//监听button
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
        dialog.setFilterPath(System.getProperty("C:\\Documents and Settings\\Administrator\\桌面\\"));
        dialog.setFilterExtensions(new String[] {"*.xls"});
        dialog.setFilterNames(new String[] {"Excel Files(*.xls)"});
        final String file = dialog.open(); 
        
        String ExcelFilePath=dialog.getFilterPath()+"\\"+dialog.getFileName();

        text.setText(ExcelFilePath);
}

});
button.setBounds(22, 8, 148, 22);
button.setText("请选择导入的EXCEL文件");

text = new Text(container, SWT.BORDER);
text.setBounds(10, 36, 542, 22);

Button button1 = new Button(container, SWT.NONE);
button1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {

}
});
button1.setBounds(581, 36, 85, 22);
button1.setText("确定导入");

//创建Table和TableColumn
table = new Table(container, SWT.BORDER | SWT.FULL_SELECTION);
table.setBounds(10, 72, 974, 603);
table.setHeaderVisible(true);
table.setLinesVisible(true);

TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(100);
tableColumn.setText("New Column");

TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
tableColumn_1.setWidth(100);
tableColumn_1.setText("New Column");

TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
tableColumn_2.setWidth(100);
tableColumn_2.setText("New Column");

TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
tableColumn_3.setWidth(100);
tableColumn_3.setText("New Column");

TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
tableColumn_4.setWidth(100);
tableColumn_4.setText("New Column");

TableColumn tableColumn_5 = new TableColumn(table, SWT.NONE);
tableColumn_5.setWidth(100);
tableColumn_5.setText("New Column");

TableColumn tableColumn_6 = new TableColumn(table, SWT.NONE);
tableColumn_6.setWidth(100);
tableColumn_6.setText("New Column");

TableColumn tableColumn_7 = new TableColumn(table, SWT.NONE);
tableColumn_7.setWidth(100);
tableColumn_7.setText("New Column");

TableColumn tableColumn_8 = new TableColumn(table, SWT.NONE);
tableColumn_8.setWidth(100);
tableColumn_8.setText("New Column");

TableColumn tableColumn_9 = new TableColumn(table, SWT.NONE);
tableColumn_9.setWidth(100);
tableColumn_9.setText("New Column"); return container;
} private void createActions()
{
// Create the actions
} //创建监听
protected StatusLineManager createStatusLineManager()
{
StatusLineManager statusLineManager = new StatusLineManager();
return statusLineManager;
} //创建窗体,销毁窗体
public static void main(String args[])
{
try
{
CardMain window = new CardMain();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
} catch (Exception e)
{
e.printStackTrace();
}
} //窗体的标题
protected void configureShell(Shell newShell)
{
super.configureShell(newShell);
newShell.setText("导入程序");
} //窗体显示的大小
protected Point getInitialSize()
{
return new Point(1000, 1800);
}
}

解决方案 »

  1.   

    网上搜下poi相关资料,这是处理excel表格数据的开源项目,可以用他的api去处理excel中的数据
      

  2.   

    读文件,感觉还是jxl比较好用
      

  3.   

    读取excel文件用jxl比较方便,还有楼主貌似想做个桌面程序,为什么不直接用java的swing,而要用eclipse的呢
      

  4.   

    在网上找了下POI和JXL的资料,用JXL实现了。谢谢各位了。