Epplus row and column last cell - C#

Asked

Viewed 1,167 times

4

I want to select a range from the first to the last cell filled in the row or column. In VBA the code is as below using

Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select

How could I do it the same way in C# using Epplus? I’m leaving cell B139 and I need to go to the last row and column filled

3 answers

3


Thank you for your attention. I was able to find the answer using LINQ

To line

 var lastRowCell1 = worksheet.Cells.Last(c => c.Start.Row == 1);

Para Coluna

 var lastColCell1 = worksheet.Cells.Last(c => c.Start.Column == 1);

2

I don’t know much about the Epplus package, but it seems to solve the problem using:

using (ExcelPackage xlPackage = new ExcelPackage(newFile)) 
{
   ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
}

var rowCnt = worksheet.Dimension.End.Row;
var colCnt = worksheet.Dimension.End.Column;

-1

            //using Excel = Microsoft.Office.Interop.Excel;//Inseriri esta linha no inicio das usabilidade;
        //Abre o arquivo execel
        String Destino = @"C:/ST/Informatica/Banco_de_Dados/Clientes.xlsx";
        Excel.Application excelApp_01 = new Excel.Application();
        excelApp_01.Visible = true;
        Excel.Workbook wbExcel_01 = excelApp_01.Workbooks.Open(Destino);
        Excel.Worksheet wsPlanilha_01 = (Excel.Worksheet)wbExcel_01.Worksheets.get_Item(1);
        //cria as variaveis iniciais
         int MX = 1;
         String valor ="A";
        //percorre todas as linhas até encontar uma vazia
        while (!string.IsNullOrEmpty(valor))
        { 
            valor = wsPlanilha_01.get_Range("A" + MX, "A" + MX).Value;
            MX++;
        };
        //insere o dado na ultima linha vazia
        wsPlanilha_01.Cells[MX - 1, 1] = textBox1.Text;
        wsPlanilha_01.Cells[MX - 1, 2] = textBox2.Text;
        wsPlanilha_01.Cells[MX - 1, 3] = textBox3.Text;
        wsPlanilha_01.Cells[MX - 1, 4] = textBox4.Text;

        //Fecha a plicação excel.
        excelApp_01.Visible = true;
        excelApp_01.ActiveWorkbook.Save();
        excelApp_01.Workbooks.Close();
        excelApp_01.Quit();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.