使用C#在Datagridview中删除基于CheckBox的多个Excel数据

时间:2016-12-19 08:45:26

标签: c# excel datagridview

我在删除多个Excel数据时发现了一些问题。我有excel数据,它们也显示在datagridview中。

WSID   Lokasi   Limit
123    A        100
456    B        200
789    C        300
321    D        400
654    E        500
876    F        600

我想根据我在datagridview中检查的复选框删除数据。我发现了一些错误。谁能帮我 ?该错误表示' System .__ ComObject'不包含'包含'

的定义

或者根据datagridview中的复选框以任何其他方式删除excel中的多个数据?

 string lokasifile = @"D:\\Data Pengisian SLA Surabaya\\" + new System.Globalization.CultureInfo("id-ID").DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek) + "_" + System.DateTime.Now.Date.ToString("dd MMM yyyy", new System.Globalization.CultureInfo("id-ID")) + ".xlsx";


private void DeleteSelectedDataBtn_Click(object sender, EventArgs e)
    {
       foreach(DataGridViewRow item in JadwalisiGV.Rows )
       {
           if (Convert.ToBoolean(item.Cells[0].Value) == true) //Convert.ToBoolean(item.Cells[0].Value) == true
            {
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;

                string str;


                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Open(lokasifile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = xlWorkBook.Sheets["Schedule"];

                for(int i = 1; i <=100; i++)
                {
                    if (!xlWorkSheet.Cells[i, 1].Contains(item.Cells[2].Value.ToString())) //It says 'System.__ComObject' does not contain a definition for 'Contains'
                  {
                      ((Excel.Range)xlWorkSheet.Rows[i]).Delete(xlWorkSheet);
                      MessageBox.Show("Data berhasil dihapus !");
                  }
                }


            }
       }
    }

2 个答案:

答案 0 :(得分:0)

.Cells会返回一个范围。 Range对象没有包含的方法, 通过使用范围上的value属性获取值,然后使用.net字符串函数包含。

答案 1 :(得分:0)

您好我发给您一个c#函数,它从Excel文件导入数据并将它们保存在数据库中。我希望这会对你有所帮助。

 [HttpPost]
    public ActionResult Import(HttpPostedFileBase excelFile)
    {


        if (!User.Identity.IsAuthenticated)
            return RedirectToAction("Login", "Account");
      Domain.User meu = serv.GetUserByLogin(User.Identity.Name);
        ViewBag.roleid = meu.RoleId;

        NormesSkills ns = serv.GetNormes();
        if (meu.RoleId == 1)
        {
            ViewBag.startBegin = ns.beginnerStart;
            ViewBag.endbegin = ns.beginnerEnd;
            ViewBag.startinter = ns.IntermedStart;
            ViewBag.endinter = ns.IntermedEnd;
            ViewBag.startadv = ns.AdvStart;
            ViewBag.endadv = ns.AdvEnd;
        }

        List<Domaine> domaines = new List<Domaine>();
        List<User> users = new List<Domain.User>();
        if (meu.RoleId == 1)
        {
            domaines = serv.getAlldomains();
            users = serv.GetAllUsers().ToList();
            users.Remove(meu);
        }
        else
        {
            domaines = meu.team.Domaines.ToList();

            if (meu.RoleId == 2)
            {
                Team t = serv.GetTeamByUserName(meu.UserName);
                domaines.AddRange(t.Domaines.ToList());
                users = t.Users.ToList();
                users.Add(meu);
            }

            if (meu.RoleId == 3)
                users.Add(meu);
        }

        List<string> d = domaines.Select(x => x.DomainName).Distinct().ToList();





        int l = users.Count + 1;
        int c = d.Count + 1;
        string[,] matrix = new string[l, c];
        string[,] matrixUpdate = new string[l, c];

        int k = 1;
        foreach (var dd in d)
        {
            matrix[0, k] = dd;
            k++;
        }

        int j = 0;
        for (int i = 1; i < l; i++)
        {
            User u = users[i - 1];
            serv.setSkills(u);
            //foreach(var u in users)
            //{
            matrix[i, 0] = u.FirstName + " " + u.LastName;
            for (int y = 1; y < c; y++)
            {
                bool verifSkillUsr = serv.VerifSkillUsr(d[y - 1], u);
                if (verifSkillUsr == true)
                {
                    Skill sk = serv.getSkillUser(d[y - 1], u);
                    if (sk.Evaluation == "0000") sk.Evaluation = "";
                    if (sk.Evaluation == "Beg") sk.Evaluation = "B";
                    if (sk.Evaluation == "Int") sk.Evaluation = "I";
                    if (sk.Evaluation == "Adv") sk.Evaluation = "A";

                    matrix[i, y] = sk.Evaluation;
                    matrixUpdate[i, y] = sk.update.ToString();

                }
                else
                {
                    matrix[i, y] = "";

                }
            }
            //}


        }
        TempData["skills"] = matrix;
        TempData["skillsUpdate"] = matrixUpdate;



        if ((excelFile == null) || (excelFile.ContentLength == 0))
        {
            ViewBag.error = "No  file";
            return View("Index");
        }
        else
        {
            if ((excelFile.FileName.EndsWith("xls")) || (excelFile.FileName.EndsWith("xlsx")))
            {
                string path="";
                DateTime date = DateTime.Now;

                if  (excelFile.FileName.EndsWith("xls")) 
                path = Server.MapPath("~/Content/Excel/"+date.ToString().Replace("/","-").Replace(":","-")+".xls");

                else
                    path = Server.MapPath("~/Content/Excel/" + date.ToString().Replace("/", "-").Replace(":", "-") + ".xlsx");

                string[] filePaths = Directory.GetFiles(Server.MapPath("~/Content/Excel/"));
                foreach (string filePath in filePaths)
                {
                    try
                    {
                         System.IO.File.Delete(filePath);
                    }
                    catch { }

                }

                //if (System.IO.File.Exists(path))
                //    System.IO.File.Delete(path);
                excelFile.SaveAs(path);

                // Reading data from excel

                Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(path);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;
                Microsoft.Office.Interop.Excel.Range range = worksheet.UsedRange;

                if (range.Rows.Count < 2)
                {
                    ViewBag.errorin = "File does not contain users";
                    return View("Index");
                }


                if (range.Columns.Count < 2)
                {
                    ViewBag.errorin = "File does not contain domains";
                    return View("Index");
                }
                int cols=range.Columns.Count;
                int lines=range.Rows.Count;


                List<string> domains=new List<string>();;

                for(int i=2;i <=cols;i++)
                {
                    string dom = ((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString() ;
                    if ((dom != "") && (dom != null))
                    {
                        dom = dom.ToLower();
                        domains.Add(dom);
                    }
                }
                domains = domains.Distinct().ToList();
                foreach(string dd in domains)
                {
                   string  dds = dd.Trim();

                    bool h = serv.verifExistenceDomaine(dds);

                    if(h==false)
                    {
                        if(meu.RoleId==2)
                        {
                            serv.AddDomaine(dds, 0, serv.GetTeamByUserName(meu.UserName).Team_Id);
                        }
                        else
                        {
                        foreach (var t in serv.GetAllTeams())
                        { serv.AddDomaine(dds, 0, t.Team_Id); }
                        }
                    }

                }





                for (int row = 2; row <= range.Rows.Count;row++ )
                {
                    List<string> verifinsertion = new List<string>();
                    //    string dom = ((Microsoft.Office.Interop.Excel.Range)range.Cells[0, i]).Text;
                    string username = ((Microsoft.Office.Interop.Excel.Range)range.Cells[row, 1]).Text.ToString();

                    User u = serv.getUserByFLName(username);
                    if(u!=null)
                    {

                        Team t = serv.GetTeamByUserName(meu.UserName);

                        for(int i=2;i<=cols;i++)
                        {
                            bool trouve = false;
                            if (meu.RoleId == 1)
                            {
                                foreach(var domaineeee in verifinsertion)
                                {
                                    if (domaineeee == ((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString())
                                    {
                                        trouve = true;
                                    }
                                }
                                if (trouve == false)
                                {
                                    serv.updateOrInsertSkill(u, ((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString(), ((Microsoft.Office.Interop.Excel.Range)range.Cells[row, i]).Text.ToString());
                                    verifinsertion.Add(((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString());
                                }

                            }
                            else
                            {
                                 foreach(var domaineeee in verifinsertion)
                                {
                                    if (domaineeee == ((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString())
                                    {
                                        trouve = true;
                                    }
                                }
                                if (trouve == false)
                                {
                                serv.updateOrInsertSkillTeam(t,u, ((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString(), ((Microsoft.Office.Interop.Excel.Range)range.Cells[row, i]).Text.ToString());
                                verifinsertion.Add(((Microsoft.Office.Interop.Excel.Range)range.Cells[1, i]).Text.ToString());
                                }
                            }
                        }
                    }
                }




                return RedirectToAction("Index", "Skills");
            }
            else
            {
                ViewBag.error = "File type error";
                return View("Index");

            }
        }




    }