Example #1
0
    protected void ExportExcelClick(object sender, DirectEventArgs e)
    {
        string sourceFile  = Server.MapPath("") + "\\FileMau\\Ma cham cong mau.xls";
        string destination = Server.MapPath("") + "\\File\\Ma cham cong export.xls";

        File.Copy(sourceFile, destination, true);
        System.Data.OleDb.OleDbConnection MyConnection;
        System.Data.OleDb.OleDbCommand    myCommand = new System.Data.OleDb.OleDbCommand();
        string sql = null;

        MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + destination + "';Extended Properties=Excel 8.0;");
        MyConnection.Open();
        myCommand.Connection = MyConnection;
        int dem = 0;

        if (radExpTatCa.Checked == true)
        {
            List <HoSoInfo> listhoso = new HoSoController().GetHoSoMaChamCong(Session["MaDonVi"].ToString(), new List <string>(), false);
            foreach (var item in listhoso)
            {
                dem++;
                sql = @"Insert into [Sheet1$] values('" + item.MACB + "','" + item.HOTEN + "','" + item.PHONGBAN + "','" + item.MaChamCong + "')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
            }
        }
        if (radExpBoPhan.Checked == true)
        {
            List <HoSoInfo> listhoso = new HoSoController().GetHoSoMaChamCong(SingleGridPanel1.GetMaDonVi(), new List <string>(), false);
            foreach (var item in listhoso)
            {
                dem++;
                sql = @"Insert into [Sheet1$] values('" + item.MACB + "','" + item.HOTEN + "','" + item.PHONGBAN + "','" + item.MaChamCong + "')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
            }
        }

        HoSoController hsc = new HoSoController();

        if (radExpChon.Checked == true)
        {
            foreach (var item in new HoSoController().GetHoSoMaChamCong(Session["MaDonVi"].ToString(), SingleGridPanel1.GetSelectedRecordIDs() ?? new List <string>(), true))
            {
                dem++;
                sql = @"Insert into [Sheet1$] values('" + item.MACB + "','" + item.HOTEN + "','" + item.PHONGBAN + "','" + item.MaChamCong + "')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
            }
        }
        MyConnection.Close();

        Dialog.ShowNotification("Thông báo", "Đã xuất thông tin của " + dem.ToString() + " cán bộ thành công");
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + "MaChamCongExport.xls");
        Response.WriteFile(destination);
        Response.End();
        wdExport.Hide();
    }