I am making an application which first imports the data from the Excel file and then uses it in the deployment of a CDF. But is it possible to change the data of the Excel file and see its corresponding CDF without executing the code in the notebook?

I have imported y1 and y2 from an excel file. Here is the code.

{DynamicModule[{y = 
Import["C:\\Users\\murali\\Desktop\\demo.xlsx"], y1 = y[[1, 1]],   y2 = y[[1, 2]], q1, q2}, 
Panel@Column[{Spacer[70] Text[
    Style["iPhone and iPad quarterly sales- units, million", Bold,
      18]], Column[{Spacer[70], 
    Text[Style["Click on/off:", Bold, 14]], q1 = True; q2 = True; 
       Button["     ", q1 = ! q1, Background -> Blue, 
        Appearance -> "Frameless"], "iPhone", {Right}], 
      Labeled[Button["     ", q2 = ! q2, Background -> Orange, 
        Appearance -> "Frameless"], "iPad", {Right}]}], 
        Table[{If[q1, y1[[i]], 0], If[q2, y2[[i]], 0]}, {i, 1, 22,
           1}], BarSpacing -> 0.8, ChartStyle -> {Blue, Orange}, 
        ImageSize -> {600, 280}, 
        GridLines -> {None, Automatic}]}]}]}]]}]

I am importing these values just as default and want that the end user can change these values. i.e.

I don't want the end user to go to the code and execute again and again if he wants to change some of the values of y1 and y2.

Is it possible that he can change value and see the difference in the CDF itself?

  • $\begingroup$ If you're on version 7 or higher you should use FileNameJoin[{"C","Users","murali","Desktop","demo2thomson.cdf"}] instead of ToFileName. This makes sure your code works the same on OSX, Linux and windows. Additionally, it's better to make the path relative to the directory where your application resides in using NotebookDirectory[]. $\endgroup$ Jun 15, 2012 at 8:13
  • 1
    $\begingroup$ Have a look at FileDate (to check when a file was last modified) and CreateScheduledTask (to Import the file if it has been changed). I'll try to post an answer later on. $\endgroup$ Jun 15, 2012 at 8:42
  • $\begingroup$ ok Thanks both of you.... I will have a look on these functions $\endgroup$
    – Jennifer
    Jun 15, 2012 at 8:52
  • 1
    $\begingroup$ @Jennifer: If you use cdf because the receipient needs to run this without a full mathematica license within the cdf-player, AFAIK it just won't work: Import will return $Failed if used within cdf player with a local file. I think it is possible to Import from a public URL, but you would have to test that (I haven't). If the receipient does have a full Mathematica or a PlayerPro this is actually not really a CDF specific question and you might get more people interested if you ask for Import within a DynamicModule which all comments and answers so far do actually treat (only). $\endgroup$ Jun 15, 2012 at 16:23
  • 1
    $\begingroup$ @Jennifer: have you tested your CDF file with the CDF player? If it's working in Mathematica that doesn't mean it also will work in the CDF player. Or are you importing those values just as defaults and want the user to be able to change those values? It think it's not only me who obviously doesn't understand what exactly you try to do, so maybe you should reformulate your questions... $\endgroup$ Jun 15, 2012 at 22:37

3 Answers 3


One starting point is as follows; it's not ideal because one has to actually save the file before Mathematica picks up the changes. Also I don't know if it will work in the CDF.

file = "test.xls";


(* {{{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}}} *)

lastModified := FileDate[file, "Modification"]; (* date of last modification *)
currentData := Import[file];

previousModified = FileDate[file, "Modification"];
startingData = Import[file];

liveData := Module[{currentTime, updateQ},currentTime = lastModified;updateQ = AbsoluteTime[currentTime] > AbsoluteTime[previousModified];{previousModified, startingData} = If[updateQ, {currentTime, currentData}, {currentTime, startingData}]]

(* liveData will Import the data and update the date of last modification *)

This will perform the check every second :

t = CreateScheduledTask[liveData, 1.0];

Now you can start the task and watch the file being changed :



Remember to stop the task when you're done :

  • 1
    $\begingroup$ For all I know this will not work within the CDF player (but might run in Mathematica or Player Pro if saved to a CDF file). Import for local files gives me OpenRead::sandbox error messages in the CDF Player for code which works fine in Mathematica. And I think that is roughly what the documentation states and not a bug... $\endgroup$ Jun 15, 2012 at 22:40
  • $\begingroup$ @b.gatessucks: Thanks for this help. aIt is able to retrieve the dynamic data from the Excel file. But can you tell how can I make barchart of the dynamic values Dynamic[liveData[[2,1,1]]] $\endgroup$
    – Jennifer
    Jun 17, 2012 at 15:21
  • $\begingroup$ @Jennifer I'd substitute my livedata with your y and leave the rest of your code untouched. $\endgroup$ Jun 17, 2012 at 16:23
  • $\begingroup$ @b.gatessucks: ok ...Thanks $\endgroup$
    – Jennifer
    Jun 17, 2012 at 17:23

From your comments I guess you look for something like below, although I'm still not sure as your question is somewhat hard to understand. I have also separated the code into:

  1. defining pathes and import default data:

    directory = FileNameJoin[{$HomeDirectory, "Desktop"}];
    y = Import[FileNameJoin[{directory, "demo.xlsx"}]];
  2. defining the graphical user interface (I used slightly different constructs as your original code), adding inputfields for the values of y1 and y2:

    gui = DynamicModule[{y1 = y[[1, 1]], y2 = y[[1, 2]], q1 = True, q2 = True},
           Text[Style["iPhone and iPad quarterly sales- units, million", Bold, 18]], 
           Alignment -> Center
         ], SpanFromLeft},
         {Spacer[{0, 20}], SpanFromLeft},
             With[{k = k},InputField[Dynamic[y1[[k]]], Number, FieldSize -> 5]],
            With[{k = k},InputField[Dynamic[y2[[k]]], Number, FieldSize -> 5]], 
         {Text[Style["Click on/off:", Bold, 14]], SpanFromLeft},
             Toggler[Dynamic[q1], {True -> Spacer[{18, 18}], False -> Spacer[{18, 18}]}, 
              Background -> Dynamic[If[q1, Blue, LightGray]]], 
             "iPhone", {Right}],
             Toggler[Dynamic[q2], {True -> Spacer[{18, 18}], False -> Spacer[{18, 18}]}, 
              Background -> Dynamic[If[q2, Orange, LightGray]]], 
             "iPad", {Right}]
            }], SpanFromLeft},
            Table[{If[q1, y1[[i]], 0], If[q2, y2[[i]], 0]}, {i,Length[y1]}],
            BarSpacing -> 0.8, ChartStyle -> {Blue, Orange}, 
            ImageSize -> {600, 280}, GridLines -> {None, Automatic}
          ], SpanFromLeft
         Alignment -> {Left, Center}
  3. Only once the above works as intended, export to cdf:

    CDFDeploy[FileNameJoin[{directory, "testdemo.cdf"}], gui, Method -> "Standalone"]
  • $\begingroup$ Thanks for your help and it is working but I dont want the input fields to be shown in the output. I was just trying to import modified values in the code as answered by 'Mr.b.gatessucks'. $\endgroup$
    – Jennifer
    Jun 18, 2012 at 10:48
  • $\begingroup$ "I was importing those values just as default and want the user to change those values." ??? $\endgroup$ Jun 18, 2012 at 18:52
  • $\begingroup$ @ Albert Retey: I wanted that first the data should be imported from the excel file and display its output, But if end user wants to change the data, then it should change it in the same excel file and display its corresponding output. $\endgroup$
    – Jennifer
    Jun 18, 2012 at 19:05
  • $\begingroup$ @Jennifer: As I said, I don't think importing a local will work if running in the CDF-Player, are you aware of that? Running in Mathematica or PlayerPro will work, though. You could use b.gatessucks suggestion then. $\endgroup$ Jun 18, 2012 at 23:31
  • $\begingroup$ @ Albert Retey: I was not aware of PlayerPro but as you said, I will try importing, using PlayerPro as soon as I get its license. $\endgroup$
    – Jennifer
    Jun 19, 2012 at 5:19

Import is disabled in the default CDF document reader. There are two common options to get around this:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.