Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

excel to text conversion

Status
Not open for further replies.

hariuliyar

Newbie level 4
Joined
Nov 14, 2011
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,313
Hello all
How we can convert excel file to text file using c/c++ programming.Please tell me whether fread and fscanf will work or what? I dont want to convert excel to CSV.I want to convert directly to .txt.Please help me. I want code to be written in C/C++ and also i dont want third party library which is paid.. .I dont want any readymade converters.
Thanks in advance.

Shreehari
 

If you don't want to use a library then you have to make your own, it would be nice if you could convert it using a function but it is not possible.
You have analyze the xls format and create conversion routines.

Alex
 

Thanks Alex.!!:p
my problem is about understanding the format..u know for text we can use fread fscan for file operations..whether the same can be used here also? if no why?
and also i am not against using any free library.But i dont want to use paid one.
 

I found
XLS is at the end of the list , there is a specification about the file format but it is complicated

I can't help much because I use embedded C for mcu and Delphi for PC applications so I'm not very familiar with C++ and I'm not familiar with XLS either

Alex
 
see the following c library. it is free not commercial.
**broken link removed**
 
Thanks srizbf...How to use that library..do u have any idea?
 

You say just "excel" file without further specification. Different versions have different formats however. Recent Excel version's *.xlsx files are e.g. zipped folders of XML files.
 

No i dont want xlsx ..only for xls format..
 

the zip contains codes for building them as dll.
it contains a file named 'xlsconverter' , reading it will give the fecilities avilable.
 

I can't think of a simple single step solution but this two step process should do the trick ...

Save the Excel file as a "Text (Tab delimited) (*.txt)" file.

Open the text file in MS-Word (or other editor that allows find and replace of special characters in an entire document).

Find and replace all of the Tabs with Line Breaks.

visit : How to convert exel to text | Techyv.com
 

This procedure allows you to export data from a worksheet range to a text file. You may specify the character (e.g, a space, tab, pipe, comma, etc) that separates the exported elements. Each row of cells in the worksheet is written to one line within the text file, and each item in that line is separated by the specified delimiter character. Any single character may be used as the delimiter.

The ExportToTextFile procedure follows. The parameters to ExportToTextFile are described in the following table:

Parameter Description
FName The name of the file to which the data will be written. The file will be created if it does not exist. See AppendData below.

Sep The character that is to separate the elements on each row of the exported file. Typically, this is vbTab, a space, a comma, semicolor, or pipe ( | ). Any character may be used.

SelectionOnly If True, only the currently selected cells are exported. If False, the entire used range of the worksheet is exported.

AppendData If True and FName exists, data is written to the end of the text file, preserving the existing contents. If False, the existing contents of FName are destroyed and only the newly exported data will appear in the output file.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportToTextFile
' This exports a sheet or range to a text file, using a
' user-defined separator character.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportToTextFile(FName As String, _
Sep As String, SelectionOnly As Boolean, _
AppendData As Boolean)

Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String


Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile

If SelectionOnly = True Then
With Selection
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
Else
With ActiveSheet.UsedRange
StartRow = .Cells(1).Row
StartCol = .Cells(1).Column
EndRow = .Cells(.Cells.Count).Row
EndCol = .Cells(.Cells.Count).Column
End With
End If

If AppendData = True Then
Open FName For Append Access Write As #FNum
Else
Open FName For Output Access Write As #FNum
End If

For RowNdx = StartRow To EndRow
WholeLine = ""
For ColNdx = StartCol To EndCol
If Cells(RowNdx, ColNdx).Value = "" Then
CellValue = Chr(34) & Chr(34)
Else
CellValue = Cells(RowNdx, ColNdx).Value
End If
WholeLine = WholeLine & CellValue & Sep
Next ColNdx
WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
Print #FNum, WholeLine
Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ExportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Because the ExportToTextFile procedure accepts input parameters, you must call it from other VBA code, such as the following:

Sub DoTheExport()
ExportToTextFile FName:="C:\Test.txt", Sep:=";", _
SelectionOnly:=False, AppendData:=True
End Sub

In the example DoTheExport procedure above, the file name and the separator character are hard coded in to the code. If you want to prompt the user for the file name and the separator character, use code like the following:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DoTheExport
' This prompts the user for the FileName and the separtor
' character and then calls the ExportToTextFile procedure.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DoTheExport()
Dim FileName As Variant
Dim Sep As String
FileName = Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="Text Files (*.txt),*.txt")
If FileName = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Sep = Application.InputBox("Enter a separator character.", Type:=2)
If Sep = vbNullString Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
Debug.Print "FileName: " & FileName, "Separator: " & Sep
ExportToTextFile FName:=CStr(FileName), Sep:=CStr(Sep), _
SelectionOnly:=False, AppendData:=True
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END DoTheExport
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
This code will prompt the user for a text file name in which to save the file and for the separator character. If the user cancels either of these dialogs, the procedure is terminated and no export operation is carried out.

View more at: How to convert exel to text | Techyv.com
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top