java 如何使用java将制表符分隔的“.txt”文件转换为Excel“.xls”格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9997129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a tab-separated ".txt" file into Excel ".xls" format using java?
提问by XenoRo
I have 498 text files resulting from WebScraping results from a form on a website, and i want to convert it all to Excel standard (.xls) format using java.
我有 498 个文本文件是由网站上的表单的 WebScraping 结果产生的,我想使用 java 将它们全部转换为 Excel 标准 (.xls) 格式。
I will have to do this conversion every month with the same amount of files, taking no less than 10 seconds per file on a hand-based conversion, summing a full 1h23m of inconvenience at best of optimism, so the method of loading each file and hitting "save_as", by hand or by robot, is forbidden.
我将不得不每个月使用相同数量的文件进行此转换,在手动转换中每个文件花费不少于 10 秒,最多乐观地总结整整 1 小时 23 分钟的不便,因此加载每个文件的方法和禁止用手或机器人点击“save_as”。
采纳答案by jtahlborn
you can use the apache poiproject to generate xls files in java.
您可以使用apache poi项目在java 中生成xls 文件。
回答by Victor Sorokin
Excel can load them as csv files, just specify that field separator is Tab
.
Excel 可以将它们加载为 csv 文件,只需指定字段分隔符为Tab
.
回答by TikkaBhuna
I would have an array looping over all the files you want. You can read in the .csv file as a standard text file storing each row in an Array and then looping over that you can create the excel sheet.
我会有一个循环遍历你想要的所有文件的数组。您可以将 .csv 文件作为标准文本文件读取,将每一行存储在一个数组中,然后循环遍历您可以创建 Excel 工作表。
Now I've never created an excel workbook from scratch but I have edited them. I wrote up the following. This will loop through all the files in a directory and scan them in. Its will just place the data from each file one after another on the first sheet. It would have been nice to use iterators but I'll let you do that. :)
现在我从来没有从头开始创建一个 excel 工作簿,但我已经编辑过它们。我写了以下内容。这将遍历目录中的所有文件并扫描它们。它只会将每个文件中的数据一个接一个地放在第一张纸上。使用迭代器会很好,但我会让你这样做。:)
public void convertCSV(File dir) throws IOException {
// Gets the CSV files.
File[] csvFiles = dir.listFiles();
// Sets up the Workbook and gets the 1st (0) sheet.
File excelFile = new File("H:\yourOutput.xls");
InputStream input = new FileInputStream(excelFile);
HSSFWorkbook workbook = new HSSFWorkbook(input);
HSSFSheet sheet = workbook.getSheetAt(0);
int rowNo = 0;
int columnNo = 0;
// Loop through the files.
for(File file : csvFiles) {
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
// Gets the row and if it doesn't exist it will create it.
Row tempRow = sheet.getRow(rowNo);
if(tempRow == null)
tempRow = sheet.createRow(rowNo);
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\t");
// While there is more text to get it will loop.
while(lineScanner.hasNext()) {
// Gets the cell in that row and if it is null then it will be created.
Cell tempCell = tempRow.getCell(columnNo, Row.CREATE_NULL_AS_BLANK);
String output = lineScanner.next();
// Write the output to that cell.
tempCell.setCellValue(new HSSFRichTextString(output));
columnNo++;
}
// Resets the column count for the new row.
columnNo = 0;
rowNo++;
}
}
// Writes the file and closes everything.
FileOutputStream out = new FileOutputStream(excelFile);
workbook.write(out);
input.close();
out.close();
}
Tested it and it works with having tab as a delimiter. You just need to specify the directory that your CSV files are in.
对其进行了测试,它可以使用制表符作为分隔符。您只需要指定 CSV 文件所在的目录。