如何在导出到 excel 之前触发更改 html table 的 Javascript 函数?
How to trigger a Javascript function that changes a html table before exporting to excel?
我有一个 html table 正在导出到 excel 文件,我可以创建、加载和导出该文件
转换为 .xls 文件格式。
当我完成向 table 添加值后
各种 SQL 语句,我调用一个 javascript 函数来检查 table 是否为 null
值,如果 table 的 'td'(分别)有空值,它会在 'td'.
中输入 0
现在的问题是,当我在另一个页面上单击导出到 excel 后调用 window.onload 上的函数时,就像它只是加载 table 而没有 运行 脚本和
excel 文件中仍有空值。
但是如果我取出将信息写入 excel 文件的代码并将其显示为普通 html 页面,它会调用该函数并将空值设置为 0。
所以我认为这个功能被完全遗漏了,我不知道如何包括
它在我写入 excel 文件的部分。
下面是我的 javascript 代码示例和我用来写入 excel 文件的代码:
<script>
window.onload = function change() {
var count='0';
var TDs=document.getElementsByTagName('td')
var length=TDs.length;
i='0';
while(i<length){
if(TDs[i].innerHTML==''){
count++;
TDs[i].innerHTML = "0";
}
i++;
}
}
</script>
<%
Response.Clear
Response.Buffer = true
Response.ContentType = "application/vnd.ms-excel"
if ModuleName = "*ALL*" then
Response.AddHeader "Content-disposition", "attachment;filename=" & CourseName & ".xls"
else
Response.AddHeader "Content-disposition", "attachment;filename=" & ModuleName & ".xls"
end if
Response.Charset = ""
!!!the code for the creating of the tables is in between here!!!
Response.End
%>
这里是我放置 -1 和 0 的 table。
*注意,这是在 4 个 while 循环和其他 3 个 tables 之间。
<tr>
<td width="50%"><font face="Arial" size="1"><%=rsUserAnswers("FileText")%></td>
<td width="50%"><font face="Arial" size="1"><%=rsUserAnswers("VoiceFile")%></td>
<%if rsUserModules1("LinkType") = "Submit Answer Multi" then
readyList1 = Split(rsUserModules1("LinkAction"),",")
%>
<td width="50%" style="font-family: Arial; font-size: x-small;">
<%For i = 0 TO UBound(readyList1) %>
<%if InStr(readyList1(i),"A")>0 then%>
<%output=rsUserAnswers("Score1")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"B")>0 then%>
<%output=rsUserAnswers("Score2")
if output<> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"C")>0 then%>
<%output=rsUserAnswers("Score3")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"D")>0 then%>
<%output=rsUserAnswers("Score4")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"E")>0 then%>
<%output=rsUserAnswers("Score5")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"F")>0 then%>
<%output=rsUserAnswers("Score6")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"G")>0 then%>
<%output=rsUserAnswers("Score7")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"H")>0 then%>
<%output=rsUserAnswers("Score8")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%next%>
</td>
<%else%>
<td width="50%"><font face="Arial" size="1"><%=rsUserAnswers("Score")%></td>
<%end if%>
</tr>
rsUserAnswer(Score1,2,3,4,5,6,7,8) 是在 SQL 中的 case 语句中创建的单独列。
我从上面得到的结果table是这样的:
AnswerText VoiceFile Score
right none -1
wrong none 'blank'
wrong none 'blank'
right none -1
但这不正确我需要这样的:
AnswerText VoiceFile Score
right none -1
wrong none 0
wrong none 0
right none -1
但是你的(@Shadow Wizard)只给了我最后一个条目:
AnswerText VoiceFile Score
right none 0 <--- needs to be -1
wrong none 0
wrong none 0
right none -1
你不能。
您正在向浏览器发送您(错误地)声称是 Excel 文档的内容。
浏览器会看到您正在向它发送 Excel 文档并将其传递给 Excel。
Excel 将打开它,发现它 不是 一个 Excel 文件但识别出它是一个 HTML 文件并尝试将其转换为 Excel 文档。
Excel 不会执行嵌入在 HTML.
中的 JavaScript
如果要修改 HTML,请使用服务器端代码进行修改。
这可能比你想象的要简单。
只有三种情况 table 单元格可能最终为空,因此在这三种情况下检查此服务器端并在这种情况下分配“0”。
为此,首先要有这样的功能:
<%
Function DefaultWhenEmpty(sValue, sDefaultValue)
If IsNull(sValue) Or sValue="" Then
DefaultWhenEmpty = sDefaultValue
Else
DefaultWhenEmpty = sValue
End If
End Function
%>
然后更改代码的那些部分以使用函数:
<td width="50%"><font face="Arial" size="1"><%=DefaultWhenEmpty(rsUserAnswers("FileText"), "0")%></td>
<td width="50%"><font face="Arial" size="1"><%=DefaultWhenEmpty(rsUserAnswers("VoiceFile"), "0")%></td>
...
<td width="50%"><font face="Arial" size="1"><%=DefaultWhenEmpty(rsUserAnswers("Score"), "0"%></td>
这会导致单元格显示为“0”,而不仅仅是空白。
我的问题解决了!感谢暗影精灵
我所做的是,不是添加代码以使用响应将 table 写入 excel,而是使用 javascript 本身将 table 发送过来至 excel。
我最初的想法是首先加载和呈现 HTML table,然后在完成后给用户一个按钮 select 以导出(因此 javascript 中提供的功能下面的link)。
因此,如果您有 jquery 或 javascript 将您的 table 导出到 excel,并且您的 [=33= 的单元格中有空值或 null 值] 或嵌套 tables 你可以添加这个 javascript 使用 body onload 标签来执行添加零的函数或者 Shadow Wizard 给我的函数默认为你设置的值,如果它是 null 或空.
<script language="JavaScript">
function change() {
var count='0';
var TDs=document.getElementsByTagName('td')
var length=TDs.length;
i='0';
while(i<length){
if(TDs[i].innerHTML==''){
count++;
TDs[i].innerHTML = "0";
}
i++;
}
}
</script>
以及使用 javascript 导出到 excel 的示例,然后按照此 link.
我有一个 html table 正在导出到 excel 文件,我可以创建、加载和导出该文件 转换为 .xls 文件格式。
当我完成向 table 添加值后 各种 SQL 语句,我调用一个 javascript 函数来检查 table 是否为 null 值,如果 table 的 'td'(分别)有空值,它会在 'td'.
中输入 0现在的问题是,当我在另一个页面上单击导出到 excel 后调用 window.onload 上的函数时,就像它只是加载 table 而没有 运行 脚本和 excel 文件中仍有空值。
但是如果我取出将信息写入 excel 文件的代码并将其显示为普通 html 页面,它会调用该函数并将空值设置为 0。
所以我认为这个功能被完全遗漏了,我不知道如何包括 它在我写入 excel 文件的部分。
下面是我的 javascript 代码示例和我用来写入 excel 文件的代码:
<script>
window.onload = function change() {
var count='0';
var TDs=document.getElementsByTagName('td')
var length=TDs.length;
i='0';
while(i<length){
if(TDs[i].innerHTML==''){
count++;
TDs[i].innerHTML = "0";
}
i++;
}
}
</script>
<%
Response.Clear
Response.Buffer = true
Response.ContentType = "application/vnd.ms-excel"
if ModuleName = "*ALL*" then
Response.AddHeader "Content-disposition", "attachment;filename=" & CourseName & ".xls"
else
Response.AddHeader "Content-disposition", "attachment;filename=" & ModuleName & ".xls"
end if
Response.Charset = ""
!!!the code for the creating of the tables is in between here!!!
Response.End
%>
这里是我放置 -1 和 0 的 table。 *注意,这是在 4 个 while 循环和其他 3 个 tables 之间。
<tr>
<td width="50%"><font face="Arial" size="1"><%=rsUserAnswers("FileText")%></td>
<td width="50%"><font face="Arial" size="1"><%=rsUserAnswers("VoiceFile")%></td>
<%if rsUserModules1("LinkType") = "Submit Answer Multi" then
readyList1 = Split(rsUserModules1("LinkAction"),",")
%>
<td width="50%" style="font-family: Arial; font-size: x-small;">
<%For i = 0 TO UBound(readyList1) %>
<%if InStr(readyList1(i),"A")>0 then%>
<%output=rsUserAnswers("Score1")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"B")>0 then%>
<%output=rsUserAnswers("Score2")
if output<> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"C")>0 then%>
<%output=rsUserAnswers("Score3")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"D")>0 then%>
<%output=rsUserAnswers("Score4")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"E")>0 then%>
<%output=rsUserAnswers("Score5")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"F")>0 then%>
<%output=rsUserAnswers("Score6")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"G")>0 then%>
<%output=rsUserAnswers("Score7")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%if InStr(readyList1(i),"H")>0 then%>
<%output=rsUserAnswers("Score8")
if output <> "-1" then %>
0
<%else%>
<%=output%>
<%end if%>
<%end if%>
<%next%>
</td>
<%else%>
<td width="50%"><font face="Arial" size="1"><%=rsUserAnswers("Score")%></td>
<%end if%>
</tr>
rsUserAnswer(Score1,2,3,4,5,6,7,8) 是在 SQL 中的 case 语句中创建的单独列。
我从上面得到的结果table是这样的:
AnswerText VoiceFile Score
right none -1
wrong none 'blank'
wrong none 'blank'
right none -1
但这不正确我需要这样的:
AnswerText VoiceFile Score
right none -1
wrong none 0
wrong none 0
right none -1
但是你的(@Shadow Wizard)只给了我最后一个条目:
AnswerText VoiceFile Score
right none 0 <--- needs to be -1
wrong none 0
wrong none 0
right none -1
你不能。
您正在向浏览器发送您(错误地)声称是 Excel 文档的内容。
浏览器会看到您正在向它发送 Excel 文档并将其传递给 Excel。
Excel 将打开它,发现它 不是 一个 Excel 文件但识别出它是一个 HTML 文件并尝试将其转换为 Excel 文档。
Excel 不会执行嵌入在 HTML.
中的 JavaScript如果要修改 HTML,请使用服务器端代码进行修改。
这可能比你想象的要简单。
只有三种情况 table 单元格可能最终为空,因此在这三种情况下检查此服务器端并在这种情况下分配“0”。
为此,首先要有这样的功能:
<%
Function DefaultWhenEmpty(sValue, sDefaultValue)
If IsNull(sValue) Or sValue="" Then
DefaultWhenEmpty = sDefaultValue
Else
DefaultWhenEmpty = sValue
End If
End Function
%>
然后更改代码的那些部分以使用函数:
<td width="50%"><font face="Arial" size="1"><%=DefaultWhenEmpty(rsUserAnswers("FileText"), "0")%></td>
<td width="50%"><font face="Arial" size="1"><%=DefaultWhenEmpty(rsUserAnswers("VoiceFile"), "0")%></td>
...
<td width="50%"><font face="Arial" size="1"><%=DefaultWhenEmpty(rsUserAnswers("Score"), "0"%></td>
这会导致单元格显示为“0”,而不仅仅是空白。
我的问题解决了!感谢暗影精灵
我所做的是,不是添加代码以使用响应将 table 写入 excel,而是使用 javascript 本身将 table 发送过来至 excel。 我最初的想法是首先加载和呈现 HTML table,然后在完成后给用户一个按钮 select 以导出(因此 javascript 中提供的功能下面的link)。
因此,如果您有 jquery 或 javascript 将您的 table 导出到 excel,并且您的 [=33= 的单元格中有空值或 null 值] 或嵌套 tables 你可以添加这个 javascript 使用 body onload 标签来执行添加零的函数或者 Shadow Wizard 给我的函数默认为你设置的值,如果它是 null 或空.
<script language="JavaScript">
function change() {
var count='0';
var TDs=document.getElementsByTagName('td')
var length=TDs.length;
i='0';
while(i<length){
if(TDs[i].innerHTML==''){
count++;
TDs[i].innerHTML = "0";
}
i++;
}
}
</script>
以及使用 javascript 导出到 excel 的示例,然后按照此 link.