How to convert a txt file to xls using C#?

  • Thread starter Thread starter Razvancb
  • Start date Start date
R

Razvancb

Guest
Hello everyone. Im trying to build a simple program to convert a txt file into xls using C#.

I can manage to do that with only one column in the txt file, but not with "dimensional" information.

Basically i want to convert the information under this into a xls file.

NAME AGE NATIONALITY
Razvan 22 Romanian
Pedro 21 Portuguese
Alphonse 32 Spanish


This is what i have so far, but since im stucked is not working.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using excel = Microsoft.Office.Interop.Excel;
using System.Threading;

namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
string[] inputLines = System.IO.File.ReadAllLines(@"C:\Users\rcbalaci\Desktop\Açores\RORA_FONTES.txt");

excel.Application oXL;
excel._Workbook oWB;
excel._Worksheet oSheet;
excel.Range oRng;

object misvalue = System.Reflection.Missing.Value;

try
{
//Ligar o excell e apanhar o objeto da app
oXL = new excel.Application();
oXL.Visible = true;

//Novo workbook
oWB = (excel._Workbook)(oXL.Workbooks.Add(""));
oSheet = (excel._Worksheet)oWB.ActiveSheet;

//Adicionar o cabeçalho
oSheet.Cells[1, 1] = "NAME";
oSheet.Cells[1, 2] = "AGE";
oSheet.Cells[1, 3] = "NATIONALITY";

//IM STUCK HERE
for(int i = 1; i<=inputLines.Length; i++)
{
oSheet.Cells[1][i + 1] = inputLines[i - 1];
oSheet.Cells[2][i + 1] = inputLines[];
}
}
catch (Exception e)
{
Console.WriteLine("Exception" + e);
throw;
}
}
}
}



I apreaciatte some help. Thank you.

Continue reading...
 
Back
Top