【ColdFusion】读取指定Excel中的内容,并且上传到服务器指定目录
2010-08-18 17:12 Peter Jin 阅读(581) 评论(0) 编辑 收藏 举报读取Excel内容的自定义函数如下:
fnc_ReadExcelSheet.cfm
<cffunction name="ReadExcelSheet" access="public" output="false" returntype="query">
<cfargument name="filename" required="true" type="string" />
<cfargument name="sheetName" required="true" type="string" />
<cfscript>
var c = "";
var stmnt = "";
var rs = "";
var sql = "Select * from [#sheetName#$]";
var myQuery = "";
arguments.filename = expandPath(arguments.filename);
if(len(trim(arguments.filename)) and fileExists(arguments.filename))
{
try
{
CreateObject("java","java.lang.Class").forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = CreateObject("java","java.sql.DriverManager").getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" & arguments.filename );
stmnt = c.createStatement();
rs = stmnt.executeQuery(sql);
myQuery = CreateObject('java','coldfusion.sql.QueryTable').init(rs);
}
catch(any e)
{
// 异常
}
}
return myQuery;
</cfscript>
</cffunction>
<cfargument name="filename" required="true" type="string" />
<cfargument name="sheetName" required="true" type="string" />
<cfscript>
var c = "";
var stmnt = "";
var rs = "";
var sql = "Select * from [#sheetName#$]";
var myQuery = "";
arguments.filename = expandPath(arguments.filename);
if(len(trim(arguments.filename)) and fileExists(arguments.filename))
{
try
{
CreateObject("java","java.lang.Class").forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = CreateObject("java","java.sql.DriverManager").getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" & arguments.filename );
stmnt = c.createStatement();
rs = stmnt.executeQuery(sql);
myQuery = CreateObject('java','coldfusion.sql.QueryTable').init(rs);
}
catch(any e)
{
// 异常
}
}
return myQuery;
</cfscript>
</cffunction>
对上面自定义函数的具体应用如下:
index.cfm
<cfoutput>
<html>
<FORM ACTION="dsp_data.cfm"
ENCTYPE="multipart/form-data"
METHOD="Post">
File: <INPUT NAME="FileContents" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
</html>
</cfoutput>
<html>
<FORM ACTION="dsp_data.cfm"
ENCTYPE="multipart/form-data"
METHOD="Post">
File: <INPUT NAME="FileContents" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
</html>
</cfoutput>
dsp_data.cfm
<cfoutput>
<cfinclude template="fnc_ReadExcelSheet.cfm">
<CFFILE ACTION="Upload"
FILEFIELD="FileContents"
DESTINATION="D:\www\ReadExcel\files\ImportDefaultData.xls"
NAMECONFLICT="OVERWRITE">
<CFOUTPUT>
Upload File Success !<br>
源文件:#cffile.ClientDirectory##cffile.ClientFile#<br>
目标文件:#cffile.ServerFile#
</CFOUTPUT>
<cfset QueryExcel = #ReadExcelSheet("files\ImportDefaultData.xls","Sheet1")#>
<html>
<head><title>ReadExcelSheet</title></head>
<body>
<br/><br/>
ReadExcelSheet<br/><br/><hr/>
<table><tbody>
<!---一覧ヘッダー--->
<tr>
<th width="25">No</th>
<th width="200" nowrap>ソースパス</th>
<th width="180" nowrap>ファイル名</th>
<th width="100" nowrap>Line</th>
<th nowrap>日本語文字</th>
</tr>
<!---一覧ボディー--->
<cfloop index="loop" from="1" to="#QueryExcel.recordcount#">
<tr>
<td width="20" style="text-align:center">
#loop#
</td>
<td style="text-align:center">
#QueryExcel.ソースパス[loop]#
</td>
<td style="text-align:center">
#QueryExcel.ファイル名[loop]#
</td>
<td style="text-align:center">
#QueryExcel.Line[loop]#
</td>
<td nowrap>
#QueryExcel.日本語文字[loop]#
</td>
</tr>
</cfloop>
</tbody></table>
</body>
</html>
</cfoutput>
<cfinclude template="fnc_ReadExcelSheet.cfm">
<CFFILE ACTION="Upload"
FILEFIELD="FileContents"
DESTINATION="D:\www\ReadExcel\files\ImportDefaultData.xls"
NAMECONFLICT="OVERWRITE">
<CFOUTPUT>
Upload File Success !<br>
源文件:#cffile.ClientDirectory##cffile.ClientFile#<br>
目标文件:#cffile.ServerFile#
</CFOUTPUT>
<cfset QueryExcel = #ReadExcelSheet("files\ImportDefaultData.xls","Sheet1")#>
<html>
<head><title>ReadExcelSheet</title></head>
<body>
<br/><br/>
ReadExcelSheet<br/><br/><hr/>
<table><tbody>
<!---一覧ヘッダー--->
<tr>
<th width="25">No</th>
<th width="200" nowrap>ソースパス</th>
<th width="180" nowrap>ファイル名</th>
<th width="100" nowrap>Line</th>
<th nowrap>日本語文字</th>
</tr>
<!---一覧ボディー--->
<cfloop index="loop" from="1" to="#QueryExcel.recordcount#">
<tr>
<td width="20" style="text-align:center">
#loop#
</td>
<td style="text-align:center">
#QueryExcel.ソースパス[loop]#
</td>
<td style="text-align:center">
#QueryExcel.ファイル名[loop]#
</td>
<td style="text-align:center">
#QueryExcel.Line[loop]#
</td>
<td nowrap>
#QueryExcel.日本語文字[loop]#
</td>
</tr>
</cfloop>
</tbody></table>
</body>
</html>
</cfoutput>