将Excel数据复制到文本文件 - Java(POI)

时间:2022-02-11 20:24:57

I am new to Excel - Java (Apache POI) and trying to figure out a way to transfer excel data to a text file.

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

My excel sheet looks something like this

我的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

My Code so far

我的代码到目前为止

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*
                        }
                    }
                }
            }

        }

Any help is appreciated.

任何帮助表示赞赏。

1 个解决方案

#1


0  

As far as I remember there is a getStringValue() method on cells. Maybe you have set the cellformat to string format before reading the string value. After reading the value you can store the rows and cell values in a List < List < String> >. (values) P Than you should find the longest string length. (k)

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

Call a for cycle inside a for cycle for every value and add extra space for the strings to reach k length. There are writer classes where you can define when to start a new row in the text file.

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

#1


0  

As far as I remember there is a getStringValue() method on cells. Maybe you have set the cellformat to string format before reading the string value. After reading the value you can store the rows and cell values in a List < List < String> >. (values) P Than you should find the longest string length. (k)

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

Call a for cycle inside a for cycle for every value and add extra space for the strings to reach k length. There are writer classes where you can define when to start a new row in the text file.

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