-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_xsl.txt
59 lines (51 loc) · 2.11 KB
/
read_xsl.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public DataTable ReadExcelContents(string fileName)
{
try
{
OleDbConnection connection = new OleDbConnection();
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + fileName); //Excel 97-2003, .xls
//string excelQuery = @"Select [Day],[Outlook],[temp],[Humidity],[Wind], [PlaySport]
// FROM [Sheet1$]";
string excelQuery = @"Select * FROM [Sheet1$]";
connection.Open();
OleDbCommand cmd = new OleDbCommand(excelQuery, connection);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = dt.DefaultView;
connection.Close();
return dt;
}
catch (Exception ex)
{
MessageBox.Show("Program can't read file. " + ex.Message, "Please Note", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
DataTable table = new DataTable();
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
string path = @"E:\ICD101.xls";//duong dan toi file excel
string Strconn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
Strconn += ";Extended Properties=Excel 8.0";
conn.ConnectionString = Strconn;
cmd.CommandText = "Select * from [exceltmp$]";//ten sheet
cmd.Connection = conn;
cmd.Connection = conn;
OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
dap.Fill(table);
dataGridView2.DataSource = table;
string strSqlconn1 = "Data Source =MAY15; Initial Catalog =temp; Integrated Security = True";
SqlConnection conn1 = new SqlConnection(strSqlconn1);
conn1.Open();
System.Data.SqlClient.SqlBulkCopy copy = new System.Data.SqlClient.SqlBulkCopy(conn1);
copy.DestinationTableName = "codedmchung";//ten table trong sql
copy.ColumnMappings.Add("Codedm", "CodeDM");
copy.ColumnMappings.Add("Ma_ICD", "Ma_ch");//tên c?t bên excel và tên c?t tuong ?ng trong sql
copy.ColumnMappings.Add("Ten_ICD", "Ten_ch");
copy.WriteToServer(table);
copy.Close();
conn1.Close();
conn1.Dispose();