我是JAVA的新手。我要求将特定文件夹中的多个文件加载到excel xlsx(不是xls文件,因为输入文件的记录超过65k)文件不同。下面是我写的代码。 xlsx文件已损坏,无法打开。你能帮忙吗?
public class twotxttoxlsx{
public static void main(String[]args){
try{
File folder = new File("C:/txttoxls/");
File[] listOfFiles = folder.listFiles();
XSSFWorkbook workbook=new XSSFWorkbook();
for (File file : listOfFiles) {
if (file.isFile()) {
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
XSSFSheet sheet = workbook.createSheet(file.getName());
FileInputStream fis = new FileInputStream(file);
System.out.println("Got here");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((thisline = br.readLine()) != null) {
al = new ArrayList<String>();
String strar[] = thisline.split("#");
for (int j = 0; j < strar.length; j++) {
for (int k = 0; k < arlist.size(); k++) {
ArrayList<String> ardata = (ArrayList<String>) arlist.get(k);
XSSFRow row = sheet.createRow((short) k);
for (int p = 0; p < ardata.size(); p++) {
XSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
al.add(strar[j]);
}
arlist.add(al);
}
fis.close();
FileOutputStream fileOut = new FileOutputStream("C:/txttoxls/Test.xlsx");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
}
}
System.out.println("Your excel file has been generated!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
编辑于4月27日:工作代码但存在性能问题。
import java.io.*;
import java.util.ArrayList;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
public class createXLSXfileTest{
public static void main(String[]args){
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("sheet1");
XSSFSheet sheet1 = (XSSFSheet) workbook.createSheet("Sheet2");
try{
File file = new File("C:/Users/Desktop/file1.txt");
FileOutputStream fileOut = new FileOutputStream("C:/Users/Desktop/test.xlsx");
if (file.isFile()) {
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
FileInputStream fis = new FileInputStream(file);
System.out.println("loading 1st file");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((thisline = br.readLine()) != null) {
al = new ArrayList<String>();
String strar[] = thisline.split("#");
for (int j = 0; j < strar.length; j++) {
for (int k = 0; k < arlist.size(); k++) {
ArrayList<String> ardata = (ArrayList<String>) arlist.get(k);
XSSFRow row = sheet.createRow((short) k);
for (int p = 0; p < ardata.size(); p++) {
XSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
al.add(strar[j]);
}
arlist.add(al);
}
fis.close();
}
File file1 = new File("C:/Users/Desktop/file2.txt");
if (file1.isFile()) {
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
FileInputStream fis = new FileInputStream(file1);
System.out.println("loading 2nd file");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((thisline = br.readLine()) != null) {
al = new ArrayList<String>();
String strar[] = thisline.split("#");
for (int j = 0; j < strar.length; j++) {
for (int k = 0; k < arlist.size(); k++) {
ArrayList<String> ardata = (ArrayList<String>) arlist.get(k);
XSSFRow row = sheet1.createRow((short) k);
for (int p = 0; p < ardata.size(); p++) {
XSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
al.add(strar[j]);
}
arlist.add(al);
}
fis.close();
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
System.out.println("Your excel file has been generated!");
}} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
答案 0 :(得分:2)
我唯一能看到的是你的工作表名称可能是个问题。
XSSFSheet sheet = workbook.createSheet(file.getName());
Excel工作表名称极其严格。最大长度为31,不允许使用许多特殊字符,包括/[]:\?
等等。考虑编写“toWorksheetName”函数将文件名清理为合法的工作表名称。
您可以尝试的另一件事是尝试使用POI读取XLS。这可能有助于缩小问题范围。 Help is here,在“使用.xlsx文件”下。
旁注:您无需关闭fis
;关闭br
就足够了。 Close()从装饰器传递到包装的流。这不会破坏任何东西。