Create an array with the data of an xls

0

I get values from an xls that I want to be stored in a class that has an array to later list Can someone tell me how to do it?

Can you help?

    
asked by anonymous 18.04.2018 / 16:14

1 answer

1

Good afternoon, first you have to reference the Excel Library in your project, after that you can get the data that is in the xls file. Below is a link that describes how to do this with vb .net. I thought it was better to pass the link than to put the code here.

Manipulating Excel files without use of OLEDB interoperability or connection

if (fupArquivo.HasFile)
{
  // Recebe o arquivo em array de bytes
  byte[] buffer = fupArquivo.FileBytes;
  // Criar o arquivo em memoria
  System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer);
  // Carrega o WorkBook do Excel
  ExcelLibrary.SpreadSheet.Workbook workbook = ExcelLibrary.SpreadSheet.Workbook.Load(stream);
  // Recupera o primeiro WorkSheet
  ExcelLibrary.SpreadSheet.Worksheet worksheet = workbook.Worksheets[0];

  // Cria uma tabela para armazenar o Excel
  System.Data.DataTable dtExcel = new System.Data.DataTable();
  dtExcel.Columns.Add("Coluna0", typeof(string));
  dtExcel.Columns.Add("Coluna1", typeof(string));

  // Percorre as linhas do Excel
  for (int rowIndex = worksheet.Cells.FirstRowIndex; rowIndex <= worksheet.Cells.LastRowIndex; rowIndex++)
  {
       // Recupera a linha do Excel
       ExcelLibrary.SpreadSheet.Row row = worksheet.Cells.GetRow(rowIndex);

       // Adiciona os dados na tabela
       System.Data.DataRow newRow = dtExcel.NewRow();
       newRow["Coluna0"] = row.GetCell(0).StringValue;
       newRow["Coluna1"] = row.GetCell(1).StringValue;
       dtExcel.Rows.Add(newRow);
  }
}
}

Reference: link

    
19.04.2018 / 18:42