按顺序读取行,但按需读取

时间:2017-10-06 08:44:28

标签: java excel apache-poi

我需要逐行遍历Excel电子表格,但不能一次遍历。我想保持对电子表格打开的流,然后将该对象传递给另一个方法。该方法将不时要求下一行数据。这个方法已经建立,所以我无法改变它。

我最初尝试使用#Clean up CSV file #Remove: &amp; \&lt; &gt; file_names = ['terms.csv'] file_names.each do |file_name| text = File.read(file_name) new_contents = text.gsub(/&amp;/, " and ") # To merely print the contents of the file, use: puts new_contents # To write changes to the file, use: File.open(file_name, "w") {|file| file.puts new_contents } end file_names.each do |file_name| text = File.read(file_name) new_contents = text.gsub(/&lt;/, " < ") puts new_contents File.open(file_name, "w") {|file| file.puts new_contents } end file_names.each do |file_name| text = File.read(file_name) new_contents = text.gsub(/&gt;/, " > ") puts new_contents File.open(file_name, "w") {|file| file.puts new_contents } end 进行此操作,效果非常好,直到我对实际数据的内存不足。

然后我尝试使用XSSFWorkbook切换到SAX解析器,使用类似于Apache POI项目提供的示例一个XSSFSheetXMLHandler的自定义解析器。但这导致所有行一次处理;如果我将它们存储在内存中以便稍后阅读,我也会耗尽内存。我也无法访问XLSX2CSV,我需要进行单元格值处理。

有人能指出我允许我这样做的示例/课程吗?

1 个答案:

答案 0 :(得分:1)

我更喜欢XML的Streaming API是StAX

知道*.xlsx文件只是ZIP存档而apache poi s OPCPackage就是ZipPackage,我们可以考虑以下方法:

  • /xl/worksheets/sheetN.xml *.xlsx Excel
  • 获取ZipPackage个包裹部分
  • 在其上创建一个StAX读者。
  • 现在我们可以使用此阅读器阅读XML

以下示例创建一个基本应用程序,使用按钮单击逐行执行此操作。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import org.apache.poi.openxml4j.opc.*;

import javax.xml.stream.*;
import javax.xml.stream.events.*;

import javax.xml.namespace.QName;

import java.util.regex.Pattern;

public class GetExcelRowByRow extends JPanel implements ActionListener {
 protected JButton button;
 protected JTextArea textArea;
 private final static String newline = "\n";

 //file path to Excel file and sheet number to work with
 private final static String filepath = "file.xlsx";
 private final static int scheetnr = 1;

 private StaxExcelRowByRowReader reader;

 public GetExcelRowByRow() {
  super(new GridBagLayout());
  button = new JButton("Next Row");
  button.addActionListener(this);
  textArea = new JTextArea(15, 50) {
   @Override
   public boolean getScrollableTracksViewportWidth() {
    return true;
   }
  };
  textArea.setLineWrap(true);
  textArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(textArea);
  GridBagConstraints c = new GridBagConstraints();
  c.gridwidth = GridBagConstraints.REMAINDER;
  c.fill = GridBagConstraints.HORIZONTAL;
  add(button, c);
  c.fill = GridBagConstraints.BOTH;
  c.weightx = 1.0;
  c.weighty = 1.0;
  add(scrollPane, c);

  try {
   reader = new StaxExcelRowByRowReader(filepath, scheetnr);
  } catch (Exception ex) {
   ex.printStackTrace();
  }

 }
 @Override
 public void actionPerformed(ActionEvent evt) {
  String row = "Row not found...";
  try {
   row = reader.getNextRow();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  textArea.append(row + newline);
  textArea.setCaretPosition(textArea.getDocument().getLength());
 }

 public StaxExcelRowByRowReader getReader() {
  return reader;
 }

 private static void createAndShowGUI() {
  JFrame frame = new JFrame("Get Excel row by row");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  GetExcelRowByRow app = new GetExcelRowByRow();
  frame.add(app);

  frame.addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent windowEvent) {
    try {
     app.getReader().close();
    } catch (Exception ex) {
     ex.printStackTrace();
    }
    System.exit(0);
   }
  });

  frame.pack();
  frame.setVisible(true);
 }

 public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    createAndShowGUI();
   }
  });
 }

 //class for reading a /xl/worksheets/sheetN.xml package part from a *.xlsx Excel ZipPackage 
 private class StaxExcelRowByRowReader {

  private XMLEventReader sheetreader;
  private OPCPackage opcpackage; 

  public StaxExcelRowByRowReader(String filepath, int sheetnr) {
   try {
    opcpackage = OPCPackage.open(filepath, PackageAccess.READ);
    //get the sheet package part
    PackagePart sheetpart = opcpackage.getPartsByName(Pattern.compile("/xl/worksheets/sheet"+sheetnr+".xml")).get(0);
    //create reader for the sheet package part            
    sheetreader = XMLInputFactory.newInstance().createXMLEventReader(sheetpart.getInputStream());
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }

  //method for getting the next row from the reader
  public String getNextRow() throws Exception {
   StringBuffer row = new StringBuffer();
   boolean valueFound = false;
   boolean nextValueIsSharedString = false;
   while(sheetreader.hasNext()){
    XMLEvent event = sheetreader.nextEvent();
    if(event.isStartElement()) {
     StartElement startElement = (StartElement)event;
     QName startElementName = startElement.getName();
     if(startElementName.getLocalPart().equalsIgnoreCase("row")) { //start element of row
      row.append("<row");
      row.append(" " + startElement.getAttributeByName(new QName("r")));
      row.append(">");
     } else if(startElementName.getLocalPart().equalsIgnoreCase("c")) { //start element of cell
      row.append("<c");
      row.append(" " + startElement.getAttributeByName(new QName("r")));
      row.append(" " + startElement.getAttributeByName(new QName("s")));
      row.append(" " + startElement.getAttributeByName(new QName("t")));
      row.append(">");
      Attribute type = startElement.getAttributeByName(new QName("t"));
      if (type != null && "s".equals(type.getValue())) {
       nextValueIsSharedString = true;
      } else {
       nextValueIsSharedString = false;
      }
     } else if(startElementName.getLocalPart().equalsIgnoreCase("v")) { //start element of value
      row.append("<v>");
      valueFound = true;
     }
    } else if(event.isCharacters() && valueFound) {
     Characters characters = (Characters)event;
     if (nextValueIsSharedString) {
      row.append("shared string: " + characters.getData());
     } else {
      row.append(characters.getData());
     }
    } else if(event.isEndElement()) {
     EndElement endElement = (EndElement)event;
     QName endElementName = endElement.getName();
     if(endElementName.getLocalPart().equalsIgnoreCase("v")) { //end element of value
      row.append("</v>");
      valueFound = false;
     } else if(endElementName.getLocalPart().equalsIgnoreCase("c")) { //end element of cell
      row.append("</c>");
     } else if(endElementName.getLocalPart().equalsIgnoreCase("row")) { //end element of row
      row.append("</row>");
      return row.toString();
     }
    }
   }
   return "No more rows.";
  }

  public void close() throws Exception {
   if (sheetreader != null) sheetreader.close();
   if (opcpackage != null) opcpackage.close();
  }
 }
}

当然,这只是一个显示原则的草案。整个应用程序将会有更多的代码。

接下来,我们必须阅读并解析包含共享字符串的/xl/sharedStrings.xml包部分。此外,我们必须阅读并解析包含单元格样式的/xl/styles.xml包部分。我们需要样式来检测数值是日期还是数字,如果是数字,那么是什么类型的数字。这是必要的,因为Excel将所有类型的数字存储为double值。日期也是数字为双打,意味着在01/01/1900之后的几天,小数部分为1h = 1 / 24,1m = 1/24 / 60,1s = 1/24/60/60。

但是这可以使用与/xl/worksheets/sheetN.xml包部分相同的方法。