I need to write a small script to take the contents of an XLS file and lay them out nicely in a web page. So far, so easy. However, the spreadsheet text will be the content of a series of social media posts which includes emojis within that content. I did a very basic test and found that the emojis turn into question marks when dropped to the page by the script.
I don't particularly like emojis myself but I don't get to choose whether they are included in the text, and they need to appear in the end result.
Here's what a stripped back version of the script looks like, with no formatting etc.
<%# Language=VBScript%>
<html><head></head>
<body>
<%
Dim objConn, objRS
Set objConn = Server.CreateObject("ADODB.Connection")
filepath = "xls/OPG.xls"
objConn.Open "DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=NO; Excel 8.0; DBQ=" & Server.MapPath(filepath) & "; "
strSQL = "SELECT * FROM A1:C400"
Set objRS=objConn.Execute(strSQL)
Set DataList = CreateObject("ADOR.Recordset")
Do Until objRS.EOF
Response.Write objRS.Fields("Post Description").Value & "<br /><br />"
objRS.MoveNext
Loop
objConn.Close
Set objConn=Nothing
%>
</body>
</html>
Here's what the original XLS file looks like...
...and here's how that comes into the web page...
How do I get around this? Any ideas?
Thanks
The Microsoft Access Database Engine is able to read and display emojis from Excel files:
https://www.microsoft.com/en-us/download/details.aspx?id=54920
You will need to set your codepage and charset to UTF-8 though:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
response.Charset = "utf-8"
%>
Connection string:
objConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath(filepath) & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
Tested and working.
Related
I want to change the date on this site to some future date (next summer), but I don't know how to.
The webpage code I am working with looks like this,
<div class="c24-travel-input-wrapper"><div class="c24-travel-tablet-input-overlay c24-travel-filled">Sa, 10.09.2022</div>
<input type="text" name="departureDate" data-package-date="10.09.2022" data-lastminute-date="10.09.2022" data-earlybird-date="31.12.2022" data-hotelonly-date="10.09.2022" data-allinclusive-date="10.09.2022" data-train-date="12.09.2022" data-user-package-date="" data-user-lastminute-date="" data-user-earlybird-date="" data-user-hotelonly-date="" data-user-allinclusive-date="" data-user-train-date="" data-valid-query-date="" data-param-map="departureDate" placeholder="tt.mm.jjjj" data-validate-ele="1" id="c24-travel-departure-date-element" data-default-searchrequest-value="16.09.2022" data-saved-date="" tabindex="" data-defaultvalue="" class="c24-travel-validate-ele" value=""><div class="c24-travel-departure-date-element-description c24-travel-date-element-description c24-travel-hidden" id="c24-travel-departure-date-element-description">01.01.</div><span class="c24-travel-icon-date c24-travel-input-icon c24-travel-date-btn"></span></div>
The date at the end of the first line is what I need to change. (c24-travel-input-wrapper)
I cannot edit the input field directly - you need to use the date picker popup, so I figure I should be using JavaScript, something like this...
Dim ch As Selenium.ChromeDriver
Dim setDate As Selenium.WebElement
Dim attrScript As String
attrScript = "arguments[0].setAttribute(" & myTravelDateS & ")" 'myTravelDateS is set earlier - example is So, 01.01.2023
ch.ExecuteScript attrScript, setDate
But I don't have enough knowledge to make this work.
The web address is https://urlaub.check24.de/
I found the solution.
Set setDate = ch.FindElementByCss("div.c24-travel-tablet-input-overlay.c24-travel-filled")
attrScript = "arguments[0].innerText = '" & myTravelDateS & "'"
ch.ExecuteScript attrScript, setDate
innerText was what I needed to know.
Thanks a lot to this great community, so much knowledge and information tucked away in these pages.
My VB.NET app currently allows me to convert CSV files to a datatable thanks to the code provided by David in this question I posted: Previous Question
Now I am trying to allow .XLSX files to be imported to a datatable as well. Currently the code looks like this:
Private Function ConvertCSVToDataTable(ByVal path As String) As DataTable
Dim dt As DataTable = New DataTable()
Using con As OleDb.OleDbConnection = New OleDb.OleDbConnection()
Try
If System.IO.Path.GetExtension(path) = ".csv" Then
con.ConnectionString = String.Format("Provider={0};Data Source={1};Extended Properties=""Text;HDR=YES;FMT=Delimited""", "Microsoft.Jet.OLEDB.4.0", IO.Path.GetDirectoryName(path))
ElseIf System.IO.Path.GetExtension(path) = ".xlsx" Then
con.ConnectionString = String.Format("Provider={0};Data Source={1};Extended Properties=""Excel 12.0 XML;HDR=Yes;""", "Microsoft.ACE.OLEDB.12.0", IO.Path.GetDirectoryName(path))
End If
Using cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM " & IO.Path.GetFileName(path), con)
Using da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd)
con.Open()
da.Fill(dt)
con.Close()
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString())
Finally
If con IsNot Nothing AndAlso con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Using
Return dt
End Function
However, when I run the code using the .XLSX file, I get the following error:
{"The Microsoft Office Access database engine cannot open or write to
the file 'C:\Users\XSLXFilePath'. It is already opened exclusively by
another user, or you need permission to view and write its data."}
The file is not open anywhere else to my knowledge. And the app also runs fine when .CSV file is put through it instead. How do I get the app to properly work for .XLSX, or any Excel file format?
I think that the error is that from the connection string and the OLEDB Command:
ConnectionString
You don't have to use IO.Path.GetDirectoryName(path) it returns the directory name, you have to provide the file full path:
con.ConnectionString = String.Format("Provider={0};Data Source={1};Extended Properties=""Excel 12.0 XML;HDR=Yes;""", "Microsoft.ACE.OLEDB.12.0", path)
Refer to this link for excel connectionstring generation function: import data from excel 2003 to dataTable
OLEDB Command
You must provide the Worksheet name in the Command instead of the Filename:
Using cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM [Sheet1$]" , con)
If the Sheet names is dynamic and you have to get the first sheet in the excel file:
Dim dbSchema as DataTable = con.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, null)
Dim firstSheetname as String = dbSchema.Rows(0)("TABLE_NAME").ToString
Using cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM [" & firstSheetname & "]" , con)
References
Reading from excel using oledbcommand
Read and Write Excel Documents Using OLEDB
Use can use the following connection string for .xlsx file.
I have used it and working fine.
P_FIle = ( File Name with path )
P_Con_Str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & P_File & ";Extended Properties=""Excel 12.0 XML;HDR=Yes;"""
How to play WAV file using VBScript in HTML?
I have got following code but it is not working.
<script type="text/vbscript" language="VBScript">
Dim strSoundFile, strCommand
strSoundFile = "C:\Sounds\Sound1.wav"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
</script>
I found another code but it doesnt work as well if I use it in HTML page but it is working great in a *.VBS.
Sub Play(SoundFile)
Dim Sound
Set Sound = CreateObject("WMPlayer.OCX")
Sound.URL = SoundFile
Sound.settings.volume = 100
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
End Sub
I have found this link https://support.microsoft.com/es-es/kb/279022
but I am not shure if it is a correct way...
The way is working fine via in BODY tag is following
<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="WindowsMediaPlayer"
width="242" height="202" style="position:absolute; left:1;top:1;">
<param name="URL" value="C:\Sound1.wav">
<param name="autoStart" value="1">
</object>
Could it be done using this?
<script type="text/vbscript" language="VBScript"> HERE </script>
I am using IE8 under MS Windows 7 Pro.
Finally I could find correct approach to do what I need is described here
Basically the idea is following
We cannot use code above in the HTML instead we have to use embedded Windows Media Player object.
We can create any methods we need to manipulate that object in VBScript.
I have a script that lists all files in a directory, then for each one it will Response.Write the name and how many downloads it has.
I have everything completed, but when I went for a test, the files that have "odd" characters in the name are replace with a ?
I'm guessing, that since some files have foreign languages as there name, and that some have the iPhone emoji icons in the name, that it doesn't recognize it and puts a ? instead, but this is a serious issue since I can't give the correct file name back to the user, then that incorrect name is fed back into the url to download. (Which doesn't work)
Any suggestions?
Edit:
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("."))
for each file in fo.files
if fs.GetExtensionName(file.Path) = "plist" then
dim tempList, tempName, ...
tempList = split(file.Name, ".")
'Manipulate name and data ...
Response.write(name)
end if
next
The file names themselves have odd characters, and file.Name returns a ? instead of what is actually there.
18アイコン is one example.
Here's some code which works fine for me:
<%# Language="VBScript" CodePage="65001" %><%
Option Explicit
Response.CodePage = 65001
Response.CharSet = "utf-8"
Dim fs, fo, file
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(Server.MapPath("."))
For Each file In fo.files
If fs.GetExtensionName(file.Path) = "plist" Then
' Do whatever here...
Response.Write file.Name & "<br>"
End If
Next
%>
If you are using any variables that you didn't dimension beforehand, you'll need to remove the Option Explicit; otherwise, VBScript will complain that you didn't dimension them.
Edit: I copy & pasted the wrong code; this code works.
I am using Office Web components to fill an Excel template with values. The template is in Excel xml format, containing all relevant fields and layout options including the page layout, landscape in this case. I'm filling this template with some real fields using the code below.
Set objSpreadsheet = Server.CreateObject("OWC11.Spreadsheet")
objSpreadsheet.XMLURL = Server.MapPath("xml") & "\MR1_Template.xls"
'Fill cells with values here
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "inline; filename=" & strFileNaam
Response.write objSpreadsheet.xmlData
After the new Excel file has been saved, the page layout options are gone. I've looked at the API documentation for the OWC but cannot find the option to specify the landscape page-layout
I'm not sure if you are passing in the right data. XMLURL seems like an odd method name to be passing in a XSL template into?
If all you are doing it doing a xsl transformation then why not just use DOMXmlDocument similar to this article:
http://www.codeproject.com/KB/XML/xml_spreadsheet_to_csv.aspx
Cut and paste for ease:
Dim xslt As New XslTransform
'Load the stylesheet.
xslt.Load(Server.MapPath(".") & "excel2csv.xsl")
Dim doc As New XmlDocument
'xmldata is string, use doc.Load(fileName) for file.
doc.LoadXml(xmlData)
'Create an XmlTextWriter which outputs to a file.
Dim fileName As String
fileName = Server.MapPath(".") & "book.csv"
Dim writer As XmlWriter = New XmlTextWriter(fileName, Nothing)
'Transform the data and send the output to the console.
xslt.Transform(doc, Nothing, writer, Nothing)
writer.Close()
After some detailed comparison of the template excel sheet (as xml) and the resulting xmlData I've decided to hack the page layout in the resulting Xml. These are the options I've added:
<x:WorksheetOptions>
<x:PageSetup><x:Layout x:Orientation="Landscape"/></x:PageSetup>
<x:FitToPage/>
<x:Print>
<x:FitWidth>2</x:FitWidth>
<x:ValidPrinterInfo/>
<x:PaperSizeIndex>9</x:PaperSizeIndex>
<x:Scale>87</x:Scale>
</x:Print>
</x:WorksheetOptions>