Create an array with the data of an xls

Asked

Viewed 46 times

0

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

Can help?

  • Please try to be clearer. You can [Edit] and add more details to your question at any time.

1 answer

1


Good afternoon, first you will have to refer to 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 best to pass the link to put the code here.

Manipulating Excel files without using interoperability or OLEDB 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: http://raphaelcardoso.com.br/criar-ler-e-modificar-arquivos-excel-sem-uso-da-interoperabilidade-ou-conexao-oledb/

  • Actually it would be better to put an example code here.

Browser other questions tagged

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