Click here to Skip to main content
15,883,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone! I'm creating a program in WPF based on C #, which is filling rows of Excel in the same column, but the code on which I base the truth I could not make it work, so I ask a little help to guide me a bit.

The example of filling in the data consists of row 15 to row 20 in the same column E, but then leave a blank space for the following information to be sent from C #.

Example Excel Image

private void btnAbrir_Click(object sender, RoutedEventArgs e)
        {
            xlexcel = new Excel.Application();

            xlexcel.Visible = true;

            // Open a File
            xlWorkBook = xlexcel.Workbooks.Open(@"C:\MyFile.xlsx", 0, true, 5, "", "", true,
            Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Cells[14, 5] = ".";
            xlWorkSheet.Cells[15, 5] = txtDepartamento.Text;
            xlWorkSheet.Cells[16, 5] = txtUsuario.Text;
            xlWorkSheet.Cells[17, 5] = txtEquipo.Text;
            xlWorkSheet.Cells[18, 5] = txtCpu.Text;
            xlWorkSheet.Cells[19, 5] = txtMonitor.Text;
            xlWorkSheet.Cells[20, 5] = txtTeclado.Text;
            xlWorkSheet.Cells[21, 5] = txtMouse.Text;

        }
        private void btnAñadir_Click(object sender, RoutedEventArgs e)
        {
            int _lastRow = xlWorkSheet.Range["E" + xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;

            xlWorkSheet.Cells[_lastRow, 5] = txtDepartamento.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtUsuario.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtEquipo.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtCpu.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtMonitor.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtTeclado.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtMouse.Text;

        }


I hope I can have explained what happens.
Thank you

What I have tried:

I tried with this code but it goes to the end of the format that I have preset.

int _lastRow = xlWorkSheet.Range["E" + xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;

            xlWorkSheet.Cells[_lastRow, 5] = txtDepartamento.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtUsuario.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtEquipo.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtCpu.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtMonitor.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtTeclado.Text;
            xlWorkSheet.Cells[_lastRow, 5] = txtMouse.Text;
Posted

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