我想将程序的输出输入到Excel工作表/

我尝试使用Apache POI

import java.io.File; import java.io.IOException;  public class Ex3 {      public static void main(String[] args) {         File currentDir = new File("C:\\Myfiles\\Script"); // current directory         displayDirectoryContents(currentDir);     }      public static void displayDirectoryContents(File dir) {         try {              File[] files = dir.listFiles();             for (File file : files) {                 if (file.isDirectory()) {                     System.out.println("folder:" + file.getCanonicalPath());                     displayDirectoryContents(file);                 } else {                 System.out.println("file:" + file.getCanonicalPath());                 }             }         } catch (IOException e) {             e.printStackTrace();         }     } } 

我想将if语句的输出输入到第一列,将else的输出输入到第二列,或者在单独的工作表中输入这两者,这都是可行的。

===============>>#1 票数:0

无论我需要什么,我总是创建相同的方法,然后对其进行调整

public static void printAtFile(String filename, String header, String content[])     {         System.out.println("Start creating file "+filename);         PrintWriter writer = null;         try {             writer = new PrintWriter(filename);             writer.println(header);             for(String u:content)                 writer.println(u);             writer.close();         } catch (Exception ex) {             System.out.println("Error while writing at file "+filename);         }     } 

在这种情况下,您想要做类似的事情

String h = "Column1,column2" String content[] = new String[N]; Arrays.fill(content, ""); for(int i=0;i<N;i++) {     if(...)content[i]="whateveryouwant,"+"blank";     else content[i]="blank,"+"whateveryouwant"; } printAtFile("file.csv",h,content);  

  ask by DarkNight translate from so

本文未有回复,本站智能推荐: