0

I tried searching for a solution here but I can't seem to find any answers. I have a textfile that appears like this:

Nmr_test   101E-6            PASSED       PASSED              PASSED        PASSED 
Dc_volts   10V_100  CAL_+10V +9.99999000  +10.0000100  +9.99999740   +9.99999727   
Dcv_lin    10V_6U   11.5     +0.0000E+000 +7.0000E+000 +2.0367E+001  +2.7427E+001  
Dcv_lin    10V_6U   3        +0.0000E+000 +5.0000E+000 +1.3331E+001  +1.8872E+001  

I have to convert this textfile to an Excel/xls file but I can't figure out how to insert them to the correct excel columns as they have different number of spaces in between columns. I've tried using this code below which is using space as a separator but it fails of course due to the varying number of spaces between the columns:

    var lines = File.ReadAllLines(string.Concat(Directory.GetCurrentDirectory(), "\\Temp_textfile.txt"));
    var rowcounter = 1;
    foreach(var line in lines)
     {
       var columncounter = 1;
       var values = line.Split(' ');
         foreach(var value in values)
          {
           excelworksheet.Cells[rowcounter, columncounter] = new Cell(value);
           columncounter++;
          }
         rowcounter++;
     }
    excelworkbook.Worksheets.Add(excelworksheet);
    excelworkbook.Save(string.Concat(Directory.GetCurrentDirectory(), "\\Exported_excelfile.xls"));

Any advice?

EDIT: Got it working using SubString that selects each column using their fixed width.

Crescendo26
  • 171
  • 6

0 Answers0