Attaching the generated xlsx file to a record in Salesforce as a file attachment #3039

Closed
opened 2023-11-30 12:13:17 +00:00 by sameermuhamadd · 1 comment

I have created an xlsx file using sheetjs with Salesforce LWC (https://docs.sheetjs.com/docs/demos/cloud/salesforce/). The problem is that the file is downloaded directly. I want it to be saved in Salesforce as well on a record. Is there any way to achieve this?

Attaching image for reference.

I have created an xlsx file using sheetjs with Salesforce LWC (https://docs.sheetjs.com/docs/demos/cloud/salesforce/). The problem is that the file is downloaded directly. I want it to be saved in Salesforce as well on a record. Is there any way to achieve this? Attaching image for reference.
Owner

The XLSX.writeFile call creates the file and initiates the download:

  @api async download() {
    await loadScript(this, sheetjs); // load the library
    // create workbook
    var wb = XLSX.utils.book_new();
    var ws = XLSX.utils.aoa_to_sheet(this.aoa);
    XLSX.utils.book_append_sheet(wb, ws, "Data");
    // export
    XLSX.writeFile(wb, "SheetForceExport.xlsx"); // <-- this line creates the file and initiates a download
  };

Instead, you can use XLSX.write . You must pass an options argument that specifies bookType: "xlsx" (file format) and a desired type (output type). For example, the following line generates a base64 string representing the workbook:

    var b64 = XLSX.write(wb, { bookType: "xlsx", type: "base64" });

If you need the data in a Uint8Array:

    var u8 = XLSX.write(wb, { bookType: "xlsx", type: "buffer" });

XLSX.write returns the file data without initiating a download.


There is likely some sort of API that lets you upload the generated file data. If not, you should be able to make a POST request to some endpoint in the Salesforce API (see https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/intro_input.htm#cc_upload_binary_files for an example).

The `XLSX.writeFile` call creates the file and initiates the download: ```js @api async download() { await loadScript(this, sheetjs); // load the library // create workbook var wb = XLSX.utils.book_new(); var ws = XLSX.utils.aoa_to_sheet(this.aoa); XLSX.utils.book_append_sheet(wb, ws, "Data"); // export XLSX.writeFile(wb, "SheetForceExport.xlsx"); // <-- this line creates the file and initiates a download }; ``` Instead, you can use `XLSX.write` . You must pass an options argument that specifies `bookType: "xlsx"` (file format) and a desired `type` (output type). For example, the following line generates a base64 string representing the workbook: ```js var b64 = XLSX.write(wb, { bookType: "xlsx", type: "base64" }); ``` If you need the data in a Uint8Array: ```js var u8 = XLSX.write(wb, { bookType: "xlsx", type: "buffer" }); ``` `XLSX.write` returns the file data without initiating a download. --- There is likely some sort of API that lets you upload the generated file data. If not, you should be able to make a POST request to some endpoint in the Salesforce API (see https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/intro_input.htm#cc_upload_binary_files for an example).
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3039
No description provided.