×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How can i read a Excell file in Fortran

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

RE: How can i read a Excell file in Fortran

Save your Excel as CSV-file (Comma Separated Values), which is basically text file and process it with fortran.  

RE: How can i read a Excell file in Fortran

(OP)
And how can i export it to 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

Here is an example:
Given is a CSV file
csv_file.csv

CODE

CHARCOL;NUMCOL
foo;123
bar;456
baz;789
and here is the program
csv_read.f95

CODE

module csv_mod
  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
Output:

CODE

$ g95  csv_read.f95 -o csv_read

$ 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,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: How can i read a Excell file in Fortran

Gi SkipVought,
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.

 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close