|
|
|
|
- Example 1: Finding a file whose path contains the "Temp" character string in a archive
- Example 2: Finding a file of a given type in an archive
- Example 3: Finding a file of a given type in an xlsx archive
Example 1: Finding a file whose path contains the "Temp" character string in a archive Example 2: Finding a file of a given type in an archive Example 3: Finding a file of a given type in an xlsx archive This code is used to find a file in an xlsx archive, to modify it and to replace it in the archive.
// Physical name of the file (the xlsx files are ZIP) sNameXlsxFile is string = fDataDir() + "\Level 1.xlsx" // Logical name that will be used with the Zip functions sZipXlsxFile is string = "ZipXlsxFile" Â // Opening bZipOpened is boolean bZipOpened = (zipOpen(sZipXlsxFile, sNameXlsxFile, zipWrite)=0) // Opening OK? IF NOT bZipOpened THEN Error("Failure opening the Excel worksheet", ErrorInfo()) RETURN END // Find a file in the ZIP nSubscriptFilesheet1xmlrels is int // Exact-match search while specifying the path nSubscriptFilesheet1xmlrels = zipFindFile(sZipXlsxFile, ... "xl\worksheets\_rels\sheet1.xml.rels", True) // File found in the zip? IF nSubscriptFilesheet1xmlrels < 1 THEN // File not found Error("File not found in the XLSX file") ELSE // Retrieve the content of the file found bufFilesheet1xmlrels is Buffer bufFilesheet1xmlrels = zipExtractFile(sZipXlsxFile, ... nSubscriptFilesheet1xmlrels, zipInMemory) // This file is an XML, load it in an XMLDocument variable xmlSheet1 is xmlDocument = XMLOpen(bufFilesheet1xmlrels, fromString) IF ErrorOccurred = True THEN Error("Unable to load the XML file found in the XLSX file", ... ErrorInfo()) ELSE // Modify the links to replace the paths FOR nLinkNum = 1 _TO_ xmlSheet1.Relationships.Relationship..Occurrence xmlSheet1.Relationships.Relationship[nLinkNum]:Target = ... Replace(xmlSheet1.Relationships.Relationship[nLinkNum]:Target, ... "file:///\\formerserver\", "file:///\\newserver\", IgnoreCase) END // Retrieve the XML modified in the buffer bufFilesheet1xmlrels = XMLBuildString(xmlSheet1) // Delete the file found in the XLSX (in the ZIP) IF zipDeleteFile(sZipXlsxFile, nSubscriptFilesheet1xmlrels) <> 0 THEN Error("Unable to delete the XML file found in the XLSX file" + ... "to update it", ErrorInfo()) ELSE // Include the new file instead IF zipAddFile(sZipXlsxFile, bufFilesheet1xmlrels, ... zipInMemory, "xl\worksheets\_rels\sheet1.xml.rels") <> 0 THEN Error("Unable to add the new XML file located " + ... "in the XLSX file to update it", ErrorInfo()) ELSE Info("Modifications successfully performed") END END END END Â // In any case at the end END: // If the zip of xlsx is opened, close it IF bZipOpened THEN zipClose(sZipXlsxFile)
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|