将Excel数据复制到文本文件-Java(POI) [英] Copying Excel data to Text File - Java (POI)

查看:205
本文介绍了将Excel数据复制到文本文件-Java(POI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的excel工作表看起来像这样

My excel sheet looks something like this

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

        }

感谢您的帮助.

推荐答案

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

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)

在for循环内为每个值调用一个for循环,并为字符串增加额外的空间以达到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.

这篇关于将Excel数据复制到文本文件-Java(POI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆