窗口有以下控件,BrownBut(按钮),tableNameslistBox(列表框),dataGridView1(dataGridView)
参考http://www.cnblogs.com/Jinglecat/archive/2006/08/26/487167.html写了以下内容,总觉得还是面向过程,请高手提一下意见,如何改为面向对象?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ConsumerAddressListChange
{
public partial class Form2 : Form
{
string xlsPath;//源文件
string connStr; public Form2()
{
InitializeComponent();
} private void BrownBut_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "Excel文件(*.xls)|*.xls||";
this.openFileDialog1.ShowDialog();
} private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
xlsPath = this.openFileDialog1.FileName.ToString();//得到源文件
// 读取Excel数据,填充DataSet
// 连接字符串
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";" +
"data source=" + xlsPath;
OleDbConnection conn = null;
DataTable tblSchema = null;
IList<string> tblNames = null; // 初始化连接,并打开
conn = new OleDbConnection(connStr);
conn.Open();
// 获取数据源的表定义元数据
tblSchema = conn.GetSchema("Tables");
tblNames = new List<string>();
foreach (DataRow row in tblSchema.Rows)
{
tblNames.Add((string)row["TABLE_NAME"]); // 读取表名
} this.tableNameslistBox.Items.Clear();//清除原有内容
foreach (string tblName in tblNames)
{
this.tableNameslistBox.Items.Add(tblName.Replace("$",""));
}
this.tableNameslistBox.SelectedIndex = 0; // 关闭连接
conn.Close(); } /// <summary>
/// 导出EXCEL表中的数据到 myDataSet
/// </summary>
private static DataSet GetTableData(string path,string selectedTableName)
{
DataSet myDataSet; //创建一个数据链接
string strCon = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= {0} ;Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", path);
OleDbConnection myConn = new OleDbConnection(strCon);
//条件查询EXCEL表
string strCom = " SELECT * FROM [{0}$] ";
string tableName = "[{0}$]"; strCom = string.Format(strCom, selectedTableName); myConn.Open();
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
//创建一个 DataSet对象
myDataSet = new DataSet();
//得到自己的DataSet对象 myCommand.Fill(myDataSet, string.Format(tableName,selectedTableName));
//关闭此数据链接
myConn.Close();
return myDataSet;
} private void tableNameslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.dataGridView1.DataSource = GetTableData(xlsPath, this.tableNameslistBox.SelectedItem.ToString()).Tables[0]; }
}
}
参考http://www.cnblogs.com/Jinglecat/archive/2006/08/26/487167.html写了以下内容,总觉得还是面向过程,请高手提一下意见,如何改为面向对象?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ConsumerAddressListChange
{
public partial class Form2 : Form
{
string xlsPath;//源文件
string connStr; public Form2()
{
InitializeComponent();
} private void BrownBut_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "Excel文件(*.xls)|*.xls||";
this.openFileDialog1.ShowDialog();
} private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
xlsPath = this.openFileDialog1.FileName.ToString();//得到源文件
// 读取Excel数据,填充DataSet
// 连接字符串
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";" +
"data source=" + xlsPath;
OleDbConnection conn = null;
DataTable tblSchema = null;
IList<string> tblNames = null; // 初始化连接,并打开
conn = new OleDbConnection(connStr);
conn.Open();
// 获取数据源的表定义元数据
tblSchema = conn.GetSchema("Tables");
tblNames = new List<string>();
foreach (DataRow row in tblSchema.Rows)
{
tblNames.Add((string)row["TABLE_NAME"]); // 读取表名
} this.tableNameslistBox.Items.Clear();//清除原有内容
foreach (string tblName in tblNames)
{
this.tableNameslistBox.Items.Add(tblName.Replace("$",""));
}
this.tableNameslistBox.SelectedIndex = 0; // 关闭连接
conn.Close(); } /// <summary>
/// 导出EXCEL表中的数据到 myDataSet
/// </summary>
private static DataSet GetTableData(string path,string selectedTableName)
{
DataSet myDataSet; //创建一个数据链接
string strCon = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= {0} ;Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", path);
OleDbConnection myConn = new OleDbConnection(strCon);
//条件查询EXCEL表
string strCom = " SELECT * FROM [{0}$] ";
string tableName = "[{0}$]"; strCom = string.Format(strCom, selectedTableName); myConn.Open();
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
//创建一个 DataSet对象
myDataSet = new DataSet();
//得到自己的DataSet对象 myCommand.Fill(myDataSet, string.Format(tableName,selectedTableName));
//关闭此数据链接
myConn.Close();
return myDataSet;
} private void tableNameslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.dataGridView1.DataSource = GetTableData(xlsPath, this.tableNameslistBox.SelectedItem.ToString()).Tables[0]; }
}
}
真要写成面向对象的话 那可就有的说了
按照我的粗浅分析
1.首先,一切皆为对象,类是对对象的抽象
看看你的代码 一大堆都放在了Form2窗体中,如果其他人要来访问 比如你那个读取Excel方法 这样合适吗?就像有个健身器放你家 大伙有事没事都去你家健身 你舒服? 所以 完全就是可以封装到其他地方 那就是类 把健身器放在一个公共场所 而且耦合性大大降低 健身器也可以自由修改组装。
你上面的程序就是读取Excel的程序,确实是面向过程的,但是这个小程序在实际工作中有面向对象的必要吗?如果把这个功能提炼出来,做成控件,拖拽就可以实现这个功能,不就是一个很好的面向对象的应用了吗?它就是一个读取Excel文件的对象,可以抽象为ExcelReader,自己封装一套Excel操作的类库 - -。最后我说一下,任何表面看是面向对象的代码,底层本质上还是面向过程的,这是不可避免的,只是上层给了你面向对象的错觉而已。我以上说的这些都是实现层次的,你完全可以设计为面向对象,可以当练习,但是意义不大。个人认为。