Wednesday, 15 January 2014

How to generate an HTML page from Mapbasic

In this post I will demonstrate how to generate an HTML page from within Mapbasic and open it in Internet explorer. 

You can download the sample code from here ExampleHTML.mb

This mbx generates static html code that is opened in Internet explorer. I have used similar code to this to generate reports and elegantly style help documentation for an mbx. By including detailed css and javascript in your page you can style tables, group data, add images and even have interactive controls (this is obviously dependent on your web development experience).

Include "ICONS.DEF"
Include "MENU.DEF"

Declare Sub Main
Declare Sub ShowHtmlPage
Declare Sub ExitExample

Sub Main

   Create Menu "Example HTML" As
      "Show Example html" Calling ShowHtmlPage,
      "Exit Example" Calling ExitExample
   Alter Menu Bar Add "Example HTML"

End Sub
Sub ShowHtmlPage
OnError Goto CatchEx

    Dim strHtml as String


     strHtml = "<!DOCTYPE html>" & Chr$(13) &
                    "<html>" & Chr$(13) &
                       "<head>" & Chr$(13) &
                          "<title>Example HTML</title>" & Chr$(13) &
                          "<style type=" & Chr$(34) & "text/css" & Chr$(34) & ">" & Chr$(13) &
                          "h1 {color:#40B3DF;}" & Chr$(13) &
                          "h2 {color:black;font-family:" & Chr$(34) & "Arial" & Chr$(34) & ";}"
                          & Chr$(13) &
                          "p {color:black;font-family:" & Chr$(34) & "Arial" & Chr$(34) & ";}"
                          & Chr$(13) &
                         "</style>" & Chr$(13) &
                       "</head>" & Chr$(13) &
                   "<body>" & Chr$(13) &
                       "<h1>Example HTML</h1>" & Chr$(13) &
                       "<h2>Heading</h2>" & Chr$(13) &
                       "<p></p>" & Chr$(13) &
                       "<p>This is an example snippet of code to show how easy it is to create " &
                       "a html page using MapBasic. </p>" & Chr$(13) &
                   "</body>" & Chr$(13) &


Dim strTempHtmlFile as String
strTempHtmlFile = GetFolderPath$ (FOLDER_MYDOCS) & "\~example.html"

Open File strTempHtmlFile For Output as #1
   Print #1, strHtml
Close File #1

Dim bInternetExplorerExists as Logical
bInternetExplorerExists = false

Dim strInternetExplorerDir as String

If FileExists("C:\Program Files (x86)\Internet Explorer\iexplore.exe") then
    strInternetExplorerDir = "C:\Program Files (x86)\Internet Explorer\iexplore.exe"
    bInternetExplorerExists = true
End If

If FileExists("C:\Program Files\Internet Explorer\iexplore.exe") then
   strInternetExplorerDir = "C:\Program Files\Internet Explorer\iexplore.exe"
   bInternetExplorerExists = true
End If

If NOT bInternetExplorerExists then
   Note "Cannot display the html file because internet explorer is not installed!"
   Run Program strInternetExplorerDir & " " & strTempHtmlFile
End If

   Exit Sub
   Note Error$()
   Resume Done
End Sub
Sub ExitExample
   End Program
End Sub

If you have any questions please don't hesitate to ask



No comments:

Post a Comment