java 在没有 Apache POI 的情况下解析 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10248821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:13:27  来源:igfitidea点击:

Parsing Excel file without Apache POI

javaapache-poi

提问by Rengasami Ramanujam

I know that we can use Apache POI to parse an Excel file and get data. But I heard of a strange thing that excel file can be passed in a similar way we parse CSV (Like just read the file from file Stream and separate each column value with a "comma" separator). When we parse Excel we have to use tab as a delimiter. Is it possible? If yes then why Apache has come up with such a complicated framework. I am puzzled. Can someone help me?

我知道我们可以使用 Apache POI 来解析 Excel 文件并获取数据。但我听说一个奇怪的事情,excel 文件可以以我们解析 CSV 的类似方式传递(就像从文件 Stream 中读取文件并用“逗号”分隔符分隔每个列值)。当我们解析 Excel 时,我们必须使用制表符作为分隔符。是否可以?如果是,那么为什么 Apache 提出了如此复杂的框架。我很困惑。有人能帮我吗?

采纳答案by maksimov

CSV is a text format, so it can be parsed using the delimiters. Old Excel is a binary and proprietary format so it needs clever decoding. The new Excel format is zipped XMLs, but one should also understand the structure of this document before it could be transformed into something as simple as reading cells one by one. So the answer to your question is no, you'll need to use Apache POI, and also - there's nothing wrong with that.

CSV 是一种文本格式,因此可以使用分隔符对其进行解析。旧 Excel 是一种二进制和专有格式,因此需要巧妙的解码。新的 Excel 格式是压缩的 XML,但在将其转换为像逐个读取单元格这样简单的内容之前,还应该了解该文档的结构。所以你的问题的答案是否定的,你需要使用 Apache POI,而且 - 这没有任何问题。

As a side note, on the path to become a good developer you will need to learn to do a bit of your own research before looking for help. Get your hands dirty is the best way to learn things.

附带说明一下,在成为一名优秀开发人员的道路上,您需要在寻求帮助之前先学会做一些自己的研究。把手弄脏是学习东西的最好方法。

回答by Michael Berry

You've probably confused what you've heard, or the person telling you was confused.

您可能混淆了您所听到的内容,或者告诉您的人感到困惑。

Some parts of Excel files canbe stored (somewhat) as CSV files as the tabular data structure fits well within a CSV file format. However, if you save in CSV format then you just get plain text in each cell - you lose all formatting information, any graphs, multiple worksheets and so on.

Excel 文件的某些部分可以(在某种程度上)存储为 CSV 文件,因为表格数据结构非常适合 CSV 文件格式。但是,如果您以 CSV 格式保存,那么您只会在每个单元格中获得纯文本 - 您会丢失所有格式信息、任何图形、多个工作表等。

The native XLS excel format is what Apache POI works with, and so can handle everything in excel, not just restrictive plain text in certain cells. CSV files have their uses but they're definitely not a straight replacement for normal Excel files.

Apache POI 使用原生 XLS excel 格式,因此可以处理 excel 中的所有内容,而不仅仅是某些单元格中的限制性纯文本。CSV 文件有其用途,但它们绝对不能直接替代普通 Excel 文件。

回答by Shahrukh A.

i tried to read/write excel file without using any external JAR like POI or any other. I am able to write file as xls format. Here is my Code

我尝试在不使用任何外部 JAR(如 POI 或任何其他 JAR)的情况下读取/写入 excel 文件。我可以将文件写为 xls 格式。这是我的代码

FileWriter fwriter = new FileWriter(file,true);
writer = new BufferedWriter(fwriter);
writer.newLine();
writer.write("a"    + "\t");
writer.write("b"    + "\t");
writer.write("c"    + "\t");
writer.write("d"    + "\t");
writer.write("e"    + "\t");
writer.write("f"    + "\t");

Reading File here is my code for reading

在这里阅读文件是我的阅读代码

if(file != null) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(file));
                String line;
                while((line = reader.readLine()) != null) {
                    String[] component = line.split("\t");
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

回答by Baisakha Chauhan

InputStream is = new FileInputStream(new File(filepath));
        StreamingReader reader=null;
        try {
            reader = StreamingReader.builder()
                    .rowCacheSize(100)     
                    .bufferSize(4096)     
                    .sheetIndex(0)        
                    .read(is);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }finally{
            is.close();
        }
        //pass here to reader and itrate it 
          for (Row row : reader) {
            if (row.getRowNum()!=0){
                for (Cell cell : row) {
              // write ur logic to store ur value 
                }

            }
        }