C#教程之C#实现Excel导入sqlite的方法

本文实例讲述了C#实现Excel导入sqlite的方法,是非常实用的技巧。分享给大家供大家参考。具体方法如下:

首先需要引用system.date.sqlite

具体实现代码如下:

?

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

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

system.date.sqlite

system.date.sqlite.linq

//导入--Excel导入sqlite

private void button2_Click(object sender, EventArgs e)

{

  DAL.Sqlite da = new DAL.Sqlite("DataByExcel.db");

  if (chk_sfzj.Checked==false)

  {

 //删除全部数据

 if (da.SqlExSQLiteCommand("delete from sqllitebyexcel"))

 {

 

 }

 else

 {

   MessageBox.Show("删除原失败,请联系管理员!");

 }

  }

  OpenFileDialog ofg = new OpenFileDialog();

  ofg.Filter = "*.xls|*.xls";

  if (ofg.ShowDialog() == System.Windows.Forms.DialogResult.OK)

  {

 string sName = ofg.FileName;

 if (new BLL.Excelcs().OutExcel(sName, da))

 {

   MessageBox.Show("导入成功");

   //bdData("");

 }

 else

 {

   MessageBox.Show("导入失败");

 }

  }

}

 

/// <summary>

/// 初始化数据库

/// </summary>

/// <param name="strSqlitePath">数据库文件路径</param>

 

 SQLiteConnection SQLCon;

public Sqlite(string dataName)

{

    SQLCon = new SQLiteConnection(string.Format("Data Source={0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, dataName));

}

 

 /// <summary>

/// 执行sql语句

/// </summary>

/// <param name="strSql">sql语句</param>

/// <returns>是否执行成功</returns>

public bool SqlExSQLiteCommand(string strSql)

{

  SqlOpen();

  SQLiteCommand cmd = new SQLiteCommand();

  cmd.Connection = SQLCon;

  cmd.CommandText = strSql;

  try

  {

 int i = cmd.ExecuteNonQuery();

 return true;

  }

  catch (Exception ex)

  {

 return false;

  }

}

 

/// <summary>

/// 导入数据到数据库

/// </summary>   

/// <param name="outFile">文件</param>

/// <param name="sql">数据库操作对象</param>

/// <returns></returns>

public bool OutExcel(string outFile,DAL.Sqlite sql)

{

  DataTable dt = DAL.Excel.TransferData(outFile, "Sheet1").Tables[0];

  try

  {

 foreach (DataRow item in dt.Rows)

 {

 

   string strSql = @"insert into sqllitebyexcel

  (No,BUSINESS_NO,BUSINESS_TYPE_NAME,VESSEL_NAME_C,VOYAGE,BILL_NO,CTNW1,CTNW2,

    CTNW3,TXDD,XXDD,CTN_NO,CTN_TYPE,NAME1,NAME2,NAME3,IN_DATE,JFJSSJ,JFSC,DYPCD,TXPCSJ,

TXPCSC,JCSJ,TXSC,H986JJYCSJ,YFYXSJ,LXSJ,LXSC,CCJFSJ,TXJCSJ,TXCCSJ,DCTXSC,TimeNow,DDTXSC)

    values('{0}','{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}')";

   string strEnd = string.Format(strSql, item[0], item[1], item[2], item[3], item[4], item[5],

 item[6], item[7], item[8], item[9], item[10], item[11], item[12],

 item[13], item[14], item[15], item[16].ToDate(), item[17].ToDate(), item[18], item[19].ToDate(),

 item[20].ToDate(), item[21], item[22].ToDate(), item[23], item[24].ToDate(), item[25].ToDate(), item[26].ToDate(),

 item[27], item[28].ToDate(), item[29].ToDate(), item[30].ToDate(), item[31], DateTime.Now.ToDate(), "");

   sql.SqlExSQLiteCommand(strEnd);

 }

    return true;

  }

  catch (Exception ex)

  {

    // MessBox.Show("");

 string aa = ex.Message;

 return false;

  }

}

 

public static string ToDate(this object obj)

{

  // if (obj == null || string.IsNullOrEmpty(obj.ToString()))

  if(string.IsNullOrEmpty(obj.ToString().Trim()))

  {

 return "null";

  }

  return ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss");

}

/// <summary>

/// 获取excel表数据

/// </summary>

/// <param name="excelFile">excel文件路径</param>

/// <param name="sheetName">excel工作表名</param>

/// <returns></returns>

public static DataSet TransferData(string excelFile, string sheetName)

{

  DataSet ds = new DataSet();

  //获取全部数据 

  string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" "Data Source=" + excelFile + ";" "Extended Properties=Excel 8.0;";

  OleDbConnection conn = new OleDbConnection(strConn);

  try

  {

    

 conn.Open();

 string strExcel = "";

 OleDbDataAdapter myCommand = null;

 strExcel = string.Format("select * from [{0}$]", sheetName);

 myCommand = new OleDbDataAdapter(strExcel, strConn);

 myCommand.Fill(ds);

  }

  catch (Exception ex)

  {

 throw new Exception(ex.Message);

  }

  finally

  {

 conn.Close();

  }

  return ds;

}

相信本文所述对大家的C#程序设计有一定的借鉴价值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值