Excel file on a dialog

Post Reply
Natter
Posts: 1279
Joined: Mon May 14, 2007 9:49 am

Excel file on a dialog

Post by Natter »

I need to show an excel file on a dialog. Can this be done ?
User avatar
Antonio Linares
Site Admin
Posts: 42840
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 173 times
Been thanked: 124 times
Contact:

Re: Excel file on a dialog

Post by Antonio Linares »

You may use a TWebView2 control and use this HTML code:

Code: Select all | Expand

<input type="file" id="input" accept=".xlsx, .xls" />
<div id="output"></div>
<script src="https://cdn.sheetjs.com/xlsx-latest/xlsx.full.min.js"></script>
<script>
  document.getElementById("input").addEventListener("change", async (e) => {
    const file = e.target.files[0];
    const data = await file.arrayBuffer();
    const workbook = XLSX.read(data);
    const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
    const html = XLSX.utils.sheet_to_html(firstSheet);
    document.getElementById("output").innerHTML = html;
  });
</script>
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1279
Joined: Mon May 14, 2007 9:49 am

Re: Excel file on a dialog

Post by Natter »

Thank you, Antonio ! Great solution !!!
Natter
Posts: 1279
Joined: Mon May 14, 2007 9:49 am

Re: Excel file on a dialog

Post by Natter »

Antonio, how can I open a specific document (C:\MyFile.xlsx) in webview ?
User avatar
Antonio Linares
Site Admin
Posts: 42840
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 173 times
Been thanked: 124 times
Contact:

Re: Excel file on a dialog

Post by Antonio Linares »

This should should work:

webviewexcel.prg

Code: Select all | Expand

#include "FiveWin.ch"

function Main()

   local oWebView := TWebView2():New()

   oWebView:SetHtml( Html() )
   oWebView:SetTitle( "Microsoft Edge WebView working from FWH" )
   oWebView:SetUserAgent( "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36" )
   oWebView:Run()
   oWebView:End()

return nil

function Html()

   local cHtml

   TEXT INTO cHtml
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Read Excel File</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        #output p {
            color: red;
        }
    </style>
</head>
<body>
    <h1>Upload and Display Excel File</h1>
    <input type="file" id="input" accept=".xlsx, .xls" />
    <div id="output"></div>

    <script src="https://cdn.sheetjs.com/xlsx-latest/xlsx.full.min.js"></script>
    <script>
        document.getElementById('input').addEventListener('change', async (e) => {
            const file = e.target.files[0];
            if (!file) return;
            try {
                const data = await file.arrayBuffer();
                const workbook = XLSX.read(data);
                const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
                const html = XLSX.utils.sheet_to_html(firstSheet);
                document.getElementById('output').innerHTML = html;
            } catch (error) {
                console.error('Error reading file:', error);
                document.getElementById('output').innerHTML = '<p>Error reading file.</p>';
            }
        });
    </script>
</body>
</html>
   ENDTEXT

return cHtml
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1279
Joined: Mon May 14, 2007 9:49 am

Re: Excel file on a dialog

Post by Natter »

Sorry, I don't understand. This example does not allow to open the file C:\MyFile.xlsx
User avatar
Antonio Linares
Site Admin
Posts: 42840
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 173 times
Been thanked: 124 times
Contact:

Re: Excel file on a dialog

Post by Antonio Linares »

This version is working fine here:

Code: Select all | Expand

#include "FiveWin.ch"

function Main()

   local oWebView := TWebView2():New()

   oWebView:SetHtml( Html() )
   oWebView:SetTitle( "Microsoft Edge WebView working from FWH" )
   oWebView:SetUserAgent( "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Mobile Safari/537.36" )
   // oWebView:OpenDevToolsWindow( .T. ) // Open DevTools
   oWebView:Run()
   oWebView:End()

return nil

function Html()

   local cHtml

   TEXT INTO cHtml
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Read Excel File</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        #output p {
            color: red;
        }
    </style>
</head>
<body>
    <h1>Upload and Display Excel File</h1>
    <input type="file" id="input" accept=".xlsx, .xls" />
    <div id="output"></div>

    <script src=https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js></script>
    <script>
        document.getElementById('input').addEventListener('change', async (e) => {
            const file = e.target.files[0];
            if (!file) return;
            try {
                const data = await file.arrayBuffer();
                const workbook = XLSX.read(data);
                const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
                const html = XLSX.utils.sheet_to_html(firstSheet);
                document.getElementById('output').innerHTML = html;
            } catch (error) {
                console.error('Error reading file:', error);
                document.getElementById('output').innerHTML = '<p>Error reading file.</p>';
            }
        });
    </script>
</body>
</html>
   ENDTEXT

return cHtml
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1279
Joined: Mon May 14, 2007 9:49 am

Re: Excel file on a dialog

Post by Natter »

When I select a file I get the message "Error reading file" (maybe I have an old version of WebView).

Is it possible to get properties of WebView object via GetProp(), GetPropA() ?
User avatar
Lailton
Posts: 187
Joined: Fri Jul 20, 2012 1:49 am
Location: Brazil
Has thanked: 2 times
Been thanked: 13 times
Contact:

Re: Excel file on a dialog

Post by Lailton »

To load local files you can combine the BIND events and send some request from web page to the FWH take the file ( convert to base64 ) and
read it from webpage.

:D
Regards,
Lailton Fernando Mariano
User avatar
Antonio Linares
Site Admin
Posts: 42840
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 173 times
Been thanked: 124 times
Contact:

Re: Excel file on a dialog

Post by Antonio Linares »

Natter wrote: Wed Apr 23, 2025 8:25 am When I select a file I get the message "Error reading file" (maybe I have an old version of WebView).

Is it possible to get properties of WebView object via GetProp(), GetPropA() ?
Please double check that you are using https://cdn.sheetjs.com/xlsx-0.20.3/pac ... ull.min.js

Here it works very fine :)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Natter
Posts: 1279
Joined: Mon May 14, 2007 9:49 am

Re: Excel file on a dialog

Post by Natter »

To load local files you can combine the BIND events and send some request from web page to the FWH take the file ( convert to base64 ) and
read it from webpage.
Lailton, could you give me a small example?
User avatar
Antonio Linares
Site Admin
Posts: 42840
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 173 times
Been thanked: 124 times
Contact:

Re: Excel file on a dialog

Post by Antonio Linares »

Dear Yuri,

Please email me your excel file and I will provide you a screenshot and the EXE
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply