当前位置:   article > 正文

将word文档数据导入到sql server数据库中_word的数据导入sql server

word的数据导入sql server

我现在的需求是这样的,需要将这些题目插入到sql server数据库中。

并且要对应起来,一开始在网上找了很多方法,都没有找到合适的。

后面感觉,还是自己写一个比较好,因为只有自己写的,才是最适合你的!

于是就开始倒腾。

1、首先是读取word中的数据,在我上一篇博客中有相关的方式,博客链接:https://blog.csdn.net/qq_36742720/article/details/83686097

2、主要步骤

(1)将word文档整理成你想要的样子

(2)通过字符串分隔生成数组

(3)将题目数组、选项数组对应起来拼接sql,并执行sql

3、具体操作

(1)整理word

下面是我整理题库后整理完成的两份word

链接:https://pan.baidu.com/s/1k7OpLNKTpOp3wR9gYMbLkg 
提取码:er0g 
(2)在数据库中建好如图所示的表(表名:tb_radio)

(3)打开vs,新建一个控制台项目,在App.config中配置你的数据库连接,main()函数中的代码如下

  1. #region 获取标题数组
  2. //加载Word文档
  3. Document docTitle = new Document();
  4. docTitle.LoadFromFile("C:/Users/承蒙时光不弃/Desktop/题目.doc");
  5. //使用GetText方法获取文档中的所有文本
  6. string titleStr = docTitle.GetText();
  7. string[] titleArr = titleStr.Split(new string[] { "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.", }, StringSplitOptions.RemoveEmptyEntries);
  8. Console.WriteLine(titleArr.Length);
  9. //for (int i = 0; i < titleArr.Length; i++)
  10. //{
  11. // Console.WriteLine(titleArr[i].Trim());
  12. // Console.WriteLine("------------------------------------------------------");
  13. //}
  14. #endregion
  15. #region 获取选项数组
  16. //加载选项文档
  17. string[,] xuanXiangArr2 = new string[70, 4];
  18. Document docXuanXiang = new Document();
  19. docXuanXiang.LoadFromFile("C:/Users/承蒙时光不弃/Desktop/答案.doc");
  20. string xuanXiangStr1 = docXuanXiang.GetText();
  21. string[] xuanXiangArr1 = xuanXiangStr1.Split(new string[] { "A." }, StringSplitOptions.RemoveEmptyEntries);
  22. Console.WriteLine("选项共有:" + xuanXiangArr1.Length);
  23. string[] temp;
  24. for(int i = 0; i < xuanXiangArr1.Length; i++)
  25. {
  26. temp=xuanXiangArr1[i].Split(new string[] { "B.","C.","D." }, StringSplitOptions.RemoveEmptyEntries);
  27. for(int j = 0; j < temp.Length; j++)
  28. {
  29. xuanXiangArr2[i, j] = temp[j];
  30. //Console.WriteLine(xuanXiangArr2[i, j].Trim());
  31. }
  32. //Console.WriteLine("------------------------------------------------------");
  33. }
  34. #endregion
  35. //拼接sql语句并执行
  36. string title;
  37. string A;
  38. string B;
  39. string C;
  40. string D;
  41. string sql;
  42. for(int i = 0; i < xuanXiangArr1.Length; i++)
  43. {
  44. title = titleArr[i].Trim();
  45. A = xuanXiangArr2[i, 0].Trim();
  46. B = xuanXiangArr2[i, 1].Trim();
  47. C = xuanXiangArr2[i, 2]==null?"null":xuanXiangArr2[i,2].Trim();
  48. D = xuanXiangArr2[i, 3] == null ? "null" : xuanXiangArr2[i, 3].Trim();
  49. if (C == "null" && D == "null")
  50. {
  51. sql = "insert into tb_radio(Title, A, B) values('"+title+"', '"+A+"', '"+B+"')";
  52. SqlHelper.ExecuteNonQuery(sql, System.Data.CommandType.Text);
  53. }
  54. else if (C!="null"&&D == "null")
  55. {
  56. sql = "insert into tb_radio(Title, A, B, C) values('" + title + "', '" + A + "', '" + B + "', '"+C+"')";
  57. SqlHelper.ExecuteNonQuery(sql, System.Data.CommandType.Text);
  58. }
  59. else if (C != "null" && D != "null")
  60. {
  61. sql = "insert into tb_radio(Title, A, B, C, D) values('" + title + "', '" + A + "', '" + B + "', '" + C + "', '" + D + "')";
  62. SqlHelper.ExecuteNonQuery(sql, System.Data.CommandType.Text);
  63. }
  64. Console.WriteLine("插入一条记录成功"+(i+1));
  65. }

上面执行sql时用到了我自己封装的SqlHelper,这里也一并提供给大家。

链接:https://pan.baidu.com/s/1fOu8m4soC-wHcrWruSrJKg 
提取码:bwob 

(4)运行代码,数据插入成功!


有不懂的欢迎留言私我~~

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号