Balumohan

July 15, 2009

cfcontent in safari

Normally for file downloads in cf we use

<!—this is the path of the file—>
<cfset fullpath = #ExpandPath(“individualExcelReport”)#&”\”&”trialexport.xls”>

<!—adding the file name—>
<cfheader name=”content-disposition” value=”attachment;filename=”trialexport.xls””>

<!—adding the description—>
<cfheader name=”Content-Description” value=”This is a tab-delimited file.”>

<cfcontent type=”application/msexcel; charset=utf-8″ reset=”true” file=”#fullpath#” deletefile=”true”>

But this worked fine in Firefox and IE. But in SAFARI it something weired happened. It showed to download a file with the name of the page in which the code is written. But the file was having the data that I was expecting. Then how come the name change to the page name???

Finally… I must say FINALLY.. I got it.. It was because the filename is not being assigned properly.

Solution:

<!—this is the path of the file—>
<cfset fullpath = #ExpandPath(“individualExcelReport”)#&”\”&”trialexport.xls”>

<!—adding the file name—>
<cfheader name=”content-disposition” value=”attachment;filename=”"trialexport.xls”"”>

<!—adding the description—>

<cfheader name=”Content-Description” value=”This is a tab-delimited file.”>

<cfcontent type=”application/msexcel; charset=utf-8″ reset=”true” file=”#fullpath#” deletefile=”true”>

In here we are surrounding the filename with an extra (“”).

<cfheader name=”content-disposition” value=”attachment;filename=”"trialexport.xls”"”>

So next time dont forget that extra pair of (“) :)

June 30, 2009

Pray for me brother : balu

Filed under: Music — Tags: , , , — balsbalu @ 12:33 pm

My second attempt for recording :)

Click Link Below

Pray for me Brother : Balu

cfscript library

Filed under: Coldfusion — Tags: , , , , — balsbalu @ 12:16 pm

Using cfscript in Coldfusion is a good practise. Here are the examples for to use the cfscipt for looping, switch ..

Setting a normal variable

<cfscript>

myvariable = “balu”;

</cfscript>

Do / While Loop

<cfscript>

myvariable = 1;

do {

myvariable  = myvariable + 1;

WriteOutput(myvariable);

} while (myvariable LTE 0);

</cfscript>

While Loop

<cfscript>

myvariable = 0;
while (myvariable LT 10) {
        myvariable = myvariable + 1;
        WriteOutput(myvariable);
}
</cfscript>

Try / Catch Statements

<cfscript>

try {
    x = 10 / 0;
}
catch(Any e) {
    WriteOutput("Error occured " & e.message);
}
</cfscript>

Single Line Comments

<cfscript>

a = a + 10; //This is a single line comment
</cfscript>

Multi-Line Comments

<cfscript>

/* This is a multi line comment
 that can written in
        multiple lines
*/
</cfscript>

FOR Loop

<cfscript>

for (i=1;i LTE ArrayLen(array);i=i+1)
 {
        WriteOutput(array[i]);
}
</cfscript>

Switch Case

<cfscript>

switch(myvalue) {
    case "1":
         WriteOutput("1 is the value");
         break;
    case "2":
         WriteOutput("2 is the value");
         break;
    default:
         WriteOutput("default value");
}
</cfscript>

If… else….

<cfscript>

if (myvariable EQ 1) {
        WriteOutput("1 is the value");
}
else if (myvariable EQ 2) {
        WriteOutput("2 is the value");
}
else {
        WriteOutput("default value");
}
</cfscript>

June 24, 2009

My First Experiment

Filed under: Music — Tags: , , — balsbalu @ 8:49 am

This is my first experiment with sound.. Please do review :)

First Experiment

CFQuery in CFScript

Filed under: Coldfusion — Tags: , , — balsbalu @ 3:24 am

Lets have a look how we can use cfquery in cfscript

<cfquery name=”getstudents” datasource=”dsnstudents”>
select stuname,stuage,stuclass from students where stuid > 1
</cfquery>

In CFscript

<cfscript>

objFactory = CreateObject(”java”,”coldfusion.server.ServiceFactory”);
objDataService = objFactory.DataSourceService;
objDataSource = objDataService.GetDataSource(”dsnstudents”);
objConnection = objDataSource.GetConnection(Enter the username and password here);
objstatementstring = “select stuname,stuage,stuclass from students where stuid > 1″;
objStatement = objConnection.PrepareStatement(objstatementstring);
objResults = CreateObject(”java”,”coldfusion.sql.QueryTable”).Init( objStatement.ExecuteQuery() );
objConnection.Close();
</cfscript>

Excel File Creation in Coldfusion using POI

Filed under: Coldfusion — Tags: , , — balsbalu @ 3:22 am

Normally we create Excel files in coldfusion by the simple cfoutput with table structure and assigning it to a file with extension xls. But by this method we can find that sometimes the excel sheet is having special characters filled up. So here comes a old “NEW” method for the rescue. ie Excel File Creation using POI. Now this method is used as this is more efficient than the normal way of excel creation.

An example code for Excel File Creation

//creating the workbook using poi
<cfset workBook = createObject(”java”,”org.apache.poi.hssf.usermodel.HSSFWorkbook”).init()/>

//assigning style to the cells in the excel sheet
<cfset cellstyle = workbook.createCellStyle()>
//assigning font weight
<cfset fontface = workbook.createFont()>
<cfset fontface.setBoldweight(fontface.BOLDWEIGHT_BOLD)>
<cfset cellstyle.setFont(fontface)>

//this is the sheet counter for the excel work book. for our example it is 3.
<cfset sheetcounter = 0>

<cfloop from=”1″ to=”3″ index=”sheetcounter”>
<cfset newSheet = workBook.createSheet()/>
<cfset row = newSheet.createRow(0)/>

//this is the student list. We will create sheets for these students
<cfset studentlist = “balu,pinky,deepu”/>
<cfset subjectlist = “science,maths,english,computer”/>

<cfloop list = “#studentlist#” index=”student”>
<cfset workBook.setSheetName(#sheetcounter#, “#student#”)/>
//this is for creating headings for each sheet.. ie the subject names science,maths,english,computer
<cfset counter = 0>
<cfloop list=”#subjectlist#” index=”header”>
<cfset cell = row.createCell(counter)/>

<cfset cell.setCellStyle(cellstyle)/>
<cfset cell.setCellValue(#header#)/>
<cfset counter = counter+1>
</cfloop>

<cfset sheetcounter = sheetcounter+1>
</cfloop>
</cfloop>

//the resultset name is studentdetails. We are looping through the query in order to assign the values to the cells of the excel sheet.

<cfloop from = “1″ to = “#listlen(studentlist)#” index = “datacounter”>

<cfset currentsheet = workBook.getSheetAt((datacounter-1)>
<cfset row = currentsheet.createRow(1)/>
<cfset datasheetcounter = 1/>

//getting data from the table student

<cfquery name=”studentdetails” datasource=”studentdb”>

select * from studentdetails where studentname = ‘#datacounter#’
</cfquery>

<cfif studentdetails.recordcount>

//looping through the student details
<cfloop query=”studentdetails”>

<cfset row = currentsheet.createRow(datasheetcounter)/>

<cfset cell = row.createCell(0)/>
<cfset cell.setCellValue(studentdetails.science)/>

<cfset cell = row.createCell(1)/>
<cfset cell.setCellValue(studentdetails.maths)/>

<cfset cell = row.createCell(2)/>
<cfset cell.setCellValue(studentdetails.english)/>

<cfset cell = row.createCell(3)/>
<cfset cell.setCellValue(studentdetails.computer)/>

<cfset datasheetcounter = datasheetcounter + 1>

</cfloop>
</cfif>

</cfloop>

//this is the directory where we are going to save the excel file
<cfset directorypath = “d:\studentdetails”/>

<cfif NOT DirectoryExists(directorypath)>
//creating the directory if the directory is not there in the server
<cfdirectory action=”create” directory=”#directorypath#” >

</cfif>

<cfset fullpath= “d:\studentdetails\studentdetails.xls”/>

//if the file is there in the directory we are deleting that file
<cfif FileExists(fullpath)>
<cffile action=”delete” file=”#fullpath#”>
</cfif>

//writing the file
<cfset fileOutStream = createObject(”java”,”java.io.FileOutputStream”).init(#fullpath#)/>
<cfset workBook.write(fileOutStream)/>
<cfset fileOutStream.close()/>

Blog at WordPress.com.