Excel 导入 显示

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
BLL

using System.Data.OleDb;
using System.Text;
namespace PowerEasy.SiteFactory.Bll.Ssms
{
    /// <summary>
    /// Score BLL Layer
    /// </summary>
    public sealed class Score
    {
        private static readonly IScore dal = DataAccess.CreateScore();
        private static string s_MessgeOfBatchImport;
        private Score() { }

        #region
        /// <summary>
        /// 添加成绩
        /// </summary>
        /// <param name="scoreInfo">成绩实体</param>
        /// <returns></returns>
        public static bool Add(ScoreInfo scoreInfo)
        {
            return dal.Add(scoreInfo);
        }
        #endregion

        #region
        /// <summary>
        /// 修改成绩
        /// </summary>
        /// <param name="scoreInfo">成绩实体</param>
        /// <returns></returns>  
        public static bool Update(ScoreInfo scoreInfo)
        {
            return dal.Update(scoreInfo);
        }
        #endregion

        #region
        /// <summary>
        /// 通过成绩ID删除成绩
        /// </summary>
        /// <param name="scoreId">成绩实体</param>
        /// <returns></returns>
        public static bool Delete(int scoreId)
        {
            return dal.Delete(scoreId);
        }
        #endregion

        #region 批量删除目标分
        /// <summary>
        /// 批量删除目标分
        /// </summary>
        /// <param name="id">目标分Id</param>
        /// <returns>删除成功返回true;失败返回false</returns>
        public static bool Delete(string id)
        {
            if (!DataValidator.IsValidId(id))
            {
                return false;
            }
            bool isok = true;
            int ScoreId = 0;

            string[] arrScoreId = id.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            ScoreInfo ScoreInfo;
            foreach (string i in arrScoreId)
            {
                ScoreId = DataConverter.CLng(i);
                ScoreInfo = ScoreBll.GetScoreById(ScoreId);
                if (Delete(ScoreId))
                {
                }
                else
                {
                    isok = false;
                    break;
                }
            }
            return isok;
        }
        #endregion

        #region
        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static ScoreInfo GetScoreById(int id)
        {
            return dal.GetScoreById(id);
        }
        #endregion

        #region
        /// <summary>
        /// 成绩列表
        /// </summary>
        /// <param name="startRowIndexId"></param>
        /// <param name="maxNumberRows"></param>
        /// <param name="testId">考试ID</param>
        /// <returns></returns>
        public static IList<ScoreInfo> GetList(int startRowIndexId, int maxNumberRows, int testId)
        {
            return dal.GetList(startRowIndexId, maxNumberRows, testId);
        }
        #endregion

        #region
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static int GetTotalOfScore()
        {
            return dal.GetTotalOfScore();
        }
        #endregion

        /// <summary>
        /// 根据考试,科目,学生来找到成绩
        /// </summary>
        /// <param name="testId">考试id</param>
        /// <param name="subjectId">科目ID</param>
        /// <param name="studentId">学生ID</param>
        public static float GetScoreBStudentId(int testId, int subjectId, int studentId)
        {
            return dal.GetScoreBStudentId(testId, subjectId, studentId);
        }

        /// <summary>
        /// 根据考试、科目、学生判断是否已经存在记录
        /// </summary>
        /// <param name="testId">考试id</param>
        /// <param name="subjectId">科目ID</param>
        /// <param name="studentId">学生ID</param>
        /// <returns></returns>
        public static bool FindScore(int testId, int subjectId, int studentId)
        {
            return dal.FindScore(testId, subjectId, studentId);
        }

        /// <summary>
        /// 通过TESTID查找studentID
        /// </summary>
        /// <param name="testId"></param>
        /// <returns></returns>
        public static ArrayList GetStudentIdByTestId(int testId)
        {
            return dal.GetStudentIdByTestId(testId);
        }

        /// <summary>
        /// 通过studentId查找学校ID
        /// </summary>
        /// <param name="studentId"></param>
        /// <returns></returns>
        public static int GetSchoolIdByTestId(int studentId)
        {
            return dal.GetSchoolIdByTestId(studentId);
        }

        /// <summary>
        /// 获取ScoreId
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="studentId"></param>
        /// <param name="subjectId"></param>
        /// <returns></returns>
        public static int GetScoreIddBy(int testId, int studentId, int subjectId)
        {
            return dal.GetScoreIddBy(testId, studentId, subjectId);
        }

        /// <summary>
        /// 获取新的成绩ID
        /// </summary>
        /// <returns>返回新的成绩ID</returns>
        public static int GetNewScoreId()
        {
            return dal.GetNewScoreId();
        }

        /// <summary>
        /// 得到Excel表字段
        /// </summary>
        /// <param name="excelSource">数据源</param>
        /// <returns>字段数组</returns>
        public static ArrayList GetExcelFields(string excelSource)
        {
            ArrayList al = new ArrayList();
            al.Add("不导入此项");
            string excelconnstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelSource + "";
            excelconnstring += @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
            OleDbConnection excelconn = new OleDbConnection(excelconnstring);
            OleDbDataAdapter mycomm = new OleDbDataAdapter("select * from [sheet1$]", excelconn);
            DataSet myds = new DataSet();
            try
            {
                mycomm.Fill(myds, "ss");
                for (int i = 0; i < myds.Tables[0].Columns.Count; i++)
                {
                    al.Add(myds.Tables[0].Columns[i].ColumnName);
                }
            }
            catch (OleDbException)
            {

            }
            finally
            {
                myds.Dispose();
                mycomm.Dispose();
                excelconn.Dispose();
            }

            return al;
        }

        /// <summary>
        /// 成绩批量导入
        /// </summary>
        /// <param name="dtField">字段对应信息</param>
        /// <param name="filePath">数据源文件路径</param>
        /// <param name="classId"></param>
        /// <param name="gradeId"></param>
        /// <param name="schoolId"></param>
        /// <param name="testId"></param>
        /// <param name="fildsName"></param>
        /// <returns>返回成功导入的成绩个数</returns>
        public static int BatchImport(DataTable dtField, string filePath, int schoolId, int gradeId, int classId, int testId, string fildsName)
        {
            bool isOk;
            bool statu = false;
            ScoreInfo scoreInfo = new ScoreInfo();
            int id, okNumber;
            string excelconnstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath;
            excelconnstring += @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
            OleDbConnection excelconn = new OleDbConnection(excelconnstring);
            string sql = "select * from [sheet1$]";
            OleDbDataAdapter comm = new OleDbDataAdapter(sql, excelconn);
            DataSet ds = new DataSet();
            try
            {
                comm.Fill(ds, "ss");
            }
            catch (OleDbException)
            {

            }

            StringBuilder errorMsg = new StringBuilder();
            okNumber = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow rows = ds.Tables[0].Rows[i];
                scoreInfo.StudentId = StudentBll.GetStudentIdByStudentNumClassId(GetExcelFieldValue(rows, dtField, "学号", string.Empty), classId);
                if (scoreInfo.StudentId==0)
                {
                    errorMsg.Append("<li>该班级找不到学号为:" + GetExcelFieldValue(rows, dtField, "学号", string.Empty) + "的学生ID,未导入!</li>");
                    continue;
                }
                scoreInfo.SchoolId = schoolId;
                scoreInfo.GradeId = gradeId;
                scoreInfo.ClassId = classId;
                scoreInfo.TestId = testId;
                string[] filds = fildsName.Split(',');
                for (int j = 1; j < filds.Length; j++)
                {
                    id = GetNewScoreId();
                    scoreInfo.ScoreId = id;
                    scoreInfo.SubjectId = Subject.GetSubjectIdBySubjectsName(filds[j]);
                    scoreInfo.Score = DataConverter.CSingle(GetExcelFieldValue(rows, dtField, filds[j], string.Empty));
                    statu = ScoreBll.FindScore(testId, scoreInfo.SubjectId, scoreInfo.StudentId);
                    if (statu)
                    {
                        scoreInfo.ScoreId = ScoreBll.GetScoreIddBy(testId, scoreInfo.StudentId, scoreInfo.SubjectId);
                        isOk = Update(scoreInfo);
                    }
                    else
                    {
                        isOk = Add(scoreInfo);
                    }
                   
                }
                okNumber = okNumber + 1;
            }
            s_MessgeOfBatchImport = errorMsg.ToString();
            ds.Dispose();
            comm.Dispose();
            excelconn.Dispose();
            return okNumber;
        }

        private static string GetExcelFieldValue(DataRow rows, DataTable dtField, string fieldName, string defaultValue)
        {
            string excelFieldValue = defaultValue;
            string excelField = "不导入此项";
            DataRow[] dr = dtField.Select("FieldName='" + fieldName + "'");
            if (dr.Length != 0)
            {
                excelField = dr[0][0].ToString();
            }
            if (string.Compare("不导入此项", excelField, StringComparison.CurrentCulture) != 0)
            {
                excelFieldValue = rows[excelField].ToString();
                if (string.Compare(fieldName, "stars", true) == 0)
                {
                    int stars = DataConverter.CLng(excelFieldValue);
                    if ((stars < 1) || (stars > 5))
                    {
                        excelFieldValue = defaultValue;
                    }
                }
            }
            if (string.IsNullOrEmpty(excelFieldValue))
            {
                excelFieldValue = defaultValue;
            }
            return excelFieldValue;
        }

        /// <summary>
        /// 成绩批量导入的反馈信息
        /// </summary>
        public static string MessgeOfBatchImport
        {
            get { return s_MessgeOfBatchImport; }
        }


        /// <summary>
        /// 创建分数列表
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="type">查询类型</param>
        /// <param name="key">学号查询条件</param>
        /// <param name="schoolId">学校ID</param>
        /// <param name="subjectstrs">选择科目</param>
        /// <param name="subjectstrname">选择科目名称</param>
        /// <returns></returns>
        public static ICollection CreateDataSource(int testId, int type, string key, int schoolId, string subjectstrs, string subjectstrname)
        {
            return dal.CreateDataSource(testId, type, key, schoolId, subjectstrs, subjectstrname);
        }

        /// <summary>
        /// 创建分数表
        /// </summary>
        /// <param name="testId"></param>
        /// <param name="classId">班级查询条件</param>
        /// <param name="gradeId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DataTable CreateDataSource2(int testId, int gradeId, int classId, string type)
        {
            return dal.CreateDataSource2(testId, gradeId, classId, type);
        }

        /// <summary>
        /// 根据科目查找历次考试成绩
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="subjectId">选择科目</param>
        /// <returns></returns>
        public static DataTable CreateDataSourceBySubject(int studentId, int subjectId)
        {
            return dal.CreateDataSourceBySubject(studentId, subjectId);
        }

        /// <summary>
        /// 根据条件判断成绩是否存在
        /// </summary>
        /// <param name="subjectId">科目ID</param>
        /// <param name="examinationId">考试ID</param>
        /// <returns></returns>
        public static bool IsExistsScore(int subjectId, int examinationId)
        {
            return dal.IsExistsScore(subjectId, examinationId);
        }
    }
}


 //ASPX

 

<%@ Page Language="C#" MasterPageFile="~/Admin/MasterPage.master" AutoEventWireup="true"
    CodeBehind="ScoreImport.aspx.cs" Inherits="PowerEasy.SiteFactory.WebSite.Admin.Ssms.ScoreImport"
    Title="成绩批量导入" %>

<%@ Register Assembly="System.Web.Extensions" Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="CphNavigation" runat="server">
    <pe:SsmsSiteMapPath ID="SmpNavigator" runat="server" SiteMapProvider="AdminMapProvider">
    </pe:SsmsSiteMapPath>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CphContent" runat="server">
    <div style="padding-top: 5px;">
        <asp:ScriptManager ID="ScriptManager2" runat="server">
        </asp:ScriptManager>
        <asp:Wizard ID="WzdStudentImport" Font-Names="Verdana" Font-Size="0.8em" runat="server"
            DisplaySideBar="False" Width="100%" ActiveStepIndex="0" OnFinishButtonClick="WzdStudentImport_FinishButtonClick"
            OnNextButtonClick="WzdStudentImport_NextButtonClick" FinishCompleteButtonText=" 完 成 "
            OnPreviousButtonClick="WzdScore_PreviousButtonClick" CancelButtonText=" 取 消 ">
            <WizardSteps>
                <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
                    <table width="100%" border="0" cellspacing="1" cellpadding="2" class="border">
                        <tr align="center">
                            <td colspan="3" class="title" style="height: 19px">
                                <b>成绩批量导入(第一步)------选择导入成绩的条件</b>
                            </td>
                        </tr>
                        <tr class="tdbg">
                            <td width="30%" align="right" class="tdbgleft">
                                <strong>请选择考试:</strong>
                            </td>
                            <td>
                                <asp:DropDownList ID="DropTest" runat="server" DataTextField="TestName" DataValueField="TestId"
                                    AutoPostBack="true" OnSelectedIndexChanged="DropTest_SelectedIndexChanged">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr class="tdbg">
                            <td width="30%" align="right" class="tdbgleft">
                                <strong>请输入查询条件:</strong><br />
                            </td>
                            <td valign="top">
                                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                    <ContentTemplate>
                                        学校:<asp:DropDownList ID="dropSchool" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropSchool_SelectedIndexChanged">
                                        </asp:DropDownList>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                    <ContentTemplate>
                                        年级:<asp:DropDownList ID="dropGrade" runat="server" DataTextField="GradeName" DataValueField="GradeId"
                                            AutoPostBack="true" OnSelectedIndexChanged="DropGrade_SelectedIndexChanged">
                                        </asp:DropDownList>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                                <asp:UpdatePanel ID="UpdatePanel4" runat="server">
                                    <ContentTemplate>
                                        班级:<asp:DropDownList ID="dropClass" runat="server" DataTextField="ClassName" DataValueField="ClassId">
                                        </asp:DropDownList>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr class="tdbg">
                            <td align="right" class="tdbgleft">
                                <strong>考试科目:</strong>
                            </td>
                            <td colspan="2">
                                <asp:Panel ID="Panel1" runat="server">
                                </asp:Panel>
                            </td>
                        </tr>
                    </table>
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
                    <table width="100%" border="0" cellspacing="1" cellpadding="2" class="border">
                        <tr align="center">
                            <td colspan="2" class="title" style="height: 19px">
                                <b>成绩批量导入(第二步)-------选择导入文件</b>
                            </td>
                        </tr>
                        <tr class="tdbg">
                            <td align="right" style="width: 30%;" class="tdbgleft">
                                <b>选择要导入的数据文件&nbsp;:&nbsp;</b>
                            </td>
                            <td>
                                <asp:FileUpload ID="UpExcellFile" runat="server" Width="400px" />
                            </td>
                        </tr>
                    </table>
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep3" runat="server" Title="Step 3">
                    <table width="100%" border="0" cellspacing="1" cellpadding="2" class="border">
                        <tr align="center">
                            <td colspan="2" class="title">
                                <b>成绩批量导入(第三步)------(选择导入项)</b>
                            </td>
                        </tr>
                        <asp:Repeater ID="RepeaterExcelFields" runat="server" OnItemDataBound="RepeaterExcelFields_ItemDataBound">
                            <ItemTemplate>
                                <tr id="Tr1" class="tdbg" runat="server">
                                    <td align="right" style="width: 50%;" class="tdbgleft">
                                        <%--<asp:PlaceHolder ID="PlaceHolderExcelField" runat="server"></asp:PlaceHolder>--%>
                                        <asp:DropDownList ID="DropExcelFields" runat="server">
                                        </asp:DropDownList>
                                        &nbsp;&nbsp;==>&nbsp;&nbsp;
                                    </td>
                                    <td>
                                        &nbsp;&nbsp;&nbsp;<asp:Literal ID="LiteralTableField" runat="server"></asp:Literal>
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:Repeater>
                    </table>
                </asp:WizardStep>
            </WizardSteps>
            <StartNavigationTemplate>
                <center>
                    <br />
                    <asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext" Text="下一步" /></center>
                </center>
            </StartNavigationTemplate>
            <StepNavigationTemplate>
                <center>
                    <br />
                    <asp:Button ID="StepMidPreButton" runat="server" CommandName="MovePrevious" Text="上一步" />
                    <asp:Button ID="StepMidNextButton" runat="server" CommandName="MoveNext" Text="下一步" /></center>
                </center>
            </StepNavigationTemplate>
            <FinishNavigationTemplate>
                <center>
                    <br />
                    <asp:Button ID="StartPreButton" runat="server" CommandName="MovePrevious" Text="上一步" />
                    <asp:Button ID="FinishButton" runat="server" CommandName="MoveComplete" Text="导入" /></center>
            </FinishNavigationTemplate>
        </asp:Wizard>
    </div>
    <br />
    <table style="width: 100%; margin: 0 auto;" cellpadding="2" cellspacing="1" class="border">
        <tr>
            <td class="spacingtitle" colspan="2" align="center">
                <strong>相关说明</strong>
            </td>
        </tr>
        <tr class="tdbg">
            <td>
                <ajaxToolkit:TabContainer runat="server" ID="Tabs" Height="150px">
                    <ajaxToolkit:TabPanel runat="Server" ID="TabPanel" HeaderText="成绩批量导入说明">
                        <ContentTemplate>
                            <ul>
                                <span style="color: #ff7300; font-weight: bold;">温馨提醒:</span>
                                <li>导入前先<a style="color: Blue; text-decoration: underline;" href="http://www.cnblogs.com/Admin/Ssms/ScoreTemplate.aspx">生成导入成绩模板</a>。</li>
                                <li>模板文件中的学号为必填项</li>
                                <li>按照生成模板输入成绩表</li>
                                <li>保存成绩的时,如果已添加有该成绩记录,将会自动覆盖原记录</li>
                                <li>由于批量导入操作比较占用资源,请不要上传过大的文件,建议在服务器访问人数少时进行!</li>
                        </ContentTemplate>
                    </ajaxToolkit:TabPanel>
                </ajaxToolkit:TabContainer>
            </td>
        </tr>
    </table>
</asp:Content>

 

 

//CS

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using PowerEasy.Framework.Common;
using PowerEasy.SiteFactory.Model.Ssms;
using PowerEasy.SiteFactory.Web.UI;
using ExaminationSubjectBll = PowerEasy.SiteFactory.Bll.Ssms.ExaminationSubject;
using GradeBll = PowerEasy.SiteFactory.Bll.Ssms.Grade;
using SchoolBll = PowerEasy.SiteFactory.Bll.Ssms.School;
using ScoreBll = PowerEasy.SiteFactory.Bll.Ssms.Score;
using StudentClassBll = PowerEasy.SiteFactory.Bll.Ssms.StudentClass;
using SubjectBll = PowerEasy.SiteFactory.Bll.Ssms.Subject;
using TestBll = PowerEasy.SiteFactory.Bll.Ssms.Examination;
using PowerEasy.Framework.Components;
using PowerEasy.SiteFactory.Bll.Ssms;

namespace PowerEasy.SiteFactory.WebSite.Admin.Ssms
{
    public partial class ScoreImport : AdminPage
    {
        private bool IsSuperAdmin = PEContext.Current.Admin.IsSuperAdmin;
        private bool IsShowRigetMenu = false;
        private bool IsManageRigetMenu = false;
        private string FileSource
        {
            get
            {
                return ViewState["FileSource"].ToString();
            }
            set
            {
                ViewState["FileSource"] = value;
            }
        }

        private DataTable TableFields
        {
            get
            {
                return (DataTable)ViewState["TableFields"];
            }
            set
            {
                ViewState["TableFields"] = value;
            }
        }

        private ArrayList ExcelFields
        {
            get
            {
                return (ArrayList)ViewState["ExcelFields"];
            }
            set
            {
                ViewState["ExcelFields"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                DropTestBind();
                DropSchoolIdBind();
                SowSubjects(DataConverter.CLng(DropTest.SelectedValue));
                if (dropSchool.SelectedIndex != (dropSchool.Items.IndexOf(dropSchool.Items.FindByText("没有学校可以选择"))))
                {
                    DropGradeIdBind();
                    if (dropGrade.SelectedIndex != (dropGrade.Items.IndexOf(dropGrade.Items.FindByText("没有年级可以选择"))))
                    {
                        DropClassBind();
                        if (dropClass.SelectedIndex != (dropClass.Items.IndexOf(dropClass.Items.FindByText("没有班级可以选择"))))
                        {
                            ChangeGrade(Convert.ToInt32(dropSchool.SelectedValue));
                            DropClassBind(Convert.ToInt32(dropGrade.SelectedValue));
                        }
                    }
                }
            }
        }

        private void DropTestBind()
        {
            Dictionary<int, string> tesstDictionary = TestBll.GetTestList();
            if (tesstDictionary.Count > 0)
            {
                DropTest.DataSource = tesstDictionary;
                DropTest.DataValueField = "key";
                DropTest.DataTextField = "value";
                DropTest.DataBind();
            }
            else
            {
                DropTest.Items.Clear();
                DropTest.Items.Insert(DropTest.Items.Count, new ListItem("没有考试可以选择"));
            }
        }

        private void DropSchoolIdBind()
        {
            Dictionary<int, string> schoolDictionary = SchoolBll.GetSchoolList();
            foreach (int i in new List<int>(schoolDictionary.Keys))
            {
                IsShowRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(i, 1) == 3) ? true : false;
                if (!IsSuperAdmin && IsShowRigetMenu)
                {
                    schoolDictionary.Remove(i);
                }
            }
            if (schoolDictionary.Count > 0)
            {
                dropSchool.DataSource = schoolDictionary;
                dropSchool.DataValueField = "key";
                dropSchool.DataTextField = "value";
                dropSchool.DataBind();
            }
            else
            {
                dropSchool.Items.Clear();
                dropSchool.Items.Insert(dropSchool.Items.Count, new ListItem("没有学校可以选择"));
            }
        }

        private void DropGradeIdBind()
        {
            Dictionary<int, string> gradeDictionary = GradeBll.GetGradeListByGradeId();
            foreach (int i in new List<int>(gradeDictionary.Keys))
            {
                IsShowRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(i, 2) == 3) ? true : false;
                if (!IsSuperAdmin && IsShowRigetMenu)
                {
                    gradeDictionary.Remove(i);
                }
            }
            if (gradeDictionary.Count > 0)
            {
                dropGrade.DataSource = gradeDictionary;
                dropGrade.DataValueField = "key";
                dropGrade.DataTextField = "value";
                dropGrade.DataBind();
            }
            else
            {
                dropGrade.Items.Clear();
                dropGrade.Items.Insert(dropGrade.Items.Count, new ListItem("没有年级可以选择"));
            }
        }
        private void DropClassBind()
        {
            IList<StudentClassInfo> studentInfolist = StudentClassBll.GetList(0, int.MaxValue, DataConverter.CLng(dropGrade.SelectedValue));
            for (int i = 0; i < studentInfolist.Count; i++)
            {
                IsShowRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(studentInfolist[i].ClassId, 3) == 3) ? true : false;
                IsManageRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(studentInfolist[i].ClassId, 3) == 2) ? true : false;
                if (!IsSuperAdmin && (IsShowRigetMenu || IsManageRigetMenu))
                {
                    studentInfolist.Remove(studentInfolist[i]);
                }
            }
            if (studentInfolist.Count > 0)
            {
                dropClass.DataSource = studentInfolist;
                dropClass.DataBind();
            }
            else
            {
                dropClass.Items.Clear();
                dropClass.Items.Insert(dropClass.Items.Count, new ListItem("没有班级可以选择"));
            }
        }

        protected void SowSubjects(int test)
        {
            int Repeat2__numRows = 1;
            ArrayList subjectArrayList = ExaminationSubjectBll.GetSubjectIdByTestId(test);
            StringBuilder jsBuilder = new StringBuilder();
            jsBuilder.Append("<table border='0' cellspacing='0' cellpadding='3'><tr>");
            for (int i = 0; subjectArrayList.Count > i; i++)
            {
                if (Repeat2__numRows != 0)
                {
                    string s = SubjectBll.GetSubjectsNameBysubjectId(DataConverter.CLng(subjectArrayList[i]));
                    jsBuilder.Append("<td><input name='chkbox' type='checkbox' value='" + s + "' checked>");
                    jsBuilder.Append("" + s + "");
                    if ((Repeat2__numRows % 8) == 0)
                    {
                        jsBuilder.Append("</tr><tr>");
                    }
                    Repeat2__numRows = Repeat2__numRows + 1;
                }
            }
            jsBuilder.Append("</tr></table>");
            Literal l = new Literal();
            l.Text = jsBuilder.ToString();
            Panel1.Controls.Add(l);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="gradeId"></param>
        private void DropClassBind(int gradeId)
        {
            IList<StudentClassInfo> studentInfolist = StudentClassBll.GetList(0, int.MaxValue, gradeId);
            for (int i = 0; i < studentInfolist.Count; i++)
            {
                IsShowRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(studentInfolist[i].ClassId, 3) == 3) ? true : false;
                IsManageRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(studentInfolist[i].ClassId, 3) == 2) ? true : false;
                if (!IsSuperAdmin && (IsShowRigetMenu || IsManageRigetMenu))
                {
                    studentInfolist.Remove(studentInfolist[i]);
                }
            }
            if (studentInfolist.Count > 0)
            {
                dropClass.DataSource = studentInfolist;
                dropClass.DataBind();
            }
            else
            {
                dropClass.Items.Clear();
                dropClass.Items.Insert(dropClass.Items.Count, new ListItem("没有班级可以选择"));
            }
        }


        /// <summary>
        /// 根据学校显示年级
        /// </summary>
        /// <param name="schoolId"></param>
        protected void ChangeGrade(int schoolId)
        {
            Dictionary<int, string> gradeDictionary = GradeBll.GetGradeListBySchoolId(schoolId, "在读");
            foreach (int i in new List<int>(gradeDictionary.Keys))
            {
                IsShowRigetMenu = (SsmsAdminCache.ShowRigetMenuStatus(i, 2) == 3) ? true : false;
                if (!IsSuperAdmin && IsShowRigetMenu)
                {
                    gradeDictionary.Remove(i);
                }
            }
            if (gradeDictionary.Count > 0)
            {
                dropGrade.DataSource = gradeDictionary;
                dropGrade.DataValueField = "key";
                dropGrade.DataTextField = "value";
                dropGrade.DataBind();
            }
            else
            {
                dropGrade.Items.Clear();
                dropGrade.Items.Insert(dropGrade.Items.Count, new ListItem("没有年级可以选择"));
            }
        }

        protected void DropSchool_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChangeGrade(Convert.ToInt32(dropSchool.SelectedValue));
            if (dropGrade.SelectedIndex == (dropGrade.Items.IndexOf(dropGrade.Items.FindByText("没有年级可以选择"))))
            {
                dropClass.Items.Clear();
                dropClass.Items.Insert(dropClass.Items.Count, new ListItem("没有班级可以选择"));
            }
            else
            {
                DropClassBind(Convert.ToInt32(dropGrade.SelectedValue));
            }
        }

        protected void DropGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropClassBind(Convert.ToInt32(dropGrade.SelectedValue));
        }

        protected void DropTest_SelectedIndexChanged(object sender, EventArgs e)
        {
            SowSubjects(DataConverter.CLng(DropTest.SelectedValue));
        }

        protected void WzdStudentImport_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (dropClass.SelectedIndex != (dropClass.Items.IndexOf(dropClass.Items.FindByText("没有班级可以选择"))))
            {
                string subjectstrs = "学号";
                if (e.CurrentStepIndex == 0)
                {
                    if (!string.IsNullOrEmpty(Request["chkbox"]))
                    {
                        ViewState["SchoolId"] = DataConverter.CLng(dropSchool.SelectedValue);
                        ViewState["GradeId"] = DataConverter.CLng(dropGrade.SelectedValue);
                        ViewState["ClassId"] = DataConverter.CLng(dropClass.SelectedValue);
                        ViewState["TestId"] = DataConverter.CLng(DropTest.SelectedValue);
                        subjectstrs = "学号," + Request["chkbox"].ToString();
                        ViewState["subjec"] = subjectstrs;
                    }
                    else
                    {
                        WriteErrMsg("请选择科目");
                    }
                }
                if (e.CurrentStepIndex == 1)
                {
                    if (!UpExcellFile.HasFile)
                    {
                        WriteErrMsg("<li>请选择你要上传的文件!</li>");
                    }
                    int fileNamePath = UpExcellFile.PostedFile.FileName.LastIndexOf("\\", StringComparison.Ordinal);
                    if (fileNamePath != -1)
                    {
                        FileSource = Server.MapPath("~/Temp/" + UpExcellFile.PostedFile.FileName.Substring(fileNamePath));
                    }
                    else
                    {
                        FileSource = Server.MapPath("~/Temp/" + UpExcellFile.PostedFile.FileName);
                    }
                    UpExcellFile.PostedFile.SaveAs(FileSource);

                    string[] fieldsDescription = ViewState["subjec"].ToString().Split(',');
                    TableFields = GetTableFields(fieldsDescription);
                    List<string> fieldsDesc = new List<string>(fieldsDescription);
                    ExcelFields = ScoreBll.GetExcelFields(FileSource);
                    if (ExcelFields.Count == 1)
                    {
                        WriteErrMsg("导入的数据文件有错误或输入了错误的文件类型,请从新选择正确的数据文件!");
                    }
                    RepeaterExcelFields.DataSource = fieldsDesc;
                    RepeaterExcelFields.DataBind();
                }
            }
            else
            {
                WriteErrMsg("请选择班级");
            }

        }

        protected void RepeaterExcelFields_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal litExcel = (Literal)e.Item.FindControl("LiteralTableField");
                DropDownList dropExcelFields = (DropDownList)e.Item.FindControl("DropExcelFields");
                litExcel.Text = e.Item.DataItem.ToString();
                dropExcelFields.DataSource = ExcelFields;
                for (int i = 0; i < ExcelFields.Count; i++)
                {
                    if (string.Compare(ExcelFields[i].ToString(), e.Item.DataItem.ToString(), StringComparison.Ordinal) == 0)
                    {
                        dropExcelFields.SelectedValue = ExcelFields[i].ToString();
                    }
                }
                dropExcelFields.DataBind();
            }
        }

        protected void WzdScore_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (e.CurrentStepIndex == 1)
            {
                SowSubjects(DataConverter.CLng(DropTest.SelectedValue));
            }
        }

        protected void WzdStudentImport_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            DataTable dtField = new DataTable();
            dtField.Columns.Add("FieldValueName", typeof(string));
            dtField.Columns.Add("FieldName", typeof(string));

            for (int i = 0; i < RepeaterExcelFields.Items.Count; i++)
            {
                string selectedValue = ((DropDownList)RepeaterExcelFields.Items[i].FindControl("DropExcelFields")).SelectedValue;

                DataRow dr = dtField.NewRow();
                string description = ((Literal)RepeaterExcelFields.Items[i].FindControl("LiteralTableField")).Text;
                dr[0] = selectedValue;
                DataTable dt = TableFields;
                DataRow[] drs = dt.Select("Description='" + description + "'");
                if (drs.Length != 0)
                {
                    dr[1] = drs[0][0];
                }
                dtField.Rows.Add(dr);

            }
            string fildsName = ViewState["subjec"].ToString();
            int schoolId = DataConverter.CLng(ViewState["SchoolId"]);
            int gradeId = DataConverter.CLng(ViewState["GradeId"]);
            int classId = DataConverter.CLng(ViewState["ClassId"]);
            int testId = DataConverter.CLng(ViewState["TestId"]);
            int okNumber = ScoreBll.BatchImport(dtField, FileSource, schoolId, gradeId, classId, testId, fildsName);

            WriteSuccessMsg("成功导入" + okNumber.ToString() + "个学生成绩记录!" + ScoreBll.MessgeOfBatchImport, "ScoreManager.aspx");
        }

        private DataTable GetTableFields(string[] fieldsDescription)
        {
            string fildsNames = ViewState["subjec"].ToString();
            string[] fieldsName = fildsNames.Split(',');
            DataTable dtf = new DataTable();
            dtf.Columns.Add("Name", typeof(string));
            dtf.Columns.Add("Description", typeof(string));

            for (int i = 0; i < fieldsName.Length; i++)
            {
                DataRow dr = dtf.NewRow();
                dr[0] = fieldsName[i];
                dr[1] = fieldsDescription[i];
                dtf.Rows.Add(dr);
            }
            return dtf;
        }
    }
}

 

 

posted @ 2012-08-07 23:17  zhudongwei  阅读(264)  评论(0)    收藏  举报