2012-01-04 68 views
1

我從ColdFusion 9導出到Excel,我想設置頁面方向和縮放比例,以便導出的Excel文檔適合頁面和打印景觀。如何做到這一點?從ColdFusion導出到Excel,如何修改頁面設置?

編輯方案:
感謝您的協助。頁面方向設置與廣告一樣工作。
我用下面的hack來使它適合頁面寬度。

此頁面包含了可能的各種設置文檔:
http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx

<cfheader name="Content-disposition" value="attachment;filename=export.xls"> 
<cfcontent type="application/application/vnd.ms-excel"> 

<!--- 
mso-page-orientation:landscape causes the exported excel spreadsheet to be printed landscape. 
Setting Scale=45 causes the printout to fit to page width for me. 
Per the documentation, I should be able to set 
<x:Print><x:FitWidth>1</x:FitWidth><x:FitHeight>32767</x:FitHeight><x:ValidPrinterInfo/></x:Print> 
but it doesn't want to work. 
The width and height appear correctly in the Page Setup dialog, but the 'Adjust to' scale size 
radio button remains selected instead of the 'Fit to' one page wide by 32767 tall radio button. 
---> 

<HTML xmlns:x="urn:schemas-microsoft-com:office:excel"> 
<HEAD> 
<STYLE> 
    <!--table 
    @page {mso-page-orientation:landscape;} 
    --> 
</STYLE> 
    <!--[if gte mso 9]><xml> 
    <x:ExcelWorkbook> 
    <x:ExcelWorksheets> 
    <x:ExcelWorksheet> 
     <x:WorksheetOptions> 
     <x:Print> 
     <x:ValidPrinterInfo/> 
     <x:Scale>45</x:Scale> 
     </x:Print> 
     </x:WorksheetOptions> 
    </x:ExcelWorksheet> 
    </x:ExcelWorksheets> 
    </x:ExcelWorkbook> 
    </xml><![endif]--> 
</HEAD> 
<BODY> 

<cfoutput> 
    <cfloop from = "1" to = "#arrayLen(reportItems)#" index = "i"> 
    <table cellpadding="1" cellspacing="1" bgcolor="dcdcdc" width="100%" border="1"> 
     ... table contents ... 
    </table> 
    </cfloop> 
</cfoutput> 

</BODY> 
</HTML> 
+0

你能否提供一個代碼示例,顯示你到目前爲止? – Micah 2012-01-04 21:16:40

回答

1

您可以使用記錄此頁上一招:

How to format an Excel workbook while streaming MIME content

基本上,數據你輸出一個標準的HTML表格,你會像打開Excel電子表格一樣。您還必須爲Excel指定mime類型,爲了更好的衡量,我還想指定content-disposition標頭以提示更好的下載文件名。

<cfcontent type="application/msexcel"/> 
<cfheader name="content-disposition" value="attachment; filename=myFile.xls"> 

關鍵看你的特定格式的問題,那麼,也是在上面的鏈接找到。您需要在<style>塊中包含MS Office特定的CSS規則mso-page-orientation:landscape;。從該鏈接:

<style> 
    <!--table 
    @page 
    {mso-header-data:"&CMultiplication Table\000ADate\: &D\000APage &P"; 
    mso-page-orientation:landscape;} 
    br 
    {mso-data-placement:same-cell;} 

    --> 
</style> 

這應該處理頁面方向問題。有一點需要注意 - Office 2007和更新版本會在打開此文件時警告用戶有關不同內容類型的信息。這只是一個煩惱(可以通過註冊表更新來禁用);一切仍然會按需要運作和運作。

0

我看不出有任何屬性的cfspreadsheet標籤來做到這一點。從Adobe網站:

<cfspreadsheet 
    action="write" 
    filename = "filepath" 
    format = "csv" 
    name = "text" 
    overwrite = "true | false" 
    password = "password" 
    query = "queryname" 
    sheetname = "text" 
> 

您可能有使用打印範圍或類似的東西在Excel導出後做。

讓CF管理導出正確的數據,讓Excel處理格式化/打印。

3

IIRC沒有什麼可烤的。但你可以挖掘底層的工作簿並使用一點魔法。 (請注意,這些打印設置每張應用)

<!--- get the underlying poi sheet ---> 
<cfset poiSheet = cfSheetObject.getWorkBook().getSheet("TheSheetName")> 
<cfset ps = poiSheet.getPrintSetup()> 
<cfset ps.setLandscape(true)> 
<!--- fit to one page ---> 
<cfset ps.setFitHeight(1)> 
<cfset ps.setFitWidth(1)> 
+0

沒關係;)我從你的更新中看到你實際上正在生成一個人造的excel文件(即html)。所以這種方法是行不通的。 – Leigh 2012-01-05 16:15:37