Can imagej read an excel file? A macro code that can read from excel file

I found some plugins that can export to excel but not vice versa. I need to import some parameters of the image from excel.

Hello Zeynab -

I am not aware of any modules that ship with stock Fiji / ImageJ
that can read from an excel file. I doubt that there are any. You
can find python and java code that can read from excel (so you
could write a jython (python) script or java plugin), but, in my past
experience, these kinds of things tend to be imperfect.

By far and away your best choice would be to use excel to export
your relevant data as a .csv (comma-separated-value) file, and
then easily read in that data with any of ImageJ’s supported
scripting languages. Trust, me – doing anything else just isn’t
worth the bother.

Thanks, mm


Thanks so much for your kind and quick response. I have not worker with .csv file before.
Can Fiji easily import from .csv? Let me explain what I need to be imported. I have x and y of many images that x and y show the edges of the specific image. I have to run a piece of code to do some steps and import each x and y of a image do specific analyzes and export to an excel file. Can csv file work in this context?

Hello Zeynab -

A .csv file looks like this:

10.0, 10.0
10.0, 20.0
20.0, 20.0
20.0, 10.0

Each line is a row of data, and the data items in each column are
separated by commas. If you had an excel spreadsheet that
contained four rows and two columns consisting of the above
data, this is (more or less) what you would get if you exported
it from excel as a .csv file.

I am not aware of any general-purpose tools built into Fiji / ImageJ
that will read .csv files for you. (I think Fiji uses .csv files in some
specific cases for data it reads and writes.) But it is easy to read
in such data “by hand” using jython (or java or ijm, etc.).

You might start with the following exercise:

Export a (small) table of number from excel as a .csv file.
Open that file in a text editor (e.g., notepad) just to see what it
looks like.
Write an ImageJ script in the scripting language of your choice
(e.g., jython), that opens that file and reads in the numbers.
Write the numbers to the Log window just to make sure you got
them right.

Thanks, mm


ImageJ has built in support for reading .csv files. Open a .csv file as a table by dragging and dropping it on the “ImageJ” windows or by using the File>Open command. Open a .csv file in a macro by using open("/path/to/file-name.csv"). Use the getResult(columnName,rowIndex) macro function to retrieve values from the table.


Thanks so much will try that.

Thanks so much will try that as well.

Please read the user guide in these places:

I am talking about importing from excel not exporting to excel.

If you save your Exel file as Excel CSV ( an available option) then you will be able to read it.

1 Like

Hello again,
I was in a hurry so I couldn’t explain more but here is maybe some help.
When you save your Excel file, save it as a CSV file. It is an option in your ‘Save As’ listing. From that you can open it in ImageJ using ‘Import Text Image’. Using that you can do many things such as use a LUT to make a heat map of your data and many other things. When you are done with anything then save it as BOTH Text Image AND Text(only,also an option in ImageJ) that way you can use the data many ways in many softwares.

1 Like

@smith_robertj Thanks for your suggestion. I need to call it in the macro language of imagej. Would your method work in this case?

Hello Zeynab,
Yes it will, but I must correct myself, I instructed you to save your Excel file as CSV and I was wrong. You have to save as a TXT file (also an option). I’m sorry to confuse you.

1 Like

Here are some experiments I did when I was figuring out how to move data back and forth.

// This macro opens the contents of the Results table in Excel
   if (nResults==0) exit("Results table is empty");
   path = "C:\\Users\\User\\Desktop\\MyData.xls";  //Change this to your desktop
  selectWindow("Results");   //Run something to get a Results table e.g. Measure blobs (sample image)
   saveAs("Measurements", path);   // saves off Results window to MyData.xls (see path above)
exec("open", path);
exec("cmd", "/c", "start", "excel.exe", path);  //Excel will complain...just ignore it and open the file.

//Edit in Excel and save as CSV (MS-DOS)  e.g. MyData.csv
//It will open back up in ImageJ where you can rename it as you like
//open it or drag and drop.

//Check out the "IJ.renameResults(oldName,newName)" macro command
// for manipulating the Results table name or rename another table "Results'
1 Like