我正在尝试从数据库动态构建word文档(我使用CFC进行查询处理)。我的努力很成功,但我只能复制英语文档。但是,我的应用程序使用的是希腊语。
当我尝试构建包含希腊字符的文档时,输出如下所示:??????????????????
。我尝试了很多东西,但没有任何工作。这里奇怪的是当我使用相同的CFC进行PDF复制时,希腊字符输出正确。
cfm文件:
<cfheader name="Content-Disposition" value="inline; filename=Save-Print.doc" charset="utf-8">
<cfcontent type="application/msword">
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Print</title>
<!--[if gte mso 9]>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:Compatibility>
<w:UseAsianBreakRules/>
</w:Compatibility>
<w:Zoom>100</w:Zoom>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
<![endif]-->
<!--[if supportFields]>
<div style="mso-element:header" id="lala">
<p class=MsoHeader><span style="color:red">
<![endif]-->
<style>
<!--
@page Section1
{
size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;
mso-header:url("http://localhost:8500/BookLedger_Final/resources/cfScripts/Header.html") h1;
mso-footer:url("http://localhost:8500/BookLedger_Final/resources/cfScripts/Header.html") f1;
}
div.Section1 {page:Section1;}
p.MsoHeader {
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #333;
}
p.MsoFooter {
}
-->
</style>
</head>
<body>
<cfif not IsDefined("URL.id")>
<cfelseif not IsDefined("URL.model")>
</cfif>
<!--- Get print details --->
<cfinvoke
component="Print"
method="myPrint"
returnvariable="getMember"
id="#URL.id#"
model="#URL.model#">
<div class=Section1>
<cfoutput query="getMember">
<b id="lala">#Title#</b>
<p style="color:##1c1c1c"><i>#Body#</i></p>
</cfoutput>
</div>
</body>
</html>
cfc文件:
<cfcomponent>
<cffunction name="myPrint" access="remote" returntype="query" output="no">
<cfargument name="id" default="0" required="false" type="numeric">
<cfargument name="model" default="" required="false" type="any">
<cfset model = #ARGUMENTS.model#>
<cfif model EQ "member">
<cfquery name="getMember" datasource="rental">
SELECT CONCAT(rental.members.firstname,' ',rental.members.lastname) AS 'Title',rental.members.biog AS 'Body'
FROM rental.members
WHERE rental.members.id = #ARGUMENTS.id#
AND rental.members.model = "#ARGUMENTS.model#"
</cfquery>
<!---<cfset queryToString = serializeJson(getMember)>
<cfset strippedQuery = REReplace(queryToString,'<[^>]*>','','all')>--->
<cfreturn getMember>
<cfelseif model EQ "new_member">
<cfquery name="getMember" datasource="rental">
SELECT CONCAT(rental.new_members.firstname,' ',rental.new_members.lastname) AS 'Title',rental.new_members.biog AS 'Body'
FROM rental.new_members
WHERE rental.new_members.id = #ARGUMENTS.id#
AND rental.new_members.model = "#ARGUMENTS.model#"
</cfquery>
<cfreturn getMember>
<cfelseif model EQ "book">
<cfquery name="getMember" datasource="rental">
SELECT rental.books.title AS 'Title',rental.books.description AS 'Body'
FROM rental.books
WHERE rental.books.id = #ARGUMENTS.id#
AND rental.books.model = "#ARGUMENTS.model#"
</cfquery>
<cfreturn getMember>
<cfelseif model EQ "journal">
<cfquery name="getMember" datasource="rental">
SELECT rental.journals.title AS 'Title',rental.journals.description AS 'Body'
FROM rental.journals
WHERE rental.journals.id = #ARGUMENTS.id#
AND rental.journals.model = "#ARGUMENTS.model#"
</cfquery>
<cfreturn getMember>
<cfelseif model EQ "cd">
<cfquery name="getMember" datasource="rental">
SELECT rental.cd.title AS 'Title',rental.cd.description AS 'Body'
FROM rental.cd
WHERE rental.cd.id = #ARGUMENTS.id#
AND rental.cd.model = "#ARGUMENTS.model#"
</cfquery>
<cfreturn getMember>
</cfif>
</cffunction>
</cfcomponent>
答案 0 :(得分:2)
当我尝试你的pastebin例子时,我也得到了胡言乱语。看起来您删除了指定utf-8的<meta>
标记。当我添加回来时,在MS Word中为我显示的字符正确。
<cfprocessingdirective pageencoding="utf-8">
<cfheader name="Content-Disposition" value="inline; filename=Save-Print.doc" charset="utf-8">
<cfcontent type="application/msword; charset=utf-8">
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Print</title>
...
答案 1 :(得分:0)
听起来非常像字符集问题。仔细阅读这篇文章http://www.joelonsoftware.com/articles/Unicode.html,它可以很好地解释一些事情。在这种情况下,你已经不遗余力地将所有内容设置为utf-8,这将使我从DB获得的内容不是unicode,而是在希腊代码页中。作为实验,您可以尝试将cfheader和标签中的字符集设置为ISO-8859-7或Windows-1253。这可能正确显示内容。您也可以在IE中取出并打开文档。有一个View-&gt;编码菜单,可让您更改IE用于渲染文档的编码,该文档可能显示实际使用的内容。
通过评论标签,我认为你让浏览器决定内容类型,它可能正在成为希腊字符集之一。您也可以尝试使用fiddler来捕获并检查来自CF的响应。如果希腊字符真正编码为utf-8,它们将占用超过1个字节,而如果它们在希腊代码页中,它们将只占用1个字节。在fiddler的响应选项卡中使用十六进制视图应该很快就能显示出来。
另一种选择可能是使用像Apache POI这样的东西来生成'真正的'单词.doc或.docx文件。这是一大堆工作,但我们在工作的地方使用它会产生良好的效果。