2010年9月10日 星期五

【轉貼】使用java 轉成excel 文件

【转帖】使用java类生成excel文件范例 http://www.mmug.cn/blogs/index.cfm?sPart=SB&iFid=203&sDayNow=2006-03-20&sAuthor=tianji


在台湾mmug的關於java的POI讀取excel文件的例子,發表后點擊率倍增,很高興有這麽多人關注我的帖子,今天在自己的blog中決定分享給大家一個關餘POI與資料庫交互,生成excel文件的例子,廢話少說,我們開始吧!

輸入簡體中文寫成csv檔,使用excel開啟顯示簡體

CFMX:
<cfcontent type="text/html; charset=UTF-8">
<cfif form.t_name neq "">
 <cfset csv_path="show/" >
 <cfset tmp_output ="浙江天台县今日通报发现天台县下路王村">

         <cfscript>
           writeFile = ("#csv_path#test_js3.csv");

           jFile = createobject("java", "java.io.File").init(writeFile);

           jStream = createobject("java", "java.io.FileOutputStream").init(jFile);

           jStream.write(239);

           jStream.write(187);

           jStream.write(191);

           jWriter = createobject("java", "java.io.OutputStreamWriter");

           jWriter.init(jStream, "UTF-8");

           jWriter.write(tmp_output);

           jWriter.flush();

           jWriter.close();

           jStream.close();

           </cfscript>

</cfif>
 =======================CF 5===================================

  <cftry>
   <cfset jStream=createobject("java","java.io.FileOutputStream")>
   <cfset jStream=jStream.init("show_u8.js")>
   <cfset jWriter=createobject("java","java.io.OutputStreamWriter")>
   <cfset jWriter=jWriter.init(jStream,"UTF-8")>
   <cfset a=jWriter.write(Chr(239))>
   <cfset a=jWriter.write(Chr(187))>
   <cfset a=jWriter.write(Chr(191))>
   <cfset a=jWriter.write(jstring)>
   <cfset a=jWriter.flush()>
   <cfset a=jWriter.close()>
   <cfset a=jStream.close()>
  <cfcatch></cfcatch>
  </cftry>

取query某欄位所有內容值存入list

<cfquery name="sel_page" datasource="#odbc#">
 select id,title  from show
</cfquery>
 
 <cfset title_list = #ValueList(sel_page.title)#>
 
<cfoutput> title_list = # title_list#</cfoutput>

網頁下載檔案

 <cfheader name="Content-disposition" value="attachment;filename=info_pps/#sel_file.pps_file#">
<cfhttp url="#expandPath('goods_A1.htm')#" method="GET" resolveurl="YES">
<cfcontent type="application/x-mspowerpoint" file = "#expandPath('info_pps/#sel_file.pps_file#')#">

匯出至excel 檔案 網頁header設定

MX:
<cfheader name="Content-Disposition" value="attachment; filename=OneMoreSpreadSheet.xls">
<cfcontent type="application/x-msexcel; charset=utf-16" reset="No">
 
5:
<CFCONTENT TYPE="application/vnd.ms-excel" >
 

XML子節點判斷是否有內容

<cfset nxml_content = xml>
<cfset nxml = XmlParse(nxml_content)>
<cfif IsDefined("nxml.ma.image")>
    <cfset n_image64 = Trim(nxml.ma.image.xmltext)>
    <cfset nxml_length = ArrayLen(nxml.ma.itemn)>
</cfif>

讀取xml

<!---使用java元件讀取xml --->
 <cfscript>
  xmlReaderPool = CreateObject("JAVA", "XMLReaderPool");
  poolXMLReader = XMLReaderPool.getInstance();
  xmlList = poolXMLReader.getXMLReader();
  xmlList.doParse(returnvalue);
  xmlRecordCount= xmlList.getAttributes("ROOT.RECORDCOUNT");
  pageRowCount = xmlList.getItemNums("ROOT.ITEM");
 </cfscript>