如果使用EPPlus

时间:2017-01-01 08:33:02

标签: c# excel datagridview

我这里有问题。如果数据存在于datagridview中,我想在我的excel文件中获取行索引。

这里我有datagridview中的数据:

WSID Lokasi
1234  A
4321  C
8765  D
5678  B

在我的Excel文件中,我有数据:

WSID Lokasi
1234  A
5678  B
4321  C
8765  D

这里我想基于我的数据得到行索引显示在datagridview中。例如,在datagridview中有4321,我想获取我的Excel中的数据的行索引是第3行。我该怎么做?

private void PrintScheduleBtn_Click(object sender, EventArgs e)
        {
            int hitung = 0;

            DialogResult dialogResult = MessageBox.Show("Apakah Anda yakin ingin menyimpan jadwal pengisian ?", "", MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)
            {
                var package = new ExcelPackage(new FileInfo(file));
                var packagedb = new ExcelPackage(new FileInfo(filedb));

                ExcelWorksheet schedule = package.Workbook.Worksheets["Schedule"];
                ExcelWorksheet cart = package.Workbook.Worksheets["Cartridge"];
                ExcelWorksheet unsche = package.Workbook.Worksheets["Unschedule"];
                ExcelWorksheet rekap = package.Workbook.Worksheets["Rekap"];

                ExcelWorksheet data = packagedb.Workbook.Worksheets["Data"];

                int startrow = data.Dimension.Start.Row;
                int endrow = data.Dimension.End.Row;

                int tima = 0;
                int timb = 0;
                int timc = 0;
                int timd = 0;
                int time = 0;

                int limit1;

                foreach (DataGridViewRow row in JadwalisiGV.Rows)
                {
                    hitung++;
                    #region Rekap
                    if (!row.IsNewRow)
                    {
                        schedule.Cells[hitung + 1, 1].Value = row.Cells[1].Value;
                        schedule.Cells[hitung + 1, 2].Value = row.Cells[2].Value;
                        schedule.Cells[hitung + 1, 3].Value = row.Cells[3].Value;
                        schedule.Cells[hitung + 1, 4].Value = row.Cells[4].Value;
                        schedule.Cells[hitung + 1, 5].Value = "Pengisian ATM";
                        schedule.Cells[hitung + 1, 6].Value = row.Cells[10].Value;

                        if (row.Cells["Tim"].Value.ToString() == "A")
                        {
                            tima++;
                            rekap.Cells[tima + 7, 3].Value = row.Cells["WSID"].Value;
                            bool limit = int.TryParse(row.Cells["Limit"].Value.ToString(), out limit1);
                            if (limit)
                                rekap.Cells[tima + 7, 4].Value = limit1;

                            IsiTimABox.Text = tima.ToString();
                        }
                        if (row.Cells["Tim"].Value.ToString() == "B")
                        {
                            timb++;
                            rekap.Cells[timb + 7, 9].Value = row.Cells["WSID"].Value;

                            bool limit = int.TryParse(row.Cells["Limit"].Value.ToString(), out limit1);
                            if (limit)
                                rekap.Cells[timb + 7, 10].Value = limit1;

                            IsiTimBBox.Text = timb.ToString();
                        }
                        if (row.Cells["Tim"].Value.ToString() == "C")
                        {
                            timc++;
                            rekap.Cells[timc + 28, 3].Value = row.Cells["WSID"].Value;

                            bool limit = int.TryParse(row.Cells["Limit"].Value.ToString(), out limit1);
                            if (limit)
                                rekap.Cells[timc + 28, 4].Value = limit1;

                            IsiTimCBox.Text = timc.ToString();
                        }
                        if (row.Cells["Tim"].Value.ToString() == "D")
                        {
                            timd++;
                            rekap.Cells[timd + 28, 9].Value = row.Cells["WSID"].Value;

                            bool limit = int.TryParse(row.Cells["Limit"].Value.ToString(), out limit1);
                            if (limit)
                                rekap.Cells[timd + 28, 10].Value = limit1;
                            IsiTimDBox.Text = timd.ToString();
                        }
                        if (row.Cells["Tim"].Value.ToString() == "E")
                        {
                            time++;
                            rekap.Cells[time + 7, 15].Value = row.Cells["WSID"].Value;

                            bool limit = int.TryParse(row.Cells["Limit"].Value.ToString(), out limit1);
                            if (limit)
                                rekap.Cells[time + 7, 16].Value = limit1;
                            IsiTimEBox.Text = time.ToString();
                        }

                        TotalMesinBox.Text = (tima + timb + timc + timd + time).ToString();

                    }

                    #endregion

                    for (int i = 1; i <= endrow; i++)
                    {
                        if (data.Cells[startrow, 2, endrow, 2].Value != null && data.Cells[startrow, 2, endrow, 2].Value.ToString().Contains(row.Cells[1].Value.ToString()))
                        {
                            //I do not know how to get the row index. I want to code here

                        }
                    }


                }



                package.Save();
                packagedb.Save();

                IsiKaset();

                MessageBox.Show("Data berhasil disimpan !");
                System.Diagnostics.Process.Start(file);
                this.Close();
            }

            else
            { 
                //do nothing
            }

        }

0 个答案:

没有答案