如何在底行中逐行添加数据?

时间:2019-06-01 14:55:00

标签: c# excel selenium selenium-webdriver epplus

下面的代码部分在我只希望输入数据的行上写。第二个bi数据不在其下面,它写在顶部。我想使用硒从Firefox打印数据。 我必须将传入的数据打印到excel文件中。我该怎么办?

此(gmailtxtyaz)的传入数据我希望将其更新为最底行

sample" 
excelWorksheet.Cells[2, 1].Value = (gmailtxtyaz);
excelWorksheet.Cells[3, 1].Value = (gmailtxtyaz);
excelWorksheet.Cells[4, 1].Value = (gmailtxtyaz);
excelWorksheet.Cells[5, 1].Value = (gmailtxtyaz);

for loop
 FileInfo file = new FileInfo(@"C:\Users\dd\Desktop\dene.xlsx");
               using (ExcelPackage excelPackage = new ExcelPackage(file))
                {


                    DateTime zaman = DateTime.Now;

                    //   var ws = p.Workbook.Worksheets.Add("s");
                    ExcelWorkbook excelWorkBook = excelPackage.Workbook;
                    ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();

                  /*  for (int i = 2; i <= 15; i++)
                    {
                        excelWorksheet.Cells[i, 3].Value = zaman.ToString();

                    }  */

                    excelWorksheet.Cells[1, 1].Value = "Gmail";
                    excelWorksheet.Cells[2, 1].Value = (gmailtxtyaz);
                    excelWorksheet.Cells[1, 1].Style.Font.Bold = true;
                    excelWorksheet.Cells[1, 2].Value = "Şifre";
                    excelWorksheet.Cells[2, 2].Value = (uret);
                    excelWorksheet.Cells[1, 2].Style.Font.Bold = true;
                    excelWorksheet.Cells[1, 3].Value = "Tarih";
                    excelWorksheet.Cells[1, 3].Style.Font.Bold = true;
                    excelWorksheet.Cells[1, 4].Value = "IP";
                    excelWorksheet.Cells[1, 4].Style.Font.Bold = true;

                    excelWorksheet.Cells.AutoFitColumns();


                    excelPackage.Save();


1 个答案:

答案 0 :(得分:1)

您可以在sheet.Cells对象之外制作字典:

                var _cells = worksheet.Cells;
                var dictionary = _cells
                     .GroupBy(c => new { c.Start.Row, c.Start.Column })
                       .ToDictionary(
                            rcg => new KeyValuePair<int, int>(rcg.Key.Row, rcg.Key.Column),
                            rcg => _cells[rcg.Key.Row, rcg.Key.Column].Value).Where(a => a.Value != null);

获取其中包含值的最后一行。

                var LastRowWithValues = dictionary.Select(cell => int.Parse(cell.Key.Key.ToString())).Max();

,然后在循环中递增LastRowWithValues以写入下一个空行。

                for (int i = 0; i < 4; i++)
                {
                    LastRowWithValues++;
                    worksheet.Cells[LastRowWithValues, 1].Value = "Gmail";
                    worksheet.Cells[LastRowWithValues, 1].Style.Font.Bold = true;
                }

我在多工作表Excel上测试了代码。