Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can currently currently export each textBox to an individual cell on row 2. However it can only write to that location which overwrites the previous data. I am trying to figure out how to make it identify the next available blank row after every submission. Below is my current code that only write to row 2.

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;
       }


What I have tried:

Tried multiple suggestions from google but I do not full understand how to modify them to fit my needs.
Posted
Updated 20-Aug-18 4:02am
v3

Appending data to existing Excel file using C# - Stack Overflow[^]

Quote:
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);
 
Share this answer
 
Comments
Member 13950471 20-Aug-18 10:03am    
Your solution has got me very close to finishing however i am still trying to iron out a few issues so I can run it. I have update my posted code to reflect the changes.
In vba, I use
VB
RowS = worksheet.UsedRange.RowS.Count
ColS = worksheet.UsedRange.Columns.Count

Interop should offer something pretty similar.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900