Advertisement

Help To convert Excel to CSV then to binary

Started by November 18, 2019 03:50 AM
11 comments, last by ericrrichards22 4 years, 10 months ago

hello!

i'm looking for a way to convert xls data to CSV then create a binary from it, where i'm going to read data for my game.. anyone have a good resource where i can learn something? i'm just using google but i'm still confused to how do it

You can export to CSV from any spreadsheet program or Google Docs.
The general routine to parse CSV files is as follows (pseudo-code):


string = read_file(csv)       // read file into a string
lines = string.split('\0')    // split by line endings
foreach(line in lines) {
  columns = line.split(',')   // split by comma or other separator
  foreach(column in columns) {
    // do something with the value here, likely cast it to some type
    // and/or write it out to another file for a binary format
  }
}

Depending on your language of choice this may be easy or less so, for example C++ is terrible at such tasks. Scripting languages such as Python and Ruby are your Swiss army knives here. Learn at least one.

Advertisement

thankàs you for your reply!

 

iàm looking to do it on Unity so C# i'll check out your pseudo code, thank's you!

There's a small error though, line endings are encoded \0A (decimal 10) and not \0 (decimal 0, which obviously is the null-terminator for a C-string). Sorry for the mistake.

XLS is the Microsoft Excel file format. I think C# must be the best language of XLS because these products are Microsoft products: .NET, C#, XLS. It is strange that you cannot find tutorials about how to read data from XLS. You need to find a lot of simple tutorials and practice with them. The practice is the one way that works.

Microsoft.Office.Interop will let you work with office files. You can get it from the NuGet package manager. Having a quick look, there are other packages for Excel but I have never used them.

Check out https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/how-to-access-office-onterop-objects

 

Advertisement

Sorry, I forgot to mention, you will need Office installed to use the interop as it actually loads the Excel program using API's. At work I install office on a server just to parse spread sheets like this. If this is a problem, look through the other NuGet packages that do excel, one might might work without office.

well i'm going to use NPOI library to read excel then i'll save it as CSV, my most confusing stuff is how should i convert it to binary, i never do something like this and i don't know exactly where to start...

i'm having problem to properly convert my Excel file to CSV

when i'm going to open the created CSV with excel i'm having one row all in a single cell, are not going to split for cell by comma..i'm missing something?

        public EResult ExcelToCSV(
            string in_excelPath, 
            string in_csvPath, 
            string in_sheetName
        )
        {
            IWorkbook workbook = null;
            StreamWriter sr = null;


            if(!File.Exists(in_csvPath+in_sheetName+CSV_EXT))
            {
                sr = new StreamWriter( in_csvPath+in_sheetName+CSV_EXT, false );
            }   
            else
            {
                Debug.LogError("Same File Exist");
                return EResult.ERROR;
            }


            using (FileStream file = new FileStream(in_excelPath+in_sheetName+FILE_EXTENSION,FileMode.Open,FileAccess.Read))
            {
                workbook = WorkbookFactory.Create(file);
            }


            const int sheetIndex = 0;


            ISheet sheet = workbook.GetSheetAt(sheetIndex);


            for (int row = 0; row <= sheet.LastRowNum; row++)
            {
                


                for (int cell = 0; cell < sheet.GetRow(row).Cells.Count ; cell++)
                {
                    
                    sr.Write( "\"" + sheet.GetRow(row).Cells[cell].ToString() + "\",");
                           
                }
                sr.WriteLine();
            }


            sr.Close();
            return EResult.FINISH;
        }  

This topic is closed to new replies.

Advertisement