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
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#?
//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.
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
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)
Provider=Microsoft.Jet.OLE
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\SOFTWA
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."