我正在读csv文件,将其传递给Webpivot表,但它显示错误。 如果我正在尝试使用其他csv文件,它会告诉我 参数列表后面的SyntaxError:missing)。
ReferenceError: Month is not defined var csvData = Month,Expenses,Income
这是我的csv文件
Month,Expenses,Income
January,339,446
February,52,571
$(document).ready(require([ "wpt/WebPivotTable", "dojo/domReady!"],function(WebPivotTable) {
<%
String orname = "c:/d.csv";
File file = new File(orname);
FileReader fin = new FileReader(file); //Read file one by one
BufferedReader bi = new BufferedReader(fin);
int res;
StringBuilder sb = new StringBuilder();
while ((res = bi.read()) != -1) {
sb.append((char) res);
//csv = csv + ((char) res); //Converted int to char and stored in csv
}
String csv = sb.toString();
%>
var WebPivotTable = new WebPivotTable({
customOptions : {
uiFlags : {
menuBtn : 0,
dataSourceBtn : 0
},
}
}, "wpt-container");
var csvData =<%=csv%>;
WebPivotTable.setCsvRawData(csvData);
}));
这是我的其他Csv文件
Name,Party,Province,Age,Gender
"Mourani, Maria",BlocQuebecois,Quebec,43,Female
"Sellah, Djaouida",NDP,Quebec,30,Female
"St-Denis, Lise",NDP,Quebec,72,Female
"Fry, Hedy",Liberal,BritishColumbia,71,Female
"Turmel, Nycole",NDP,Quebec,70,Female
"Sgro, Judy",Liberal,Ontario,68,Female
"Raynault, Francine",NDP,Quebec,67,Female
"Davidson, Patricia",Conservative,Ontario,66,Female
答案 0 :(得分:1)
您需要将csvData
中的文本转义为正确的javascript字符串。
请注意,您正在阅读csv文件并按原样打印到页面中。 adeneo建议输入的内容如下:
var csvData = "Month,Expenses,Income
January,339,446
February,52,571";
这是无效的javascript。
我建议像:
StringBuilder sb = new StringBuilder();
String line;
boolean first = true;
while ((line = bi.readLine()) != null) {
if (!first) {
sb.append("\n\t+ "); // to append the lines in javascript
}
sb.append("'"); // string quote in js
sb.append(line.trim()); // trim to remove the original trailing newline
sb.append("\\n'"); // output newline in js and string quote
if (first) {
first = false; // only once to keep track of the + symbols
}
}
sb.append(";\n"); // terminate the string in javascript
// ... further down
var csvData =<%=sb.toString()%>;
这应输出如下内容:
var csvData = 'Month,Expenses,Income'
+ 'January,339,446'
+ 'February,52,571';
这显然非常容易出错,因为你的csv文件中可能有任何类型的字符可能会破坏你的javascript字符串,但是对于这个例子它应该有效。此外,如果您有一个非常大的CSV,您的页面将需要一段时间才能加载,直到整个CSV渲染 - 在幕后 - 在您的代码中。