2017-08-24 41 views
0

我是新来的Excel - Java(Apache POI),并试图找出一种方法将excel数据传输到文本文件。复制Excel数据到文本文件 - Java(POI)

我的Excel工作表看起来是这样的

 name table latest_table Date 
1 george table1 t459   2017-08-24 
2 john table1 thi7   2017-07-23 
3 mary table1 gdr99   2017-07-22 

到目前为止我的代码

public static void excel_to_text(XSSFWorkbook wb) { 
     XSSFSheet sheet = wb.getSheetAt(0); 
     int rows = sheet.getPhysicalNumberOfRows(); 
     int cols = 0, tmp = 0; 
     XSSFRow row; 
     XSSFCell cell; 

     for(int i = 0; i < 10 || i < rows; i++) { 
       row = sheet.getRow(i); 
       if(row != null) { 
        tmp = sheet.getRow(i).getPhysicalNumberOfCells(); 
        if(tmp > cols) cols = tmp; 
       } 
      } 

      for(int r = 0; r < rows; r++) { 
       row = sheet.getRow(r); 
       if(row != null) { 
        for(int c = 0; c < cols; c++) { 
         cell = row.getCell((short)c); 
         if(cell != null) { 
          *// code here* 
         } 
        } 
       } 
      } 

     } 

任何帮助表示赞赏。

+1

你的代码有什么问题? – 2017-08-24 15:51:51

+0

文本文件应该如何显示?如果你的问题是关于如何将数据/字符串写入文件,请看这里: https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to- it-in-java – Piwo

+0

如何存储数据,因为存在不同类型的数据。另外,有没有办法让文本文件看起来完全像表一样? – Chid

回答

0

据我记得有一个单元格getStringValue()方法。在读取字符串值之前,可能您已将cellformat设置为字符串格式。 读取值后,您可以将行和单元格值存储在列表<列表<字符串>>中。 (值) P 比你应该找到最长的字符串长度。 (k)

为每个值调用for循环for循环,并为字符串添加额外空间以达到k长度。有些作家类可以定义何时在文本文件中开始一个新行。