Transform a byte array into Dataset

Asked

Viewed 73 times

1

I am reading an excel file (xls, xlsx, csv) and displaying its contents on a new page using Dataset. I can do for xls and xlsx, but for csv it’s not rolling.

public static DataSet result;

public static void prepareFile(HttpPostedFileBase file) {
        Stream stream = file.InputStream;
        IExcelDataReader reader = null;
        byte[] fileDataCsv;

        if (file.FileName.EndsWith(".csv")) {
            BinaryReader binaryReader = new BinaryReader(file.InputStream, Encoding.UTF8);
            fileDataCsv = binaryReader.ReadBytes(file.ContentLength);
            showCsv(fileDataCsv);
        } else if (file.FileName.EndsWith(".xls")) {
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
            showXls(reader);
        } else if (file.FileName.EndsWith(".xlsx")) {
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            showXls(reader);
        } else {
            //TODO: Message error
            return;
        }    
    }

I saw in some forums some people getting pretty close to what I wanted using byte[], but I can’t figure out what I need to do to turn these byte[] in DataSet.

Any idea?

PS: I know I might be doing wrong, so I’m open to displaying this file . csv in any other way.

1 answer

1


I would do otherwise:

 //Declaro o StreamReader para o caminho onde se encontra o arquivo 
        StreamReader rd = new StreamReader(@"e:\file.csv"); 
        //Declaro uma string que será utilizada para receber a linha completa do arquivo 
        string linha = null; 
        //Declaro um array do tipo string que será utilizado para adicionar o conteudo da linha separado 
        string[] linhaseparada = null; 
        //realizo o while para ler o conteudo da linha 
        while ((linha = rd.ReadLine()) != null) 
        { 
            //com o split adiciono a string 'quebrada' dentro do array 
            linhaseparada = linha.Split(';'); 
            //aqui incluo o método necessário para continuar o trabalho 

        } 
        rd.Close(); 
  • 1

    Thanks! I made some modifications to my code and managed to adapt :)

Browser other questions tagged

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