我从网上得到了一段关于阅读excel文件的代码。但它打印出无法识别的标志。如何以原始格式打印出来?下面是打印输出:
\H 1 h } 0vb Yp k G | 8! k \r q H)Fd i 4 0 Q ` s W&N q l)qt m S z| t L
代码在这里:
package readText;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class fileData {
public static void main(String[] args) {
String file_name = "F:\\Testfile.xlsx";
try{
ReadFileLocal file = new ReadFileLocal(file_name);
String[] arylines = file.openFile();
int i;
for (i=0; i<arylines.length; i++){
System.out.println(arylines[i]);
}
}
catch(IOException e)
System.out.println(e.getMessage());
}
}
}
public class readFileLocal {
private String path;
public readFileLocal(String file_path){
path = file_path;
}
int readLines() throws IOException{
FileReader file_to_read = new FileReader(path);
BufferedReader lines = new BufferedReader (file_to_read);
int numberOfLines = 0;
while((lines.readLine())!= null) {
numberOfLines ++;
}
lines.close();
return numberOfLines;
}
public String[] openFile() throws IOException{
FileReader freader = new FileReader (path);
BufferedReader textReader = new BufferedReader (freader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i<numberOfLines; i++){
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
答案 0 :(得分:1)
您似乎正在尝试将二进制文件作为文本读取。您将需要一个专门的库来访问Java中的Excel文件的内容。
答案 1 :(得分:1)
您可以使用Apache POI lib来读取Excel文件。