为了工作对账的需要,尝试制作了一个对账的小程序,虽然最后没用上,但还是写点什么记录下来。这个程序是基于c# winform开发的,主要实现了讲Excel导入到DataGirdview中,并实现一些相应的查询。以下是Form的截图。
1、以下是“选择文件路径”按钮的代码,选择文件的路径将在tbxPath(textbox)中显示。
1
2
3
4
5
6
7 |
private
void
btnChose_Click( object
sender, EventArgs e) { if
( this .openFileDialog1.ShowDialog() == DialogResult.OK) { this .tbxPath.Text = this .openFileDialog1.FileName; } } |
2、将Excel导入到DataGridView中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
public
DataTable ExcelToDataTableBySql( string
path, string
sql) { string
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ path + ";Extended Properties=‘Excel 8.0;HDR=Yes;IMEX=1‘;" ; //path来自tbxPath(textbox) OleDbConnection conn = new
OleDbConnection(strConn); conn.Open(); System.Data.DataTable schematable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null ); string
tableName = schematable.Rows[0][2].ToString().Trim(); OleDbDataAdapter myCommand = null ; DataSet ds = new
DataSet(); string
strExcel = string .Format( "select * from [{0}$] where {1}" , tbxWorkSheet.Text.ToString(),sql); //tbxWorkSheet是Excel中的工作簿,sql来自tbxsql(textbox) myCommand = new
OleDbDataAdapter(strExcel, strConn); myCommand.Fill(ds, tbxWorkSheet.Text.ToString()); DataTable dt = ds.Tables[0]; return
dt; } |
原文:http://www.cnblogs.com/gogoSpace/p/3528534.html