Java 读取excel文件的内容
我正在寻找一些帮助,从excel文件中读取内容。 这是代码,我希望它从.csv文件中读取,然后 生成相同的结果 我的错误显示Java 读取excel文件的内容,java,csv,Java,Csv,我正在寻找一些帮助,从excel文件中读取内容。 这是代码,我希望它从.csv文件中读取,然后 生成相同的结果 我的错误显示 java.lang.NumberFormatException: For input string: "companycode" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDoubl
java.lang.NumberFormatException: For input string: "companycode"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at testgbt.TestGBT.main(TestGBT.java:40)
请提前帮助和感谢
package testgbt;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Vector;
import java.io.FileOutputStream;
import testgbt.BoostingTree;
import testgbt.BoostingTree.ResultFunction;
public class TestGBT {
public static void main(String[] args) {
String data_name = "C:\\Mihir\\NetBeans\\TestGBT\\data.csv";
Vector<Vector<Double>> x = new Vector<Vector<Double>>();
Vector<Double> y = new Vector<Double>();
try {
FileInputStream fis = new FileInputStream("C:\\Mihir\\NetBeans\\TestGBT\\data.csv");
// -------- load data from the file -------------
// get object of data input stream
BufferedReader b_reader = new BufferedReader(new
InputStreamReader(new FileInputStream(data_name))); // buffer
String read_line;
while ((read_line = b_reader.readLine()) != null) {
String[] str_list = read_line.trim().split(",");
Vector<Double> buffer_x = new Vector<Double>();
buffer_x.add(Double.valueOf(str_list[0]));
x.add(buffer_x);
y.add(Double.valueOf(str_list[1]));
}
b_reader.close();
// --------- learn a function of y=f(x) -------
BoostingTree gbt_ranker = new BoostingTree();
ResultFunction res_fun = gbt_ranker.gradient_boosting_tree(x, y);
// --------- save the curve fitting result y=f(x) ------
FileWriter file_writer = new FileWriter("C:\\Mihir\\NetBeans\\TestGBT\\result.txt");
FileOutputStream fos=new FileOutputStream("C:\\Mihir\\NetBeans\\TestGBT\\result.csv");
for (int i = 0; i < x.size(); i ++) {
file_writer.append(String.format("%f,%f,%f\n",
x.get(i).get(0),
res_fun.get_value(x.get(i)),
y.get(i)));
}
file_writer.close();
} catch (Exception err) {
err.printStackTrace();
System.exit(0);
}
}
}
包测试GBT;
导入java.io.BufferedReader;
导入java.io.FileInputStream;
导入java.io.FileWriter;
导入java.io.InputStreamReader;
导入java.util.Vector;
导入java.io.FileOutputStream;
导入testgbt.BoostingTree;
导入testgbt.BoostingTree.ResultFunction;
公共类TestGBT{
公共静态void main(字符串[]args){
字符串数据\u name=“C:\\Mihir\\NetBeans\\TestGBT\\data.csv”;
向量x=新向量();
向量y=新向量();
试一试{
FileInputStream fis=新的FileInputStream(“C:\\Mihir\\NetBeans\\TestGBT\\data.csv”);
//-----从文件中加载数据-------------
//获取数据输入流的对象
BufferedReader b_reader=新的BufferedReader(新的
InputStreamReader(新文件InputStream(数据名称));//缓冲区
字符串读取线;
while((read\u line=b\u reader.readLine())!=null){
String[]str_list=read_line.trim().split(“,”);
向量缓冲区_x=新向量();
buffer_x.add(Double.valueOf(str_list[0]);
x、 添加(缓冲区x);
y、 添加(Double.valueOf(str_list[1]);
}
b_reader.close();
//------学习y=f(x)的函数-------
BoostingTree gbt_ranker=新BoostingTree();
结果函数res_fun=gbt_ranker.梯度树(x,y);
//------保存曲线拟合结果y=f(x)------
FileWriter file\u writer=newfilewriter(“C:\\Mihir\\NetBeans\\TestGBT\\result.txt”);
FileOutputStream fos=新的FileOutputStream(“C:\\Mihir\\NetBeans\\TestGBT\\result.csv”);
对于(int i=0;i
您的输入CSV是否有标题行?从输入数据中删除标题行或更改代码以跳过第一行
通过更改此行,您可以轻松跳过第一行:
String read_line;
为此:
String read_line = b_reader.readLine();
看起来stru列表[1]不是您要查找的数据。事实上,看起来您正在尝试将一个列标题转换为双精度。您可能只需要跳过标题。您的问题是什么?你不明白为什么字符串不能转换成双精度?我不知道如何让它读取字符串值,第一行的错误。。。公司代码编号格式例外。。。它仍然给出错误此代码读取字符串值,您的问题是什么?您是否尝试过调试?能否向我们显示.CSV文件的数据可能至少在第一行?有什么帮助吗?如何跳过标题和第一列?我将所有内容复制到一个文本文件中,但没有标题和列名,但现在我无法返回到哪家公司获得了输出哦,非常感谢它跳过了标题。。。请建议我如何跳过第一列?对于companycodes??我的第一列是公司代码java.lang.NumberFormatException:对于输入字符串:“C1”有任何帮助我如何跳过标题和第一列?我将所有内容复制到一个文本文件中,没有标题和列名,但现在我无法返回到我为哪家公司获得输出。首先想到的是在解析调用周围放置一个try catch,如果发生异常,则继续。这并不理想,但却是一种想法。标题是第一行吗?如果是这样,您可以保持行计数并跳过循环中的第0行。