1
/**//// <summary>
2
/// 读取Excel文档
3
/// </summary>
4
/// <param name="Path">文件名称</param>
5
/// <returns>返回一个数据集</returns>
6
public DataSet ExcelToDS(string Path)
7

{
8
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
9
OleDbConnection conn = new OleDbConnection(strConn);
10
conn.Open();
11
string strExcel = "";
12
OleDbDataAdapter myCommand = null;
13
DataSet ds = null;
14
strExcel="select * from [sheet1$]";
15
myCommand = new OleDbDataAdapter(strExcel, strConn);
16
ds = new DataSet();
17
myCommand.Fill(ds,"table1");
18
return ds;
19
}
20
21
22
/**//// <summary>
23
/// 写入Excel文档
24
/// </summary>
25
/// <param name="Path">文件名称</param>
26
public bool SaveFP2toExcel(string Path)
27

{
28
try
29

{
30
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
31
OleDbConnection conn = new OleDbConnection(strConn);
32
conn.Open();
33
System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
34
cmd.Connection =conn;
35
//cmd.CommandText ="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
36
//cmd.ExecuteNonQuery ();
37
for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
38

{
39
if(fp2.Sheets [0].Cells[i,0].Text!="")
40

{
41
cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
42
fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
43
"','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
44
cmd.ExecuteNonQuery ();
45
}
46
}
47
conn.Close ();
48
return true;
49
}
50
catch(System.Data.OleDb.OleDbException ex)
51

{
52
System.Diagnostics.Debug.WriteLine ("写入Excel发生错误:"+ex.Message );
53
}
54
return false;
55
}


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
