从 ColdFusion 导出到 Excel,如何修改页面设置?

发布于 2024-12-25 01:59:52 字数 2041 浏览 3 评论 0原文

我正在从 ColdFusion 9 导出到 Excel,并且想要设置页面方向和缩放比例,以便导出的 Excel 文档适合页面并横向打印。如何做到这一点?

使用解决方案进行编辑:
感谢您的帮助。页面方向设置如宣传的那样工作。
我使用以下技巧来使其适合页面宽度。

此页面包含有关各种可能设置的文档:
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>

I'm exporting from ColdFusion 9 to Excel and I want to set the page orientation and scaling so the exported excel document fits to page and prints landscape. How to accomplish this?

Edit with solution:
Thanks for the assistance. The page-orientation setting worked as advertised.
I used the following hack to get it to fit to page width.

This page contains documentation on the various settings that are possible:
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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稳稳的幸福 2025-01-01 01:59:52

IIRC 没有任何内容。但是您可以进入底层工作簿并使用一点 POI 的魔力。 (请注意,这些打印设置适用于每张纸)

<!--- 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)>

IIRC there is nothing baked in. But you can tap into the underlying workbook and use a little POI magic. (Note, these print settings are applied per sheet)

<!--- 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)>
醉生梦死 2025-01-01 01:59:52

您可以使用本页上记录的技巧:

如何在流式传输 MIME 内容时格式化 Excel 工作簿

基本上,您输出一个标准的 HTML 数据表,您希望将其作为 Excel 电子表格打开。您还必须指定 Excel 的 mime 类型,并且为了更好地衡量,我还喜欢指定内容处置标头以提示提供更好的下载文件名。

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

那么,您的特定格式问题的关键也可以在上面的链接中找到。您需要包含一个带有 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 及更高版本将在打开此文件时警告用户有关不同内容类型的信息。这只是一个烦恼(可以通过注册表更新来禁用);一切仍将按需要工作和运行。

You can use a trick documented on this page:

How to format an Excel workbook while streaming MIME content

Basically, you output a standard HTML table of data that you would like opened as an Excel spreadsheet. You also have to specify the mime-type for Excel, and for good measure I also like to specify the content-disposition header to prompt for a better download file name.

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

The key to your specific formatting question, then, is also found in that above link. You need to include a <style> block with the MS Office-specific CSS rule mso-page-orientation:landscape;. From that link:

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

  -->
</style>

This should handle the page orientation question. One thing to note - Office 2007 and newer will warn users about differing content types when opening this file. It's simply an annoyance (that can be disabled with a registry update); everything will still work and function as needed.

酷炫老祖宗 2025-01-01 01:59:52

我在 cfspreadsheet 标记中没有看到任何属性可以完成此操作。来自 adobe 站点:

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

在 Excel 中导出后,您可能需要使用打印范围或类似的东西来执行此操作。

让 CF 管理导出正确的数据,让 Excel 处理格式化/打印。

I don't see any attributes to accomplish this in the cfspreadsheet tag. From adobe site:

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

You may have to do that after the export in Excel using print ranges or something similar.

Let CF manage exporting the correct data, let Excel handle the formatting/printing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文