Group Abstract Group Abstract

Message Boards Message Boards

CDF for serializing XLSX

Posted 2 years ago

I have some code that takes spreadsheets in a specific format, and breaks them into CSVs. This isn't easily deployed to Wolfram Cloud since the output is a folder of CSVs, and I would prefer to have the serialization to happen in the same folder as the source file.

Since I don't have Enterprise Mathematica, what options are there to share this tool with others? CDF doesn't allow me through FormPage or InputField to use a file as an input.

POSTED BY: Eric Smith
Posted 2 days ago

Eric,

The limitation you've hit is correct: CDF Player/Free CDF intentionally blocks file system access to prevent arbitrary code execution on the end-user's machine.

This means you cannot use InputField or FormPage in a CDF to browse for a local file and then save multiple output files to the same location.

Alternative Deployment and Sharing Options

A) Cloud-Based Solution

Use the Wolfram Cloud to handle the file operations then compress the multiple CSV outputs into a single ZIP file for the user to download.

Tool Structure

You can use the APIFunction or a FormFunction to manage the process entirely on the Cloud.

Input

Use FormFunction with the parameter type "UploadedFile" for the user to upload the XLXSX file.

Process

The function processes the uploaded file's data and generates a list of CSV files in the Cloud's temporary directory.

Compress

Use CreateArchive to combine all generated CSVs into a single .zip file.

Output

Use Export with the "Attachment" option to force the browser to download the resulting ZIP file.

Example Code

CloudDeploy[
 FormFunction[
  {"Spreadsheet" -> "UploadedFile"},
  Module[{data, sheetNames, tempDir, archivePath},
   (* 1. Import all sheets from the uploaded file *)
   data = Import[#Spreadsheet, {"XLSX", "Data", All}];
   sheetNames = Import[#Spreadsheet, {"XLSX", "SheetNames"}];

   (* 2. Create a temporary folder in the cloud for the CSVs *)
   tempDir = CreateDirectory[FileNameJoin[{$TemporaryDirectory, "CSVs"}]];

   (* 3. Export each sheet as a separate CSV file *)
   MapIndexed[
    Export[FileNameJoin[{tempDir, sheetNames[[#2[[1]]]] <> ".csv"}], #1] &,
    data
   ];

   (* 4. Create a ZIP archive of the entire temporary folder *)
   archivePath = CreateArchive[tempDir, "Serialized_CSVs.zip", "ZIP"];

   (* 5. Deliver the ZIP file as a browser download *)
   Export[archivePath, "Attachment"]
  ],
  "Download"
 ]
]
POSTED BY: Rob Pacey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard