如何用Java将控制台输出写入Excel文件
大家好,我是java新手。在我的程序中,我可以选择文本文件并读取它们。我需要在不使用数据库的情况下将这些读取文件写入excel文件。我写了这些代码 我使用对象数组插入数据,但我不需要逐个写入文本文件的全部内容 有人能帮我吗……提前谢谢。 我的代码 公共类第一示例{如何用Java将控制台输出写入Excel文件,java,excel,console-output,Java,Excel,Console Output,大家好,我是java新手。在我的程序中,我可以选择文本文件并读取它们。我需要在不使用数据库的情况下将这些读取文件写入excel文件。我写了这些代码 我使用对象数组插入数据,但我不需要逐个写入文本文件的全部内容 有人能帮我吗……提前谢谢。 我的代码 公共类第一示例{ public static void main(String[] args) throws FileNotFoundException, IOException, BiffException, WriteException {
public static void main(String[] args) throws FileNotFoundException, IOException, BiffException, WriteException {
String excelFilePath="C:\\Users\\Morlot\\Desktop\\Dosy1.xlsx";
try{
FileInputStream inpStr=new FileInputStream(new File(excelFilePath));
org.apache.poi.ss.usermodel.Workbook wbook= org.apache.poi.ss.usermodel.WorkbookFactory.create(inpStr);
org.apache.poi.ss.usermodel.Sheet sheet=wbook.getSheetAt(0);
Object[][] bookData= {{
“20.938”,“0.2670”,“304.66”,“0.6196”,“0.0000”,“0.0000”,“0.971”,“0.1377”,“1.5959”,“0.4619”,“0.4605”,“0.299.15”,“100.03”,“0.0007”,“0.0030”,“0.2314”,“0.0031”,“0.299.51”,“14:51:06\n”},{“20.938”、“0.2670”、“304.66”、“0.6249”、“0.0000”、“0.0000”、“0.0000”、“0.1252”、“0.1252”、“0.6774”、“0.4240”、“0.4213”、“0.297.19”、“100.03”、“0.0006”、“0.0029”、“0.0031”、“0.299.51”、“14:51:09\n”},{
“20.938”,“0.2670”,“304.67”,“0.6286”,“0.0000”,“0.0000”,“0.971”,“0.1000”,“1.7182”,“0.3423”,“0.3412”,“0.293.18”,“99.934”,“0.0013”,“0.0027”,“0.2288”,“0.0031”,“0.299.51”,“14:51:12\n”},{
“20.937”“0.2670”“304.67”“0.6342”“0.0000”“0.1249”“0.846”“0.0964”“0.7998”“0.3332”“0.3310”“0.291.19”“99.979”“0.0004”“0.0026”“0.2316”“0.0031”“14:51:15\n” }};
int rowCount=sheet.getFirstRowNum();
for(Object[] dataBook : bookData){
Row row=sheet.getRow(++rowCount);
int columnCount=0;
org.apache.poi.ss.usermodel.Cell cell= row.createCell(columnCount);
cell.setCellValue(rowCount);
for(Object field : dataBook){
cell= row.createCell(++columnCount);
if(field instanceof String){
cell.setCellValue((String) field);
}else if(field instanceof Integer){
cell.setCellValue((Integer) field);
}
}
}
inpStr.close();
FileOutputStream opStr= new FileOutputStream("C:\\Users\\Morlot\\Desktop\\Dosy1.xlsx");
System.out.println(excelFilePath + "data2 is written succesfully");
//10 kasım init text verisini excele yazdırma komutu
wbook.write(opStr);
wbook.close();
}catch(IOException | EncryptedDocumentException
| InvalidFormatException ex){
ex.printStackTrace();
}
}
}
解决此问题的更简单方法是另存为*.csv文件。 这是一个包含一些规则的文本文件,链接解释如下:
它可以通过任何excel读取,解决方法更简单,另存为*.csv文件。 这是一个包含一些规则的文本文件,链接解释如下:
它可以由任何excel读取您需要将数据导出到*.csv文件中。 我曾经遇到过类似的问题,这篇文章非常有用: 以下是相关部分: CSVUtils.java
您需要将数据导出到*.csv。 我曾经遇到过类似的问题,这篇文章非常有用: 以下是相关部分: CSVUtils.java
csv文件的工作原理是这样的:获取一些txt文件,并用扩展名.csv保存,它包含的内容应该是这样的:year,branhd,id 1997,Ford,E350,其中第一行是列名,后面是carrier return和逗号作为collumnswell的分隔符,csv文件的工作原理是:获取一些txt文件,用扩展名.csv保存它,它包含的内容应该是这样的:year,branhd,id 1997,Ford,E350,其中第一行是列名,后面是carrier return和逗号作为collumns的分隔符
package com.mkyong.utils;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public class CSVUtils {
private static final char DEFAULT_SEPARATOR = ',';
public static void writeLine(Writer w, List<String> values) throws IOException {
writeLine(w, values, DEFAULT_SEPARATOR, ' ');
}
public static void writeLine(Writer w, List<String> values, char separators) throws IOException {
writeLine(w, values, separators, ' ');
}
//https://tools.ietf.org/html/rfc4180
private static String followCVSformat(String value) {
String result = value;
if (result.contains("\"")) {
result = result.replace("\"", "\"\"");
}
return result;
}
public static void writeLine(Writer w, List<String> values, char separators, char customQuote) throws IOException {
boolean first = true;
//default customQuote is empty
if (separators == ' ') {
separators = DEFAULT_SEPARATOR;
}
StringBuilder sb = new StringBuilder();
for (String value : values) {
if (!first) {
sb.append(separators);
}
if (customQuote == ' ') {
sb.append(followCVSformat(value));
} else {
sb.append(customQuote).append(followCVSformat(value)).append(customQuote);
}
first = false;
}
sb.append("\n");
w.append(sb.toString());
}
}
package com.mkyong.utils;
import java.io.FileWriter;
import java.util.Arrays;
public class CVSUtilExample {
public static void main(String[] args) throws Exception {
String csvFile = "/Users/mkyong/csv/abc.csv";
FileWriter writer = new FileWriter(csvFile);
CSVUtils.writeLine(writer, Arrays.asList("a", "b", "c", "d"));
//custom separator + quote
CSVUtils.writeLine(writer, Arrays.asList("aaa", "bb,b", "cc,c"), ',', '"');
//custom separator + quote
CSVUtils.writeLine(writer, Arrays.asList("aaa", "bbb", "cc,c"), '|', '\'');
//double-quotes
CSVUtils.writeLine(writer, Arrays.asList("aaa", "bbb", "cc\"c"));
writer.flush();
writer.close();
}
}