Excel文件导入到数据库

此案例需要引入NPOI.dll文件和Ionic.Zip.dll文件

 

  1 using System;

  2 

  3 using System.Collections.Generic;

  4 

  5 using System.Linq;

  6 

  7 using System.Text;

  8 

  9 using System.IO;

 10 

 11 using NPOI.SS.UserModel;

 12 

 13 using NPOI.HSSF.UserModel;

 14 

 15 using System.Data.SqlClient;

 16 

 17 using System.Data;

 18 

 19 using Microsoft.International.Converters.PinYinConverter;

 20 

 21 using System.Configuration;

 22 

 23  

 24 

 25 namespace  Excel文件导入到数据库

 26 

 27 {

 28 

 29     class Program

 30 

 31     {

 32 

 33         static void Main(string[] args)

 34 

 35         {

 36 

 37             

 38 

 39             string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

 40 

 41             using (SqlConnection conn = new SqlConnection(connStr))

 42 

 43             {

 44 

 45                 conn.Open();

 46 

 47 //创建回滚事务,如果操作错误,则数据库回到操作前的状态

 48 

 49                 SqlTransaction trans = conn.BeginTransaction();

 50 

 51                 try

 52 

 53                 {

 54 

 55     //sql语句

 56 

 57                     string sql = @"insert into T_Customers values(@CustomerName,

 58 

 59                                        @CellPhone,@Landline,@Postcode,@Email,@Address,@BranchId,

 60 

 61                                        @BuyDate,@CarNum,@BracketNum,@Brand,@TypeNum,@Suggestion,

 62 

 63                                        @Remarks,@Pinyin,@PinyinShort)";

 64 

 65                     

 66 

 67                     using (FileStream fsRead = File.OpenRead("客户资料new.xls"))

 68 

 69                     {

 70 

 71     //创建工作簿

 72 

 73                         using (Workbook book = new HSSFWorkbook(fsRead))

 74 

 75                         {

 76 

 77     

 78 

 79                             using (Sheet sheet = book.GetSheetAt(0))

 80 

 81                             {

 82 

 83                                 //将Excel文件中每个字段的值与数据库中每个字段的值相对应

 84 

 85                                 for (int i = 1; i < sheet.LastRowNum; i++)

 86 

 87                                 {

 88 

 89                                     //参数

 90 

 91                                     SqlParameter[] paras ={

 92 

 93                                                  new SqlParameter("@CustomerName",SqlDbType.NVarChar),

 94 

 95                                                  new SqlParameter("@CellPhone",SqlDbType.VarChar),

 96 

 97                                                  new SqlParameter("@Landline",SqlDbType.VarChar),

 98 

 99                                                  new SqlParameter("@CarNum",SqlDbType.VarChar),

100 

101                                                  new SqlParameter("@BracketNum",SqlDbType.VarChar),

102 

103                                                  new SqlParameter("@BuyDate",SqlDbType.DateTime),

104 

105                                                  new SqlParameter("@Postcode",DBNull.Value),

106 

107                                                  new SqlParameter("@Email",DBNull.Value),

108 

109                                                  new SqlParameter("@Address",DBNull.Value),

110 

111                                                  new SqlParameter("@BranchId",DBNull.Value),

112 

113                                                  new SqlParameter("@Brand",DBNull.Value),

114 

115                                                  new SqlParameter("@TypeNum",DBNull.Value),

116 

117                                                  new SqlParameter("@Suggestion",DBNull.Value),

118 

119                                                  new SqlParameter("@Remarks",DBNull.Value),

120 

121                                                  new SqlParameter("@Pinyin",SqlDbType.VarChar),

122 

123                                                  new SqlParameter("@PinyinShort",SqlDbType.VarChar),

124 

125  

126 

127                                              };

128 

129                                     Row row = sheet.GetRow(i);

130 

131                                     for (int c = 0; c <= row.LastCellNum; c++)

132 

133                                     {

134 

135     //如果Excel文件中的单元格的内容为空,那么就将数据库中对应字段的值设置为DBNull.Value

136 

137                                         paras[c].Value = row.GetCell(c) == null ? DBNull.Value : (object)row.GetCell(c).ToString();

138 

139  

140 

141  

142 

143  

144 

145                                         paras[14].Value = PinyinConvert(row.GetCell(0).ToString());

146 

147                                         paras[15].Value = PinyinConverta(row.GetCell(0).ToString());

148 

149  

150 

151                                     }

152 

153                                     //执行ql语句,如果执行成功,则提交事务

154 

155                                     SqlHelper.ExecuteNonQuery(sql, paras);

156 

157                                     trans.Commit();

158 

159                                     Console.WriteLine("成功");

160 

161  

162 

163                                 }

164 

165                             }

166 

167                         }

168 

169                     }

170 

171                 }

172 

173                 //如果执行失败,则回滚事务

174 

175                 catch (Exception e)

176 

177                 {

178 

179                     trans.Rollback();//回滚事务

180 

181                     Console.WriteLine("失败"+e.Message.ToString());

182 

183                     throw;

184 

185                 }

186 

187             }

188 

189         }

190 

191                 

192 

193                       

194 

195                  

196 

197            

198 

199     

200 

201         #region 

202 

203         private static string  PinyinConvert(string str)

204 

205         {

206 

207             StringBuilder sb1 = new StringBuilder();

208 

209            

210 

211             foreach (char item in str)

212 

213             {

214 

215                 if (ChineseChar.IsValidChar(item))

216 

217                 {

218 

219                     ChineseChar cnChar = new ChineseChar(item);

220 

221                     if (cnChar.PinyinCount > 0)

222 

223                     {

224 

225                         sb1.Append(cnChar.Pinyins[0].Substring(0, cnChar.Pinyins[0].Length - 1));

226 

227                      

228 

229  

230 

231                     }

232 

233                 }

234 

235             }

236 

237             return sb1.ToString();

238 

239         }

240 

241         #endregion

242 

243         #region 

244 

245         private static string  PinyinConverta(string str)

246 

247         {

248 

249            

250 

251             StringBuilder  sb2=new StringBuilder();

252 

253             foreach (char item in str)

254 

255             {

256 

257                 if (ChineseChar.IsValidChar(item))

258 

259                 {

260 

261                     ChineseChar cnChar = new ChineseChar(item);

262 

263                     if (cnChar.PinyinCount > 0)

264 

265                     {

266 

267                       

268 

269                         sb2.Append(cnChar.Pinyins[0].Substring(0,1));

270 

271  

272 

273                     }

274 

275                 }

276 

277             }

278 

279             return sb2.ToString();

280 

281         }

282 

283         #endregion

284 

285     }

286 

287 }

 

 

 

 

你可能感兴趣的:(Excel)