Avatar of pchen0688
pchen0688
 asked on

Covert excel file into pipe delimited format in C# or VB

Hi,
I need sample codes to pick up two excel files and convert them into pipe delimited format in a VB/C# application.

Thanks,
Perry
Software.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Alexandre Simões

8/22/2022 - Mon
Alexandre Simões

You'll have to make a OleDb connection to the Excel file:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";

and read the following:

Important note!
The quota " in the string needs to be escaped using your language specific escape syntax.
c#, c++   \"
VB6, VBScript   ""
xml (web.config etc)   "
or maybe use a single quota '.
"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.
"IMEX=1;" tells the driver to always read "intermixed" (numbers, dates, strings etc) data columns as text. Note that this option might affect excel sheet write access negative.
SQL syntax "SELECT * FROM [sheet1$]". I.e. excel worksheet name followed by a "$" and wrapped in "[" "]" brackets.
Check out the [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel] located registry REG_DWORD "TypeGuessRows". That's the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows. This might hurt performance.
If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: "Could not decrypt file."
VBRocks

Here's how you can do it.

1.  Query the Excel file, put it in a DataTable
2.  Loop through each row in the DataTable and write it out to a file.


    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click


        Dim excelDoc As String = "C:\TEMP\My Worksheet.xls"
        Dim delimitedFile As String = "C:\Temp\MyFile.txt"

        Dim delimter As String = "|"

        Dim table As DataTable = GetExcelData(excelDoc, "Sheet1", False)

        Dim writer As New IO.StreamWriter(delimitedFile, False)

        'Loop through each row and write it out
        For Each row As DataRow In table.Rows
            writer.WriteLine(Join(row.ItemArray, delimter))
        Next

        writer.Close()
        writer.Dispose()

        MsgBox("done")


    End Sub


    Public Shared Function GetExcelData( _
        ByVal path As String, ByVal tableName As String, _
        ByVal bFirstRowIsHeader As Boolean) _
        As DataTable

        Dim connectionString As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & _
                path & "';Extended Properties='Excel 8.0;HDR=" & _
                bFirstRowIsHeader & ";IMEX=1'"

        'Remove any dollar signs from the name,
        '    because they will be added below
        tableName = tableName.Replace("$", "")


        Dim table As New DataTable(IO.Path.GetFileNameWithoutExtension(path))

        Dim adapter As New OleDb.OleDbDataAdapter( _
            "SELECT * FROM [" & tableName & "$]", connectionString)

        adapter.Fill(table)
        adapter.Dispose()

        Return table

    End Function

pchen0688

ASKER
Hi,
This certainly helps, but  if I use win form and open file dialog to pick up the two excel files and convert them to pipe delimited format and ftp them to a remote server. How do the codes look like in C#?

Again, thanks for your help.
Perry
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
VBRocks


private void Button1_Click(object sender, System.EventArgs e)
{
   
    string excelDoc = "C:\\TEMP\\My Worksheet.xls";
    string delimitedFile = "C:\\Temp\\MyFile.txt";
   
    string delimter = "|";
   
    DataTable table = GetExcelData(excelDoc, "Sheet1", false);
   
    IO.StreamWriter writer = new IO.StreamWriter(delimitedFile, false);
   
    //Loop through each row and write it out
    foreach (DataRow row in table.Rows) {
        writer.WriteLine(Strings.Join(row.ItemArray, delimter));
    }
   
    writer.Close();
    writer.Dispose();
   
    MessageBox.Show("done");
   
}


public static DataTable GetExcelData(string path, string tableName, bool bFirstRowIsHeader)
{
   
    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties='Excel 8.0;HDR=" + bFirstRowIsHeader + ";IMEX=1'";
   
    //Remove any dollar signs from the name,
    // because they will be added below
    tableName = tableName.Replace("$", "");
   
   
    DataTable table = new DataTable(IO.Path.GetFileNameWithoutExtension(path));
   
    OleDb.OleDbDataAdapter adapter = new OleDb.OleDbDataAdapter("SELECT * FROM [" + tableName + "$]", connectionString);
   
    adapter.Fill(table);
    adapter.Dispose();
   
    return table;
}
pchen0688

ASKER
Hi,
I am still looking for a better solution to my project: a win application ( either in VB or C#) for end user to load excel files (output from crystal reports) and convert them to pipe delimited format and ftp them to a remote server.
 Requirement will be:
1) Load two excel files from open file dialog
2) Click button to convert them to pipe delimited text files
3) ftp these text files to a remote server.

Basically, I am a Sybase/Actuare/Crystal report developer, do not have much experience in VB/C#.

Thanks for your help!

Perry


 


Alexandre Simões

Can you tell us what's wrong with the code posted above?
In what way you want it to be better.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
pchen0688

ASKER
Hi AlexCode,
There is nothing wrong with your code. I really appreciated your help. LIke I said, I am not a VB/C# developer. I just want the sample codes that can be easily followed through to do the following things:
1) Load two excel files through open file dialogue
2) Convert them into pipe delimited files at one shot
3) Ftp them to a remote server



Alexandre Simões

What you're asking for is an application.
Here at Experts-Exchange we don't usually do applications, we point out solutions.

Applications demand a greater deal of time to assemble.
If you're whiling to wait I can write it for you some time between now and tomorrow.

Cheers,
Alex
pchen0688

ASKER
AlexCode,

That will be great!  I can wait. Thanks for your help!
Perry
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Alexandre Simões

Hi,

as promised here's the code.
I'm prepared to send you is a full working console application that can be configured to match your needs.
Please give me your email so I can mail it to you. If you prefer mail me to mail@alexcode.com
On the application forder you'll find 3 files
  • ETL.exe   (the application)
  • ETL.exe.config   (the application's configuration file)
  • ProjectPurpose.htm   (some instructions... nothing facy)
The code was developed using VS 2008 so you must have at least the Express edition to open it.

Please read the instructions and edit the configuration file before running the application, it will not run until you do so.

As this is a Console application you can even add to a windows task to run on a defined hour.

Send me the email and let me know what you think of it.

Cheers,
Alex

pchen0688

ASKER
AlexCode,

Thanks for your help!

Here is my e-mail address:
pchen0688@gmail.com
Perry
pchen0688

ASKER

The smple code provided by AlexCode is what I have been looking for. I tried it and it works.

Thanks all!

Perry
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Alexandre Simões

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.