using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using System.Data.SqlClient;public partial class Admin_Grade : System.Web.UI.Page
{
    SQLHelper sql = new SQLHelper();
    sqlDataUnits da = new sqlDataUnits();
    DataSet dt = new DataSet();
    private static DataSet importDs;
    Common common = new Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        sql.RunSQL("select * from Student", ref dt);
        GridView1.DataSource = dt.Tables[0];
        GridView1.DataBind();
        if (importDs != null && importDs.Tables[0].Rows.Count > 0)
        {
            GridView2.DataSource = importDs;
            GridView2.DataBind();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            common.MoustBackColor(e);
            if (Request.QueryString["id"] != null)
            {
                //单击 事件
                e.Row.Attributes.Add("OnClick", "aa('" +
       e.Row.Cells[0].Text + "&" + e.Row.Cells[1].Text + "')");
            }
            else
            {
                //双击 事件
                e.Row.Attributes.Add("OnDblClick", "DbClickEvent('" +
       e.Row.Cells[0].Text + "')");
                //设置悬浮鼠标指针形状为"小手"
                e.Row.Attributes["style"] = "Cursor:hand";
            }        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("style", "vnd.ms-Excel.numberformat:@;");
        }
    }
    private static string modifyFileName;
    protected void btn_file_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = false;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=pkmv_de.xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }    protected void btnDel_Click(object sender, EventArgs e)
    {
        LinkButton lb = sender as LinkButton;
        GridViewRow row = lb.Parent.Parent as GridViewRow;
        string id = row.Cells[0].Text.ToString();
        if (id != "")
        {
            string sqlstr = "DELETE Student WHERE ID=" + id;
            sql.RunSQL(sqlstr);
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    { }    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
        try
        {
            fileName = uploadXls();
            if (fileName != string.Empty)
            {
                importXlsToData(fileName);
            }
        }
        catch (Exception ex)
        {        }
    }
   
    private void importXlsToData(string fileName)
    {
        importDs = sql.getOledbDataSet(fileName);
        GridView2.DataSource = importDs;
        GridView2.DataBind();
    }
    /// <summary>
    /// 上传文件到服务器
    /// </summary>
    /// <returns></returns>
    /// 
    private string uploadXls()
    {
        HttpPostedFile uploadFile = fileUpload.PostedFile;
        string oriFileName = string.Empty;
        string uploadFilePath = string.Empty;
        modifyFileName = string.Empty;
        string fileExtend = "";
        int fileSize = 0;
        try
        {
            if (fileUpload.HasFile)
            {
                fileSize = uploadFile.ContentLength;
                if (fileSize == 0)
                {
                    //showAlert(this, "导入的文件大小为0,请重新导入!");
                    return oriFileName;
                }
                oriFileName = uploadFile.FileName;
                fileExtend = Path.GetExtension(oriFileName);
                fileExtend = fileExtend.ToLower();
                if (fileExtend != ".xls")
                {
                    //showAlert(this, "您选择的文件格式不正确,请导入Excel格式的文件!");
                    return oriFileName;
                }
                uploadFilePath = Server.MapPath(ConfigurationManager.AppSettings["receiveDocument"]);
                modifyFileName = System.Guid.NewGuid().ToString();
                modifyFileName += fileExtend;//oriFileName;
                //判断路劲是否存在
                DirectoryInfo dir = new DirectoryInfo(uploadFilePath);
                if (!dir.Exists)
                {
                    dir.Create();
                }
                //判断文件是否存在
                oriFileName = uploadFilePath + "\\" + modifyFileName;
                if (File.Exists(oriFileName))
                {
                    File.Delete(oriFileName);
                }
                uploadFile.SaveAs(oriFileName);
            }
            else
            {
                //showAlert(this, "请选择要上传的文件!");
                return oriFileName;
            }
        }
        catch (Exception e)
        {        }
        return oriFileName;
    }
    private void addGrade()
    {
        if (GridView2.Rows.Count > 0)
        {
            foreach (GridViewRow row in GridView2.Rows)
            {
                SqlParameter[] prams ={
                sql.CreateInParam("@sn",SqlDbType.NVarChar,50,row.Cells[0].Text.ToString()),
                sql.CreateInParam("@sname",SqlDbType.NVarChar,50,row.Cells[1].Text.ToString()),
                sql.CreateInParam("@pwd",SqlDbType.NVarChar,50,row.Cells[2].Text.ToString()),
                sql.CreateInParam("@major",SqlDbType.NVarChar,50,row.Cells[3].Text.ToString())
                //sql.CreateInParam("@dept",SqlDbType.NVarChar,50,row.Cells[4].Text.ToString()),
         };
                //da.runSql("Score_Insert " + row.Cells[0].Text.ToString() + "," + row.Cells[1].Text.ToString() + "," + row.Cells[2].Text.ToString() + ","
                //    + row.Cells[3].Text.ToString() + "," + row.Cells[4].Text.ToString() + "," + row.Cells[5].Text.ToString() + ","
                //    + row.Cells[6].Text.ToString() + "," + row.Cells[7].Text.ToString() + "," + row.Cells[8].Text.ToString() + ","
                //    + row.Cells[9].Text.ToString() + "," + row.Cells[10].Text.ToString() + "," + row.Cells[11].Text.ToString() + ","
                //    + row.Cells[12].Text.ToString() + "," + row.Cells[13].Text.ToString() + "," + row.Cells[14].Text.ToString() + ","
                //    + row.Cells[15].Text.ToString() + "," + row.Cells[16].Text.ToString() + "," + row.Cells[17].Text.ToString() + ","
                //    + row.Cells[18].Text.ToString() + "," + row.Cells[19].Text.ToString() + "," + row.Cells[20].Text.ToString() + ","
                //    + row.Cells[21].Text.ToString() + "," + row.Cells[22].Text.ToString() + "," + row.Cells[23].Text.ToString() + ","
                //    + row.Cells[24].Text.ToString() + "," + row.Cells[25].Text.ToString() + "," + row.Cells[26].Text.ToString() + ","
                //    + row.Cells[27].Text.ToString() + "," + row.Cells[28].Text.ToString() + "," + row.Cells[29].Text.ToString() + ","
                //    + row.Cells[30].Text.ToString() + "," + row.Cells[31].Text.ToString() + "," + row.Cells[32].Text.ToString() + ","
                //    + row.Cells[33].Text.ToString() + "," + row.Cells[34].Text.ToString() + "," + row.Cells[35].Text.ToString() + ","
                //    + row.Cells[36].Text.ToString() + "," + row.Cells[37].Text.ToString() + "," + row.Cells[38].Text.ToString() + ","
                //    + row.Cells[39].Text.ToString() + "," + row.Cells[40].Text.ToString() + "," + row.Cells[41].Text.ToString() + ","
                //    + row.Cells[42].Text.ToString() + "," + row.Cells[43].Text.ToString() + "," + row.Cells[44].Text.ToString() + ","
                //    + row.Cells[45].Text.ToString() + "," + row.Cells[46].Text.ToString() + "," + row.Cells[47].Text.ToString() + ","
                //    + row.Cells[48].Text.ToString() + "," + row.Cells[49].Text.ToString() + "," + row.Cells[50].Text.ToString() + ","
                //    + row.Cells[51].Text.ToString() + "," + row.Cells[52].Text.ToString() + "," + row.Cells[53].Text.ToString() + ","
                //    + row.Cells[54].Text.ToString() + "," + row.Cells[55].Text.ToString() + "," + row.Cells[56].Text.ToString() + ","
                //    + row.Cells[57].Text.ToString() + "," + row.Cells[58].Text.ToString() + "," + row.Cells[59].Text.ToString());
               sql.RunProc("StudentInsert", prams);
            }
        }
        else
        {
            //showAlert(this, "保存数据为空!");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        addGrade();
    }
    private void delFile(string fileName)
    {
        if (fileName != "")
        {
            string serverPath = Server.MapPath(ConfigurationManager.AppSettings["receiveDocument"]);
            serverPath = serverPath + "\\" + fileName;
            if (File.Exists(serverPath))
            {
                try
                {
                    File.Delete(serverPath);
                }
                catch (Exception ex)
                {                }
            }
        }
    }
}