Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some text files which looks like this. The @@@ in the file marks the start of a new column. I want to use Microsoft Excel 14.0 Object Library or any other free library to convert this file to a excel file like this.

Can anyone help?

What I have tried:

I'm totally blank on this. I was thinking about using the split method in the text file to get the contents for each row and column but have no clue how to implement it
Posted
Updated 27-Aug-18 6:23am
Comments
Nelek 27-Aug-18 8:31am    
Another possibility could be to just replace that delimiters with the ";" and save the file as *.CSV
Excel already has a lot of own functionality to deal with that.
Or am I missing something?
Member 12692000 27-Aug-18 9:03am    
How do I implement the text to column with the delimiter using c#?

You would need to read each line and split the fields between the "@@@" strings. You can then create the new Excel file by using the Microsoft.Office.Interop.Excel Namespace | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 12692000 27-Aug-18 9:34am    
Can you show some code regarding the loop which adds the values from the fields split between the "@@@" strings to the excel file? It will be really helpful.
Richard MacCutchan 27-Aug-18 12:23pm    
See below.
This shows the basics of saving information to an Excel workbook. You just need to modify it to your own specific requirements. If you do not want column headers then just omit the first loop, and leave the row number equal to 1.
C#
using Excel = Microsoft.Office.Interop.Excel;


void Save()
{
    //Create an Excel application instance
    Excel.Application excelApp = new Excel.Application();

    //Create an Excel workbook instance and open it from the predefined location
    Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(Type.Missing);
    Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add(Type.Missing);
    excelWorkSheet.Name = "mydata";

    int row = 1;
    int column = 1;
    for // a loop for all the column names
    {
        // row 1 contains column headers
        excelWorkSheet.Cells[row, column] = // text of the column header
        ++column;
    }

    int row = 2;    // leave as 1 if no column headers
    foreach (string str in // the lines of text)
    {
        string[] fields = str.Split(new char[]{ '@', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        for (column = 0; column < fields.length; ++column)
        {
            excelWorkSheet.Cells[row, column + 1] = fields[column];
        }
        row++;
    }
    excelWorkBook.SaveAs(fileName);
    excelWorkBook.Close();
    excelApp.Quit();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900