如何从excel到datatable获取值? [英] How to get the values from excel to datatable?

查看:78
本文介绍了如何从excel到datatable获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一张Excel表格,其中有一些列固定了一些列,一些列是动态的,这意味着我有一个学生如果班级改变了学生的数量也改变了。所以我在这里遇到问题我写了如下代码



Hi,

I have an Excel Sheet in which there are some columns in which some columns are fixed and some columns are dynamic that means i have a students in class if the class changes the number of student also changes.So i have a problem here i wrote the code as below

foreach (DataRow row in dtSub.Rows)
                {
                    objSubExamSubjectMarks = new SpGetAllStudentSubExamMarks_Result();
                    if (row["ExamSubMapId"] != DBNull.Value)
                        objSubExamSubjectMarks.ExamSubMapId = Convert.ToInt32(row["ExamSubMapId"]);
                    if (row["CourseId"] != DBNull.Value)
                        objSubExamSubjectMarks.CourseId = Convert.ToInt32(row["CourseId"]);
                    if (row["ExamCourseMappingId"] != DBNull.Value)
                        objSubExamSubjectMarks.ExamCourseMappingId = Convert.ToInt32(row["ExamCourseMappingId"]);
                    if (row["ExamTypeId"] != DBNull.Value)
                        objSubExamSubjectMarks.ExamTypeId = Convert.ToInt32(row["ExamTypeId"]);
                    if (row["SubjectId"] != DBNull.Value)
                        objSubExamSubjectMarks.SubjectId = Convert.ToInt32(row["SubjectId"]);
                    if (row["SubSectionId"] != DBNull.Value)
                        objSubExamSubjectMarks.SubSectionId = Convert.ToInt32(row["SubSectionId"]);
                    if ((row["Description"] != DBNull.Value) && (!string.IsNullOrEmpty(row["Description"].ToString())))
                        objSubExamSubjectMarks.Description = Convert.ToString(row["Description"]);
                    List<student> lstStudents =
                        objGenlogic.getAllStudents(Convert.ToDateTime(ddlBulkAcad.SelectedValue))
                            .FindAll(
                                F =>F.SectionId == Convert.ToInt32(ddlBulkSec.SelectedValue) &&
                                    F.CourseId == Convert.ToInt32(row["CourseId"]));
                    objSubExamSubjectMarks.AcademicYear = ddlBulkAcad.SelectedValue;
                    foreach (Student objStudent in lstStudents)
                    {

                        if (row[objStudent.FullName + "-" + objStudent.StudentId] != DBNull.Value)
                            objSubExamSubjectMarks.MarksObtained =
                                Convert.ToString(row[objStudent.FullName + "-" + objStudent.StudentId]);
                        objSubExamSubjectMarks.StudentId = objStudent.StudentId;
                        
                        lstSubExamData.Add(objSubExamSubjectMarks);
                    }

                }



这里我的问题是我将结果保存在lstSubExamData中但它显示的是所有的相同的studentid列表中的记录。

任何人都可以帮助我吗?

提前致谢


My Problem here is i am saving the results in lstSubExamData but it is showing the same studentid for all the records in the list.
Can anyone help me?
Thanks in advance

推荐答案

foreach (Student objStudent in lstStudents)
              {
                  foreach (DataRow row in dtSub.Rows)
                  {
                      objSubExamSubjectMarks = new SpGetAllStudentSubExamMarks_Result();


                      if (row["ExamSubMapId"] != DBNull.Value)
                          objSubExamSubjectMarks.ExamSubMapId = Convert.ToInt32(row["ExamSubMapId"]);
                      if (row["CourseId"] != DBNull.Value)
                          objSubExamSubjectMarks.CourseId = Convert.ToInt32(row["CourseId"]);
                      if (row["ExamCourseMappingId"] != DBNull.Value)
                          objSubExamSubjectMarks.ExamCourseMappingId = Convert.ToInt32(row["ExamCourseMappingId"]);
                      if (row["ExamTypeId"] != DBNull.Value)
                          objSubExamSubjectMarks.ExamTypeId = Convert.ToInt32(row["ExamTypeId"]);
                      if (row["SubjectId"] != DBNull.Value)
                          objSubExamSubjectMarks.SubjectId = Convert.ToInt32(row["SubjectId"]);
                      if (row["SubSectionId"] != DBNull.Value)
                          objSubExamSubjectMarks.SubSectionId = Convert.ToInt32(row["SubSectionId"]);
                      if ((row["Description"] != DBNull.Value) && (!string.IsNullOrEmpty(row["Description"].ToString())))
                          objSubExamSubjectMarks.Description = Convert.ToString(row["Description"]);

                      objSubExamSubjectMarks.AcademicYear = ddlBulkAcad.SelectedValue;


                      if (row[objStudent.FullName + "-" + objStudent.StudentId] != DBNull.Value)
                          objSubExamSubjectMarks.MarksObtained =
                              Convert.ToString(row[objStudent.FullName + "-" + objStudent.StudentId]);
                      objSubExamSubjectMarks.StudentId = objStudent.StudentId;

                      lstSubExamData.Add(objSubExamSubjectMarks);
                  }

              }


这篇关于如何从excel到datatable获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆