<!--- Create a microsoft Excel workbook through the POI system. ---> <cfset objWorkbook = CreateObject( "java", "org.apache.poi.hssf.usermodel.HSSFWorkbook" ).Init() /> <!--- Get global CSS classes that have both a name and a value attribute. ---> <cfset arrCSS = XmlSearch( xmlData, "/workbook/classes/*[ @name and @value ]" ) /> <!--- Get the sheet nodes. ---> <cfset arrSheets = XmlSearch( xmlData, "/workbook/sheets/*" ) /> <!--- Loop over the sheet nodes. ---> <cfloop index="intSheet" from="1" to="#ArrayLen( arrSheets )#" step="1"> <!--- Get a short-hand pointer to the current sheet. ---> <cfset xmlSheet = arrSheets[ intSheet ] /> <!--- Grab the name text nodes from this sheet. ---> <cfset arrNames = XmlSearch( xmlSheet, "./name/text()" ) /> <!--- Grab the rows noes from this sheet. ---> <cfset arrRows = XmlSearch( xmlSheet, "./rows/row/" ) /> <!--- Check to see if we found a sheet name. ---> <cfif ArrayLen( arrNames )> <!--- We found a sheet name, so set the value. ---> <cfset strSheetName = arrNames[ 1 ].XmlValue /> <cfelse> <!--- No sheet name was found, so use default. ---> <cfset strSheetName = "Sheet1" /> </cfif> <!--- Create an Excel sheet in the current workbook with the given name. ---> <cfset objSheet = objWorkbook.CreateSheet( JavaCast( "string", strSheetName ) ) /> <!--- Loop over the row nodes. ---> <cfloop index="intRow" from="1" to="#ArrayLen( arrRows )#" step="1"> <!--- Get a short-hand pointer to the current row. ---> <cfset xmlRow = arrRows[ intRow ] /> <!--- Grab all the cells for this row. ---> <cfset arrCells = XmlSearch( xmlRow, "./cell/" ) /> <!--- Create the row in the current sheet. ---> <cfset objRow = objSheet.CreateRow( JavaCast( "int", (intRow - 1) ) ) /> <!--- Loop over the cell nodes. ---> <cfloop index="intCell" from="1" to="#ArrayLen( arrCells )#" step="1"> <!--- Get a short hand pointer to the cell. ---> <cfset xmlCell = arrCells[ intCell ] /> <!--- Create a cell in the current row. ---> <cfset objCell = objRow.CreateCell( JavaCast( "int", (intCell - 1) ) ) /> <!--- Set the cell value. Here is where would check any kind of explicit data types and formatting. However, for this proof of concept, we are only going to deal with the String data type. ---> <cfset objCell.SetCellValue( JavaCast( "string", Trim( xmlCell.XmlText ) ) ) /> </cfloop> </cfloop> </cfloop> <!--- ASSERT: At this point, we have populated our workbook with zero or more sheets of data. Now, we have to write the workbook to the file system. ---> <!--- Create a file output stream to which we will write the new workbook binary. ---> <cfset objFileOutputStream = CreateObject( "java", "java.io.FileOutputStream" ).Init( JavaCast( "string", ExpandPath( "./xml_to_excel.xls" ) ) ) /> <!--- Write the workout data to the file stream. ---> <cfset objWorkbook.Write( objFileOutputStream ) /> <!--- Close the file output stream. This will release any locks on the file and finalize the process. ---> <cfset objFileOutputStream.Close() />