分享

导出EXCEL

 zww_blog 2012-04-05
 (1、)   //导出事件(导出时不能用ajax事件,否则导出失败)
function Initexport() {
    $("#BtnExport").click(function() {
        var url = "/OptionSetUp/SuggestionExport?kind=1";
        var selectStatus = document.getElementById("selectStatus").value;
        //    var l = document.getElementById("Location").value;
        var startTime = document.getElementById("SuggestionTimeBegin").value;
        var endTime = document.getElementById("SuggestionTimeEnd").value;
        var employeeName = document.getElementById("TextEmployeeName").value;
        var employeeNo = document.getElementById("TextEmployeeNo").value;
        var suggestionContent = document.getElementById("TextSugeestionContent").value;
        if (selectStatus != "")
            url = url + "&selectStatus=" + encodeURIComponent(selectStatus);
        //    if (l != "")
        //        url = url + "&location=" + l;
        if (startTime != "")
            url = url + "&startTime=" + encodeURIComponent(startTime);
        if (endTime != "")
            url = url + "&endTime=" + encodeURIComponent(endTime);
        if (employeeName != "")
            url = url + "&employeeName=" + encodeURIComponent(employeeName);
        if (employeeNo != "")
            url = url + "&employeeNo=" + encodeURIComponent(employeeNo);
        if (suggestionContent != "")
            url = url + "&suggestionContent=" + encodeURIComponent(suggestionContent);
        location.href = url;
    });
}   



/// <summary>
        /// 导出建议
        /// </summary>
        /// <returns></returns>
        public void SuggestionExport()
        {
            V_IRS_SuggestionResult vepr = new V_IRS_SuggestionResult();
            //EntityList vepr = new EntityList();
            int status = 0;
            string employeeNo = "";
            string employeeName = "";
            string sugContent = "";
            DateTime startTime = new DateTime();
            DateTime endTime = new DateTime();
            string kind = Request.QueryString["kind"].ToString();
            if (Request.QueryString["selectStatus"] != null)
                status = Convert.ToInt32(Request.QueryString["selectStatus"]);
            //if (Request.QueryString["location"] != null)
            //    location = Request.QueryString["location"];
            if (Request.QueryString["suggestionContent"] != null)
                sugContent = Request.QueryString["suggestionContent"];
            if (Request.QueryString["employeeName"] != null)
                employeeName = Request.QueryString["employeeName"];
            if (Request.QueryString["employeeName"] != null)
                employeeNo = Request.QueryString["employeeNo"];
            if (Request.QueryString["employeeNo"] != null)
                startTime = Convert.ToDateTime(Request.QueryString["startTime"]);
            if (Request.QueryString["endTime"] != null)
                endTime = Convert.ToDateTime(Request.QueryString["endTime"]).AddDays(1);
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            //判断当前人是否是管理员
            List<V_IRS_Suggestion> list = new List<V_IRS_Suggestion>();
            try
            {
                string auth = this.prizeLogic.IsExistAuth(CurrentUserInfo.UserID);
                if (Session["IRSUserInfo"] != null && userLogic.isReceiveAuth(CurrentUserInfo.UserID))
                {
                    list = this.sugLogic.GetSuggestionWhere(employeeNo, employeeName, sugContent, startTime, endTime, status);
                }
                else
                {
                    Response.Redirect("/Home/Home");
                }

                List<V_IRS_Suggestion> listReturn = new List<V_IRS_Suggestion>();
                for (int i = 0; i < list.Count; i++)
                {
                    listReturn.Add(list[i]);
                }
                vepr.totalCount = list.Count;
                vepr.suggestion = listReturn;
                vepr.success = true;
            }
            catch (Exception e)
            { }
            if (list.Count > 0)
            {
                try
                {
                    string filePath = Server.MapPath("../Temp/Report.xls");
                    FileInfo downloadFile = new FileInfo(filePath);
                    if (downloadFile.Exists)
                    {
                        System.IO.File.Delete(filePath);
                    }
                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet sheet = workbook.CreateSheet("UserSuggestionReport");
                    HSSFCellStyle cellStyle = workbook.CreateCellStyle();
                    // HSSFDataFormat format = workbook.CreateDataFormat();
                    cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");//设置日期格式 @代表文本类型
                    //填充表头  
                    HSSFRow dataRow = sheet.CreateRow(0);
                    dataRow.CreateCell(0).SetCellValue("员工工号");
                    dataRow.CreateCell(1).SetCellValue("员工姓名");
                    dataRow.CreateCell(2).SetCellValue("工作地点");
                    dataRow.CreateCell(3).SetCellValue("员工BU");
                    dataRow.CreateCell(4).SetCellValue("建议日期");
                    dataRow.CreateCell(5).SetCellValue("建议内容");
                    dataRow.CreateCell(6).SetCellValue("建议分类");
                    //填充内容 
                    V_IRS_Suggestion item = null;
                    string jobName = "";
                    for (int i = 0; i < list.Count; i++)
                    {
                        item = list[i];
                        dataRow = sheet.CreateRow(i + 1);
                        dataRow.CreateCell(0).SetCellValue(item.employee_no == null ? "" : item.employee_no.ToString());//推荐人工号
                        dataRow.CreateCell(1).SetCellValue(item.user_name == null ? "" : item.user_name.ToString());//推荐人姓名
                        dataRow.CreateCell(2).SetCellValue(item.location_name == null ? "" : item.location_name.ToString());//工作地点
                        dataRow.CreateCell(3).SetCellValue(item.BU == null ? "" : item.BU.ToString());//推荐人BU
                        //dataRow.CreateCell(4).SetCellValue(item.suggestion_time == null ? "" : item.suggestion_time.ToString());//建议日期
                        dataRow.CreateCell(5).SetCellValue(item.suggestion == null ? "" : item.suggestion.ToString());//建议内容BU
                        dataRow.CreateCell(6).SetCellValue(item.suggestion_type == null ? "" : item.suggestion_type.ToString());//建议分类
                        if (item.suggestion_time == null)
                        {
                            dataRow.CreateCell(4).SetCellValue("");//建议日期
                        }
                        else
                        {
                            dataRow.CreateCell(4).SetCellValue(((DateTime)item.suggestion_time).ToString("yyyy-MM-dd"));//建议日期
                        }

                        ////候选人状态
                        //if (Convert.ToInt32(item.suggestion_type) == 1 || Convert.ToInt32(item.suggestion_type) == 11 || Convert.ToInt32(item.suggestion_type) == 15)
                        //{
                        //    dataRow.CreateCell(6).SetCellValue("未答复");
                        //}
                        //else if (Convert.ToInt32(item.suggestion_type) == 2)
                        //{
                        //    dataRow.CreateCell(6).SetCellValue("已答复"); ;
                        //}
                        dataRow.GetCell(4).CellStyle = cellStyle;
                    }
                    //保存  
                    using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                    {
                        workbook.Write(fs);
                    }
                    workbook.Dispose();

                    Thread.Sleep(3000);
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = "application/ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=ReportList.xls");
                    Response.BinaryWrite(System.IO.File.ReadAllBytes(filePath));
                    System.IO.File.Delete(filePath);
                }
                catch (Exception ex)
                {
                    logLogic.AddLog(ex, "导出时异常", "Method:ReportExport");
                    Response.Redirect("/Help/ErrorPage");
                }

            }
            else
            {
                Response.Redirect("IRSReport");
            }

        }

(2、)


//导出事件(导出时不能用ajax事件,否则导出失败)
function Initexport() {
    $("#BtnExport").click(function() {
        var url = "/OptionSetUp/GradeExport?";
        var startTime = document.getElementById("GradeTimeBegin").value;
        var endTime = document.getElementById("GradeTimeEnd").value;
        if (startTime != "")
            url = url + "startTime=" + startTime;
        if (endTime != "")
            url = url + "&endTime=" + endTime;
        location.href = url;
    });
}

 /// <summary>
        /// 员工评分导出
        /// </summary>
        /// <returns></returns>
        public void GradeExport()
        {
            List<Avg_GradeAnswerScore> list = new List<Avg_GradeAnswerScore>();
             
            DateTime startTime = DateTime.Now.AddDays(-1000);
            DateTime endTime = DateTime.Now;
            if (Request.QueryString["startTime"] != null)
            {
                startTime = Convert.ToDateTime(Request.QueryString["startTime"]);
            }
            if (Request.QueryString["endTime"] != null)
            {
                endTime = Convert.ToDateTime(Request.QueryString["endTime"]);
            }

            list = gradeLogic.EmpGradeAnswerScoreByDate(startTime.ToString(), endTime.ToString());
            if (list.Count > 0)
            {
                try
                {
                    string filePath = Server.MapPath("../Temp/GradeReport.xls");
                    //FileInfo downloadFile = new FileInfo(filePath);
                    //if (downloadFile.Exists)
                    //{
                    //    System.IO.File.Delete(filePath);
                    //}
                    string sql = "";
                    sql = @"INSERT INTO [Sheet1$]([题目号], [平均分]) 
                                       VALUES(@question_id, @avg_score)";
                    System.IO.File.Copy(Server.MapPath("~/Template/GradeDemo.xls"), filePath);
                    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;"))
                    {
                        conn.Open();
                        foreach (var item in list)
                        {
                            OleDbCommand cmd = new OleDbCommand(sql, conn);
                            cmd.Parameters.AddWithValue("@question_id", item.Question_id == null ? 0 : item.Question_id);
                            cmd.Parameters.AddWithValue("@avg_score", item.avg_score == null ? 0 : item.avg_score);
                            
                            cmd.ExecuteNonQuery();
                        }
                        conn.Close();
                        conn.Dispose();
                        GC.Collect();
                    }
                    Thread.Sleep(3000);
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = "application/ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=GradeList.xls");
                    Response.BinaryWrite(System.IO.File.ReadAllBytes(filePath));

                    System.IO.File.Delete(filePath);
                }
                catch (Exception ex)
                {
                    logLogic.AddLog(ex, "导出员工评分时异常", "Method:GradeExport");
                    Response.Redirect("/Help/ErrorPage");
                }
            }
            else
            {
                Response.Redirect("EmployeeGrade");
            }
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多