如何使用C#查找下一个空白行并将表单数据导出到Excel

时间:2018-09-22 18:26:06

标签: c# excel forms

当前,我当前可以将每个textBox导出到第2行的单个单元格。但是,它只能写入覆盖先前数据的位置。我正在尝试弄清楚如何使它在每次提交后识别下一个可用的空白行。下面是我目前仅写入第2行的代码。这是我在C#中的第一个项目,因此我对C#的了解不多。

   Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
   Microsoft.Office.Interop.Excel.Sheets xlBigSheet;

   object misValue;
   String myPath;
   private void Button1_Click(object sender, EventArgs e)
   {

       string myPath = @"C:\\Users\\N0m4d\\Desktop\\Longhouse\\database.xlsx"; // this must be full path.
       FileInfo fi = new FileInfo(myPath);
       if (!fi.Exists)
       {
           Console.Out.WriteLine("file doesn't exists!");
       }
       else
       {
           var excelApp = new Microsoft.Office.Interop.Excel.Application();
           var workbook = excelApp.Workbooks.Open(myPath);
           Worksheet worksheet = workbook.ActiveSheet as Worksheet;


           Range range1 = worksheet.Cells[2, 1] as Range;
           range1.Value2 = textBox1.Text;
           Range range2 = worksheet.Cells[2, 2] as Range;
           range2.Value2 = textBox2.Text;
           Range range3 = worksheet.Cells[2, 3] as Range;
           range3.Value2 = textBox3.Text;
           Range range4 = worksheet.Cells[2, 4] as Range;
           range4.Value2 = textBox4.Text;
           Range range5 = worksheet.Cells[2, 5] as Range;
           range5.Value2 = textBox5.Text;
           Range range6 = worksheet.Cells[2, 6] as Range;
           range6.Value2 = textBox6.Text;

           excelApp.Visible = true;
           workbook.Save();
           //workbook.Close();

           textBox1.Text = string.Empty;
           textBox2.Text = string.Empty;
           textBox3.Text = string.Empty;
           textBox4.Text = string.Empty;
           textBox5.Text = string.Empty;
           textBox6.Text = string.Empty;

           xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBigSheet.get_Item("Sheet1");
           Microsoft.Office.Interop.Excel.Range last = xlWorkSheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
           int lastUsedRow = last.Row;
           getData(lastUsedRow + 1);
       }
   }

   private void getData(int lastRow_)
   {

       lastRow_ = xlWorkSheet.Cells.Find(
                   "*",
                   xlWorkSheet.Cells[1, 1],
                   misValue,
                   Microsoft.Office.Interop.Excel.XlLookAt.xlPart,
                   Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                   Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious,
                   misValue,
                   misValue,
                   misValue).Row + 1;
   }

1 个答案:

答案 0 :(得分:1)

这是您要实现的目标吗?

   Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
   Microsoft.Office.Interop.Excel.Sheets xlBigSheet;

   object misValue;
   String myPath;
   private void Button1_Click(object sender, EventArgs e)
   {

       string myPath = @"C:\\Users\\N0m4d\\Desktop\\Longhouse\\database.xlsx"; // this must be full path.
       FileInfo fi = new FileInfo(myPath);
       if (!fi.Exists)
       {
           Console.Out.WriteLine("file doesn't exist!");
       }
       else
       {
           var excelApp = new Microsoft.Office.Interop.Excel.Application();
           var workbook = excelApp.Workbooks.Open(myPath);
           Worksheet worksheet = workbook.ActiveSheet as Worksheet;

      Microsoft.Office.Interop.Excel.Range lastRow = worksheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
           int lastUsedRow = last.Row;
           int newRow = lastUsedRow + 1;

           Range range1 = worksheet.Cells[newRow , 1] as Range;
           range1.Value2 = textBox1.Text;
           Range range2 = worksheet.Cells[newRow , 2] as Range;
           range2.Value2 = textBox2.Text;
           Range range3 = worksheet.Cells[newRow , 3] as Range;
           range3.Value2 = textBox3.Text;
           Range range4 = worksheet.Cells[newRow , 4] as Range;
           range4.Value2 = textBox4.Text;
           Range range5 = worksheet.Cells[newRow , 5] as Range;
           range5.Value2 = textBox5.Text;
           Range range6 = worksheet.Cells[newRow , 6] as Range;
           range6.Value2 = textBox6.Text;

           excelApp.Visible = true;
           workbook.Save();
           //workbook.Close();

           textBox1.Text = string.Empty;
           textBox2.Text = string.Empty;
           textBox3.Text = string.Empty;
           textBox4.Text = string.Empty;
           textBox5.Text = string.Empty;
           textBox6.Text = string.Empty;
       }
   }