|
<cfobject type="java" class="java.awt.Color" name="Color" action="CREATE">
<cfobject type="java" class="EasyXLS.Constants.Styles" name="Styles" action="CREATE">
<cfobject type="java" class="EasyXLS.Constants.Alignment" name="Alignment" action="CREATE">
Tutorial 02<br>
----------<br>
<cfobject type="java" class="EasyXLS.ExcelDocument" name="workbook" action="CREATE">
<cfquery name="myQuery" datasource="northwind">
SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + '/' + CAST(Day(ord.OrderDate) AS varchar) +
'/' + CAST(year(ord.OrderDate) AS varchar) AS 'Order_Date',
P.ProductName AS 'Product_Name', O.UnitPrice AS Price,
' ' + CAST(O.Quantity AS varchar) AS Quantity,
O.UnitPrice * O. Quantity AS Value
FROM Orders AS ord, [Order Details] AS O, Products AS P
WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID
</cfquery>
<cfobject type="java" class="EasyXLS.Util.List" name="lstRows" action="CREATE">
<cfobject type="java" class="EasyXLS.Util.List" name="lstHeaderRow" action="CREATE">
<cfset lstHeaderRow.addElement("Order Date")>
<cfset lstHeaderRow.addElement("Product Name")>
<cfset lstHeaderRow.addElement("Price")>
<cfset lstHeaderRow.addElement("Quantity")>
<cfset lstHeaderRow.addElement("Value")>
<cfset lstRows.addElement(lstHeaderRow)>
<cfloop query="myQuery">
<cfobject type="java" class="EasyXLS.Util.List" name="RowList" action="CREATE">
<cfset RowList.addElement(#Order_Date#)>
<cfset RowList.addElement(#Product_Name#)>
<cfset RowList.addElement(#Price#)>
<cfset RowList.addElement(#Quantity#)>
<cfset RowList.addElement(#Value#)>
<cfset lstRows.addElement(RowList)>
</cfloop>
<cfobject type="java" class="EasyXLS.ExcelAutoFormat" name="xlsAutoFormat" action="CREATE">
<cfobject type="java" class="EasyXLS.ExcelStyle" name="xlsHeaderStyle" action="CREATE">
<cfobject type="java" class="java.awt.Color" name="lightGreen" action="CREATE">
<cfset lightGreen.init(JavaCast("int", "144"), JavaCast("int", "238"), JavaCast("int", "144"))>
<cfset xlsHeaderStyle.setBackground(lightGreen)>
<cfset xlsHeaderStyle.setFontSize(12)>
<cfset xlsAutoFormat.setHeaderRowStyle(xlsHeaderStyle)>
<cfobject type="java" class="EasyXLS.ExcelStyle" name="xlsEvenRowStripesStyle" action="CREATE">
<cfobject type="java" class="java.awt.Color" name="FloralWhite" action="CREATE">
<cfset FloralWhite.init(JavaCast("int", "255"), JavaCast("int", "250"), JavaCast("int", "240"))>
<cfset xlsEvenRowStripesStyle.setBackground(FloralWhite)>
<cfset xlsEvenRowStripesStyle.setFormat("$0.00")>
<cfset xlsEvenRowStripesStyle.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT)>
<cfset xlsAutoFormat.setEvenRowStripesStyle(xlsEvenRowStripesStyle)>
<cfobject type="java" class="EasyXLS.ExcelStyle" name="xlsOddRowStripesStyle" action="CREATE">
<cfobject type="java" class="java.awt.Color" name="OddRowStripesColor" action="CREATE">
<cfset OddRowStripesColor.init(JavaCast("int", "240"), JavaCast("int", "247"), JavaCast("int", "239"))>
<cfset xlsOddRowStripesStyle.setBackground(OddRowStripesColor)>
<cfset xlsOddRowStripesStyle.setFormat("$0.00")>
<cfset xlsOddRowStripesStyle.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT)>
<cfset xlsAutoFormat.setOddRowStripesStyle(xlsOddRowStripesStyle)>
<cfobject type="java" class="EasyXLS.ExcelStyle" name="xlsLeftColumnStyle" action="CREATE">
<cfobject type="java" class="java.awt.Color" name="FloralWhite" action="CREATE">
<cfset FloralWhite.init(JavaCast("int", "255"), JavaCast("int", "250"), JavaCast("int", "240"))>
<cfset xlsLeftColumnStyle.setBackground(FloralWhite)>
<cfset xlsLeftColumnStyle.setFormat("mm/dd/yyyy")>
<cfset xlsLeftColumnStyle.setHorizontalAlignment(Alignment.ALIGNMENT_LEFT)>
<cfset xlsAutoFormat.setLeftColumnStyle(xlsLeftColumnStyle)>
Writing file C:\Samples\Tutorial02 - export List to Excel with formatting.xlsx<br>
<cfset ret=workbook.easy_WriteXLSXFile_FromList(
"C:\Samples\Tutorial02 - export List to Excel with formatting.xlsx", lstRows, xlsAutoFormat, "Sheet1")>
<cfset sError=workbook.easy_getError()>
<cfif (sError is "")>
<cfoutput>
File successfully created.
</cfoutput>
<cfelse>
<cfoutput>
Error encountered: #sError#
</cfoutput>
</cfif>
<cfset workbook.Dispose()>
|