How can i read a Excell file in Fortran
How can i read a Excell file in Fortran
(OP)
Hello everyone,
I want to make a program that is basicly a mineral database, so I have a list of minerals and after that i have some options like organize them etc.
But i have a problem, i have the Excel list and I want export it to read it in Fortran.
How can I do that??
Do i have to put the list in ".txt" format?
Greetings to everybody
I want to make a program that is basicly a mineral database, so I have a list of minerals and after that i have some options like organize them etc.
But i have a problem, i have the Excel list and I want export it to read it in Fortran.
How can I do that??
Do i have to put the list in ".txt" format?
Greetings to everybody
RE: How can i read a Excell file in Fortran
RE: How can i read a Excell file in Fortran
Im using :
open (1, file='listaminerais.csv', status='old', iostat=stat)! Open file
if (stat .ne. 0) then
write(*,*) 'File cannot be opened
end if
But it isnt working!! :S
RE: How can i read a Excell file in Fortran
Given is a CSV file
csv_file.csv
CODE
foo;123
bar;456
baz;789
csv_read.f95
CODE
type csv_header_line
character(10) :: c01 ! column 01
character(10) :: c02 ! column 02
end type csv_header_line
type csv_data_line
character(10):: c01 ! column 01
integer :: c02 ! column 02
end type csv_data_line
contains
subroutine parse_header(separator, line, header)
character(*), intent(in) :: separator, line
type(csv_header_line), intent(out) :: header
integer :: separator_pos
!separator position
separator_pos = index(line, separator)
header%c01 = adjustl(trim(line(:separator_pos-1)))
header%c02 = adjustl(trim(line(separator_pos+1:)))
end subroutine parse_header
subroutine parse_data(separator, line, dta, nr)
character(*), intent(in) :: separator, line
integer, intent(in) :: nr
type(csv_data_line), dimension(:), intent(inout) :: dta
integer :: separator_pos
!separator position
separator_pos = index(line, separator)
dta(nr)%c01 = adjustl(trim(line(:separator_pos-1)))
! convert substring into integer number
read (line(separator_pos+1:),'(I3)') dta(nr)%c02
end subroutine parse_data
end module csv_mod
program csv_read
use csv_mod
implicit none
integer, parameter :: max_csv_lines = 100 ! max number of lines in CSV
integer, parameter :: csv_columns = 2 ! number of columns in CSV
integer stat, line_nr, nr, j, computed_number
character :: separator = ';'
character(80) :: line
type(csv_header_line) :: csv_header
type(csv_data_line), dimension(max_csv_lines) :: csv_data
! open file
open (1, file='csv_file.csv', status='old', iostat=stat)
if (stat .ne. 0) then
write(*,*) 'File cannot be opened !'
go to 99
end if
write(*,*) 'Reading CSV-file...'
! process file
line_nr = 0
do while (.true.)
read(1, '(A)', end=99) line
line_nr = line_nr + 1
if (line_nr .eq. 1) then
call parse_header(separator, adjustl(trim(line)), csv_header)
else
! if line_nr > 1 then parse data line
nr = line_nr - 1
call parse_data(separator, adjustl(trim(line)), csv_data, nr)
end if
end do
! close file
99 continue
close(1)
write(*,*) 'Done.'
write(*,*) 'Number of all lines found in CSV = ', line_nr
write(*,*) 'Number of data lines found in CSV = ', nr
! write the data
write(*, '(A)') '****************************************'
write(*,'(A10, A10, A10)') csv_header%c01, &
adjustr(csv_header%c02), &
'3*NUMCOL'
write(*, '(A)') '****************************************'
do j = 1, nr
computed_number = 3 * csv_data(j)%c02
write (*,'(A10, I10, I10)') csv_data(j)%c01, &
csv_data(j)%c02, &
computed_number
end do
write(*, '(A)') '****************************************'
end program csv_read
CODE
$ csv_read
Reading CSV-file...
Done.
Number of all lines found in CSV = 4
Number of data lines found in CSV = 3
****************************************
CHARCOL NUMCOL 3*NUMCOL
****************************************
foo 123 369
bar 456 1368
baz 789 2367
****************************************
RE: How can i read a Excell file in Fortran
Can you use ActiveX Data Objects to QUERY the Excel Sheet as a table?
I know it can be done in VB. Don't know if you can manipulate objects in FORTRAN.
Skip,
Just traded in my old subtlety...
for a NUANCE!
RE: How can i read a Excell file in Fortran
I'm using on the windows g95 and gfortran and I cannot imagine how to use ActiveX Objects with these compilers. But, maybe another commercial fortran compilers for windows support that.