两张表导入到一个DataGrid里面(题目表和答案表)

前台代码

  1. <asp:Content ID="Content1" ContentPlaceHolderID="cphToolBar" runat="server">
  2. <epoint:Button ID="btnImport" runat="server" Text="题库信息导入" />
  3. <span>
  4. <span style="color: red;">模板下载:</span><a target="_blank"
  5. href="题库模版.xls"><span style="color: blue;">点击下载文件</span> </a>
  6. </span>
  7. </asp:Content>
  8.  
  9. <asp:UpdatePanel runat="server" ID="UpdatePanel_Upload">
  10. <ContentTemplate>
  11. <epoint:CuteWebUIUpload_NoBut ID="upload1" AllowFileList="xlsx;xls" runat="server"
  12. MaxAttachCount="-1" MaxAttachCountOneTime="" UseCustomSaveMethod="true" OnFileUploadCompleted_Custom="upload1_FileUploadCompleted_Custom" />
  13. </ContentTemplate>
  14. </asp:UpdatePanel>

后台代码

  1. /// <summary>
  2. /// 导入
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="args"></param>
  6.  
  7. protected void upload1_FileUploadCompleted_Custom(object sender, EventArgsOperate.AttachEventArgs[] args)
  8. {
  9. if (!Directory.Exists(Server.MapPath(@"ImportExcel")))
  10. Directory.CreateDirectory(Server.MapPath(@"ImportExcel"));
  11. string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
  12. string oldfileName = args[].FileName;
  13. string documentType = oldfileName.Substring(oldfileName.LastIndexOf('.'), oldfileName.Length - oldfileName.LastIndexOf('.'));
  14. string fileName = "Import_" + mark + documentType;
  15. args[].CuteArgs.CopyTo(Server.MapPath(@"ImportExcel\") + fileName);
  16.  
  17. ReadExcel(Server.MapPath(@"ImportExcel\") + fileName);
  18. }
  19.  
  20. public void ReadExcel(string ExcelFile)
  21. {
  22. DataSet ds;
  23. string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFile + ";" + "Extended Properties='Excel 12.0';";
  24. OleDbConnection conn = new OleDbConnection(strConn);
  25. DataTable dtExcelSchema = new DataTable();
  26. try
  27. {
  28. conn.Open();
  29. dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
  30. new object[] { null, null, null, "Table" });//获取需要上传的Excel的Sheet
  31. conn.Close();
  32. }
  33. catch
  34. {
  35. throw;
  36. }
  37. for (int k = ; k <= dtExcelSchema.Rows.Count; k++)
  38. {
  39. try
  40. {
  41. ds = new DataSet();
  42. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [Sheet" + k + "$]", strConn);
  43. oada.Fill(ds);
  44. }
  45. catch
  46. {
  47. throw;
  48. }
  49.  
  50. DataTable dt = ds.Tables[];
  51. if (dt.Rows.Count > )
  52. {
  53.  
  54. for (int i = ; i < dt.Rows.Count; i++)
  55. {
  56. M_Exam_Subject m_es = new M_Exam_Subject();
  57. M_Exam_Answer m_ea = new M_Exam_Answer();
  58. #region 插入题目
  59. m_es.SubjectGuid = NewSubjectGuid();
  60. m_es.Title = Convert.ToString(dt.Rows[i][]);//题目名称
  61. string type = Convert.ToString(dt.Rows[i][]);//题目类型
  62. switch (type)
  63. {
  64. case "单选": m_es.Type = "";
  65. break;
  66. case "多选": m_es.Type = "";
  67. break;
  68. case "判断": m_es.Type = "";
  69. break;
  70. case "填空": m_es.Type = "";
  71. break;
  72. case "简答": m_es.Type = "";
  73. break;
  74. }
  75.  
  76. string difficult = Convert.ToString(dt.Rows[i][]);//题目难度
  77. switch (difficult)
  78. {
  79. case "简单": m_es.Difficult = ;
  80. break;
  81. case "一般": m_es.Difficult = ;
  82. break;
  83. case "难": m_es.Difficult = ;
  84. break;
  85. }
  86. m_es.AnswerNote = Convert.ToString(dt.Rows[i][]);//答案解析
  87. m_es.GroupID = Convert.ToInt32(ParentRowID);
  88. b_examsubject.Insert(m_es);
  89. #endregion
  90. //插入主观题答案
  91. if ((type == "填空") || (type == "简答"))
  92. {
  93. m_es.SubjectGuid = m_es.SubjectGuid;
  94. m_es.RightAnswer = Convert.ToString(dt.Rows[i][]);//正确答案
  95. b_examsubject.Update(m_es);
  96. }
  97. //插入客观题答案
  98. else
  99. {
  100. //for (int j = 3; j < 7; j++)
  101. //{
  102. // m_ea.SubjectGuid = m_es.SubjectGuid;
  103. // m_ea.AnswerGuid = Guid.NewGuid().ToString();
  104. // m_ea.AnswerName = Convert.ToString(dt.Rows[i][j]);//答案
  105. // b_examanswer.Insert(m_ea);
  106. //}
  107. DataView dvRecord = dt.DefaultView;
  108. string answerid1 = Guid.NewGuid().ToString();
  109. string answerid2 = Guid.NewGuid().ToString();
  110. string answerid3 = Guid.NewGuid().ToString();
  111. string answerid4 = Guid.NewGuid().ToString();
  112. string answerid5 = Guid.NewGuid().ToString();
  113. string answerid6 = Guid.NewGuid().ToString();
  114. if (Convert.ToString(dvRecord[i][]) != "")
  115. b_examanswer.InsertAnswer(answerid1, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
  116. if (Convert.ToString(dvRecord[i][]) != "")
  117. b_examanswer.InsertAnswer(answerid2, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
  118. if (Convert.ToString(dvRecord[i][]) != "")
  119. b_examanswer.InsertAnswer(answerid3, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
  120. if (Convert.ToString(dvRecord[i][]) != "")
  121. b_examanswer.InsertAnswer(answerid4, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
  122. if (Convert.ToString(dvRecord[i][]) != "")
  123. b_examanswer.InsertAnswer(answerid5, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
  124. if (Convert.ToString(dvRecord[i][]) != "")
  125. b_examanswer.InsertAnswer(answerid6, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
  126.  
  127. //添加正确答案
  128. int num = ;
  129. if (Convert.ToString(dvRecord[i][]) != "")
  130. {
  131. string strright = Convert.ToString(dvRecord[i][]).Trim();
  132. if (strright.IndexOf('A') >= || strright.IndexOf('a') >= )
  133. {
  134. num++;
  135. b_examanswer.UpdateAnswer_isRight(answerid1, true);
  136. }
  137. if (strright.IndexOf('B') >= || strright.IndexOf('b') >= )
  138. {
  139. num++;
  140. b_examanswer.UpdateAnswer_isRight(answerid2, true);
  141. }
  142. if (strright.IndexOf('C') >= || strright.IndexOf('c') >= )
  143. {
  144. num++;
  145. b_examanswer.UpdateAnswer_isRight(answerid3, true);
  146. }
  147. if (strright.IndexOf('D') >= || strright.IndexOf('d') >= )
  148. {
  149. num++;
  150. b_examanswer.UpdateAnswer_isRight(answerid4, true);
  151. }
  152. if (strright.IndexOf('E') >= || strright.IndexOf('e') >= )
  153. {
  154. num++;
  155. b_examanswer.UpdateAnswer_isRight(answerid5, true);
  156. }
  157. if (strright.IndexOf('F') >= || strright.IndexOf('f') >= )
  158. {
  159. num++;
  160. b_examanswer.UpdateAnswer_isRight(answerid6, true);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. BindGrid();
  168. }

单表导入到一个DataGrid里面

后台代码

  1. protected void InfoExport()
  2. {
  3. try
  4. {
  5. string ExcelName = this.CreateExcel();
  6. //将服务器上的Excel导出
  7. // CuteWebUIOperate.DownloadFile(HttpContext.Current, Server.MapPath("ExcelExport/") + ExcelName, ExcelName, false);
  8. string strScript = "window.open('ExcelExport/" + ExcelName + "');";
  9. this.WriteAjaxMessage(strScript);
  10. }
  11. catch
  12. {
  13. throw;
  14. }
  15. }
  16.  
  17. protected string CreateExcel() //生成Excel
  18. {
  19. string Header = "报名信息";
  20. string strFileName = "";
  21.  
  22. // 生成文件夹
  23. string fileFolderPath = Server.MapPath("ExcelExport/");
  24. if (!System.IO.Directory.Exists(fileFolderPath))
  25. System.IO.Directory.CreateDirectory(fileFolderPath);
  26.  
  27. Workbook wb = new Workbook();
  28.  
  29. wb.Worksheets.Add("Sheet1");
  30.  
  31. Worksheet ws = wb.ActiveWorksheet;
  32. //first row 19cell
  33. WorksheetMergedCellsRegionCollection wm = ws.MergedCellsRegions;
  34.  
  35. WorksheetMergedCellsRegion wmc = wm.Add(, , , );//起始位置和终止位置
  36. wmc.Value = Header;
  37. wmc.CellFormat.Alignment = HorizontalCellAlignment.Center;
  38. wmc.CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
  39. wmc.CellFormat.BottomBorderColor = Color.Black;
  40. wmc.CellFormat.LeftBorderColor = Color.Black;
  41. wmc.CellFormat.RightBorderColor = Color.Black;
  42. wmc.CellFormat.TopBorderColor = Color.Black;
  43.  
  44. wmc.CellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
  45. wmc.CellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
  46. wmc.CellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
  47. wmc.CellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
  48. wmc.CellFormat.WrapText = ExcelDefaultableBoolean.True;
  49. wmc.CellFormat.Font.Name = "宋体";
  50. //字体大小
  51. wmc.CellFormat.Font.Height = ;
  52. IWorksheetCellFormat HeadCellFormat = wb.CreateNewWorksheetCellFormat();
  53. HeadCellFormat.Alignment = HorizontalCellAlignment.Center;
  54. HeadCellFormat.Font.Bold = ExcelDefaultableBoolean.True;
  55. HeadCellFormat.Font.Name = "宋体";
  56.  
  57. HeadCellFormat.BottomBorderColor = Color.Black;
  58. HeadCellFormat.LeftBorderColor = Color.Black;
  59. HeadCellFormat.RightBorderColor = Color.Black;
  60. HeadCellFormat.TopBorderColor = Color.Black;
  61.  
  62. HeadCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
  63. HeadCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
  64. HeadCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
  65. HeadCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
  66. HeadCellFormat.WrapText = ExcelDefaultableBoolean.True;
  67.  
  68. IWorksheetCellFormat ItemCellFormat = wb.CreateNewWorksheetCellFormat();
  69. //CellFormat.Alignment = HorizontalCellAlignment.Center;
  70. //CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
  71. ItemCellFormat.FillPattern = FillPatternStyle.Default;
  72. ItemCellFormat.ShrinkToFit = ExcelDefaultableBoolean.True;
  73. ItemCellFormat.BottomBorderColor = Color.Black;
  74. ItemCellFormat.LeftBorderColor = Color.Black;
  75. ItemCellFormat.RightBorderColor = Color.Black;
  76. ItemCellFormat.TopBorderColor = Color.Black;
  77.  
  78. ItemCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
  79. ItemCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
  80. ItemCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
  81. ItemCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
  82. ItemCellFormat.WrapText = ExcelDefaultableBoolean.True;
  83. ItemCellFormat.FormatString = "##,##0.00";
  84. ItemCellFormat.Font.Name = "宋体";
  85. int n;
  86. n = ;
  87. wmc = wm.Add(, n, , n++);
  88. wmc.Value = "序号";
  89. wmc.CellFormat.SetFormatting(HeadCellFormat);
  90.  
  91. wmc = wm.Add(, n, , n++);
  92. wmc.Value = "姓名";
  93. wmc.CellFormat.SetFormatting(HeadCellFormat);
  94.  
  95. wmc = wm.Add(, n, , n++);
  96. wmc.Value = "身份证号";
  97. wmc.CellFormat.SetFormatting(HeadCellFormat);
  98.  
  99. wmc = wm.Add(, n, , n++);
  100. wmc.Value = "单位名称";
  101. wmc.CellFormat.SetFormatting(HeadCellFormat);
  102.  
  103. wmc = wm.Add(, n, , n++);
  104. wmc.Value = "计划名称";
  105. wmc.CellFormat.SetFormatting(HeadCellFormat);
  106.  
  107. wmc = wm.Add(, n, , n++);
  108. wmc.Value = "报名项";
  109. wmc.CellFormat.SetFormatting(HeadCellFormat);
  110.  
  111. wmc = wm.Add(, n, , n++);
  112. wmc.Value = "准考证号";
  113. wmc.CellFormat.SetFormatting(HeadCellFormat);
  114.  
  115. wmc = wm.Add(, n, , n++);
  116. wmc.Value = "成绩";
  117. wmc.CellFormat.SetFormatting(HeadCellFormat);
  118.  
  119. ws.Columns[n].Width = * ;
  120. wmc = wm.Add(, n, , n++);
  121. wmc.Value = "是否合格";
  122. wmc.CellFormat.SetFormatting(HeadCellFormat);
  123.  
  124. DataView dv = GetExcelData();//获取报名信息
  125. for (int i = ; i < dv.Count; i++)
  126. {
  127. n = ;
  128.  
  129. wmc = wm.Add(i + , n, i + , n++);
  130. wmc.Value = Convert.ToString(i + );
  131. wmc.CellFormat.SetFormatting(ItemCellFormat);
  132.  
  133. wmc = wm.Add(i + , n, i + , n++);
  134. wmc.Value = dv[i]["Name"].ToString();
  135. wmc.CellFormat.SetFormatting(ItemCellFormat);
  136.  
  137. wmc = wm.Add(i + , n, i + , n++);
  138. wmc.Value = dv[i]["IdentityNum"].ToString();
  139. wmc.CellFormat.SetFormatting(ItemCellFormat);
  140.  
  141. wmc = wm.Add(i + , n, i + , n++);
  142. wmc.Value = dv[i]["DanWeiName"].ToString();
  143. wmc.CellFormat.SetFormatting(ItemCellFormat);
  144.  
  145. wmc = wm.Add(i + , n, i + , n++);
  146. wmc.Value = dv[i]["PlanName"].ToString();
  147. wmc.CellFormat.SetFormatting(ItemCellFormat);
  148.  
  149. wmc = wm.Add(i + , n, i + , n++);
  150. wmc.Value = dv[i]["ItemName"].ToString();
  151. wmc.CellFormat.SetFormatting(ItemCellFormat);
  152.  
  153. wmc = wm.Add(i + , n, i + , n++);
  154. wmc.Value = dv[i]["ZhunKZNum"].ToString();
  155. wmc.CellFormat.SetFormatting(ItemCellFormat);
  156.  
  157. wmc = wm.Add(i + , n, i + , n++);
  158. wmc.Value = dv[i]["Score"].ToString();
  159. wmc.CellFormat.SetFormatting(ItemCellFormat);
  160.  
  161. wmc = wm.Add(i + , n, i + , n++);
  162. wmc.Value = dv[i]["IsPass"].ToString();
  163. wmc.CellFormat.SetFormatting(ItemCellFormat);
  164.  
  165. }
  166.  
  167. string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
  168. strFileName = "Export_" + mark + ".xls";
  169. BIFF8Writer.WriteWorkbookToFile(wb, Server.MapPath("ExcelExport/" + strFileName));
  170.  
  171. return strFileName;
  172. }
  173.  
  174. protected DataView GetExcelData()
  175. {
  176. int TotalNum = ;
  177. string where = " where 1=1 ";
  178. if (!String.IsNullOrEmpty(txtIdentityNum.Text))
  179. {
  180. where += "and IdentityNum like '%" + txtIdentityNum.Text + "%'";
  181. }
  182. where += "and PlanGuid = '" + PlanGuid + "'";
  183. where += "and Status != '" + + "'";
  184. string connectionStringName = "DJG_PeiXun_ConnectionString";
  185. string fields = "*";
  186. string sortExpression = "order by Row_ID desc";
  187.  
  188. DataTable DvPaging = new DB_Common().GetData_Page_Table(
  189. fields,
  190. DataGrid1.PageSize,
  191. DataGrid1.CurrentPageIndex + ,
  192. "View_Score_UserType",
  193. "Row_ID",
  194. where,
  195. sortExpression,
  196. out TotalNum,
  197. connectionStringName
  198. );
  199. return DvPaging.DefaultView;
  200. }

C# Excel导入的更多相关文章

  1. C# Excel导入、导出【源码下载】

    本篇主要介绍C#的Excel导入.导出. 目录 1. 介绍:描述第三方类库NPOI以及Excel结构 2. Excel导入:介绍C#如何调用NPOI进行Excel导入,包含:流程图.NOPI以及C#代 ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出-自定义表模导入

    系列目录 前言 上一节使用了LinqToExcel和CloseXML对Excel表进行导入和导出的简单操作,大家可以跳转到上一节查看: ASP.NET MVC5+EF6+EasyUI 后台管理系统(6 ...

  3. 解析大型.NET ERP系统 设计通用Microsoft Excel导入功能

    做企业管理软件很难避免与Microsoft Excel打交道,常常是软件做好了,客户要求说再做一个Excel导入功能.导入Excel数据的功能的难度不大,从Excel列数据栏位的取值,验证值,再导入到 ...

  4. (转)高效的将excel导入sqlserver中

    大部分人都知道用oledb来读取数据到dataset,但是读取之后怎么处理dataset就千奇百怪了.很多人通过循环来拼接sql,这样做不但容易出错而且效率低下,System.Data.SqlClie ...

  5. 安全的将excel导入sqlite3的解决方案

    最近在做一个小项目时,需要把一个excel中的数据保存到sqlite3数据库中以备后用,表中有字符也有数字,要用到特定的数据类型方便后续使用,参照网上的方法,将excel文件转换为csv文件后,在导入 ...

  6. 利用反射实现通用的excel导入导出

    如果一个项目中存在多种信息的导入导出,为了简化代码,就需要用反射实现通用的excel导入导出 实例代码如下: 1.创建一个 Book类,并编写set和get方法 package com.bean; p ...

  7. C# EXCEL导入 混合列文字为空,找不到可安装的 ISAM的解决办法

    C# EXCEL导入 混合列文字为空,找不到可安装的 ISAM的解决办法 使用C#导入 Excel数据到 DataTable,如果连接串中只写 Excel 8.0,则正常的字符列,数值列都没有问题,但 ...

  8. Excel导入导出的业务进化场景及组件化的设计方案(上)

    1:前言 看过我文章的网友们都知道,通常前言都是我用来打酱油扯点闲情的. 自从写了上面一篇文章之后,领导就找我谈话了,怕我有什么想不开. 所以上一篇的(下)篇,目前先不出来了,哪天我异地二次回忆的时候 ...

  9. Excel 导入到Datatable 中,再使用常规方法写入数据库

    首先呢?要看你的电脑的office版本,我的是office 2013 .为了使用oledb程序,需要安装一个引擎.名字为AccessDatabaseEngine.exe.这里不过多介绍了哦.它的数据库 ...

  10. nopi excel 导入

    #region 从Excel导入 /// <summary> /// 读取excel ,默认第一行为标头 /// </summary> /// <param name=& ...

随机推荐

  1. Unity3D之协程(Coroutines & Yield )

    在Unity中StartCoroutine/yield return这个模式到底是怎么应用的? 比如你要一个方法进行一个比较耗时的复杂运算~同时又想让脚本流畅的进行其他操作而不是卡在那里等该方法执行完 ...

  2. SQL Prompt自定义代码片段

    新增代码片段: 代码片段管理: 代码片段中可以使用以下占位符:详见参考: $DATE$ 插入当前日期. $TIME$ 插入当前时间 $USER$ 插入当前电脑的用户名 $PASTE$ 插入剪切板内容 ...

  3. loadrunner常用术语

    1.场景 在loadrunner中主要表现为controller中设计与执行测试用例中的用户场景.主要工作有,在controller中选择虚拟用户脚本.设置虚拟用户数量.配置虚拟用户运行时的行为.选择 ...

  4. awk命令简单介绍

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  5. 19 Using Optimizer Hints

    19.1 Overview of Optimizer Hints A hint is an instruction to the optimizer. In a test or development ...

  6. 匿名函数和Lamda

    不是本人所写!网络收集 C#中的匿名函数和Lamda是很有意思的东东,那么我们就来介绍一下,这到底是什么玩意,有什么用途了? 打开visual studio 新建一个控制台程序. 我们利用委托来写一个 ...

  7. Postman 安装及使用入门教程

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  8. C#_批量插入数据到Sqlserver中的四种方式

    先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生成一个GUID算法所花的时间肯定比你从数据表中重新查询上一条记 ...

  9. News common vocabulary

    英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 ...

  10. iOS7中的ViewController切换

    转自:https://onevcat.com/2013/10/vc-transition-in-ios7/ iOS 7 SDK之前的VC切换解决方案 在深入iOS 7的VC切换效果的新API实现之前, ...