Read data from Excel file using Apex in Salesforce - javascript

I have a requirement where we need to parse the excel file and insert the data in one of the object using apex. This excel file contains multiple sheets. We need to read the data from multiple sheets (as of now one sheet is also fine) and upload the data.
Please help me with a solution.

I would suggest to provide more details about where the question starts and ends since it implies you already have the data available in your apex class but also doesn't include any information about the type/format you have it loaded as etc.
Here is a concept of what you might do (using a visualforce page and an apex controller) in case you are trying to figure out where to start:
1) Use apex:inputFile to load up the file into your apex controller (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputFile.htm)
Note: You can only upload files <= 10Mb this way
2) I would then send the uploaded object from my apex controller back to visualforce
One way to do this is to have a public property in the apex (e.g. MyExcelFile), and have your form trigger an apex:actionFunction to rerender a hidden component whose content is {!MyExcelFile} + run your parsing logic
(https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_display_field_values.htm)
(https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm)
3) Then I could parse it using whatever javascript library I felt like in my visualforce page (How to parse Excel file in Javascript/HTML5)
4) From there it is just a matter of calling a function in your apex controller which does the insert/update/whatever operation(s) and passing the params
(https://eltoro.secure.force.com/PassingParametersFromVisualforcePageToApexControllerViaApexParam)

Related

PowerBi: Query HTML table

What I need
I need to retrieve data from this source . Let's assume I must use only PowerBi for this.
What I did so far
If I use the basic web source option, then the query is just basically an htlm parsing with which I can easily get the data found in the html scope of the page, example:
Source:
The steps I'm following through Web source option:
Query:
(to simplify the example, assume we don't need the dates)
You can download that example .pbix file here.
The problem
The problem is that I need more data, which can't be accessed through the html preview. For example, let's imagine I need to retrieve the data from January 2010 to April 2020. Those king of queries can only be done via this button located in the webpage (which exports the requested data to an Excel workbook):
The idea is to get this process automated, so going to the source and export the excel file all the time is not an option.
Inspecting the element I realized that what it does is execute a javascript function:
The question
As a PowerBi/PowerQuery noob I wonder: Is there any way I can get that data directly with PowerBi (maybe calling the js function somehow)? If there is so, then how?
Thank you in advance.
The solution to my case was to use URL parameters to retrieve de data without parsing the html table.
❌Original URL I was using:
https://gee.bccr.fi.cr/indicadoreseconomicos/Cuadros/frmVerCatCuadro.aspx?idioma=1&CodCuadro=%20400
✔️New URL for the query, adding some parameters:
https://gee.bccr.fi.cr/indicadoreseconomicos/Cuadros/frmVerCatCuadro.aspx?idioma=1&CodCuadro=%20400&Idioma=1&FecInicial=2010/01/01&FecFinal=2040/01/01&Filtro=0&Exportar=True
This procedure only works in this case, because obviously the parameters will not be the same on other web pages.
However, I post this answer to keep the main idea for those who are in a similar situation: first try with the appropriate url parameters to get the data in a different format. Of course you first must know which are the available parameters, which is a limitation.

I have been given a cust_key I can use to import and display data from an API and need specific examples

I am barely getting around in HTML5, and trying to do PHP and AJAX and js all at the same time. LOL. I have been given a cust_key and an API that produces an XML output based off the criteria you send in via a url. To integrate this feature to your site you will need to be able to pull information from an XML file. I have an html table built, and the radio buttons, ... now I'd like to GET the info from the remote site (how do I attach my cust_key to results from all the radio buttons?) and display the results on my own site?

How to send and store data in an XML file, using an HTML form?

I need to create a report generation system which sends the reports to a DB. Can I replace the DB with an XML file, so that I don't have to use a server. Is it even possible?
You can create an HTML 5 web page that generates an xml file and let the user save it.
On the next time the user can supply the page with the last generated xml.
Look here: How to open a local disk file with Javascript?
Also, you can use HTML 5 key value store for loading every time the last data file generated.
I would recommend exploring those 2 options, and maybe later on adding a server to store the data.

Embed PDF w/POST Parameters

I need to pass a bunch of parameters to a web API method which returns a byte array that is needed to display a PDF report on a page. I tried doing this via a GET method but the query string is too long and it throws an exception. I have tried shortening the query string as much as physically possibly but it is still too long. All of the external PDF viewer libraries require a string path to the PDF. I need something that allows me to supply a string path and some parameters or just a string path but that uses a POST instead of a GET. I was thinking about POSTing the object values to the web API along with a GUID and storing it somehow on the server then with an tag calling another API method and retrieving the stored data by only passing the GUID as a parameter but I don't want to make a database table for only that to save and retrieve the data. I entertained the idea of saving the byte array to a PDF on the server and then simply pointing a PDF viewer to that after but I had no success with that either and that is not ideal.
File.WriteAllBytes(path, res);
The code above is the most common way I have found to save the file but when I open the new PDF file after it says it is corrupt and it won't allow me to open it. Ideally I would just find a method that allows me to POST data to my web API then embeds the result in a pdf.js/pdfObject/ control. Please help, this has stumped me for days. Thanks in advance!!

Accessing File contents based on Key using AJAX or Jquery

My web application uses file which has been updated by other process. My application reads the content of the file using ajax
xmlhttp.open("GET","/config/myfile",false);
xmlhttp.send();
Once response is received then app parses this response and shows that values on Web UI. The file contains 50 fields and whenever I want to read any single field I need to open whole file.
Is there any way to get the values of single field based on key instead of reading whole file.
As per my understanding we need to read and open file and then parse the response text. But would like to know is there any way to reduce the file calls with any other method.
I want to achieve this to reduce the file I/O operations. Since other processes are writing in to it and at same time my web app accessing to read the latest value.
Any other option would be appreciated.
Note :- I do not have to use any server side scripting lang option.
Regds

Categories

Resources