package scrollable.excel.reader;

import java.io.IOException; import java.io.InputStream; import java.util.Locale; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.poi.POIXMLProperties; import org.apache.poi.POIXMLTextExtractor; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.util.SAXHelper; import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler; import org.apache.poi.xssf.model.StylesTable; import org.apache.xmlbeans.XmlException; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader;

public class ScrollDownXSSFEventBasedExcelExtractor{

private OPCPackage container;  //private POIXMLProperties properties;  private int sheetId;

private transient BlockingQueue<SimpleRow> rowsQueue = new ArrayBlockingQueue<SimpleRow>(1);  private transient boolean hasNextRow = true;  private transient SimpleRow currentRow;

public ScrollDownXSSFEventBasedExcelExtractor(String fileName, int sheetId) throws XmlException, OpenXML4JException, IOException {   this.container = OPCPackage.open(fileName);   //this.properties = new POIXMLProperties(container);   this.sheetId = sheetId;   }

public void processSheet(){   new XMLParser().start();  }    public SimpleRow nextRow(){   SimpleRow row = null;   if(hasNextRow){    while(true)    {     try {      row = rowsQueue.take();      break;     } catch (InterruptedException e) {      // TODO Auto-generated catch block      e.printStackTrace();     }    }   }   return row;  }      public boolean hasNextRow(){   return hasNextRow;  }

protected class ScrollableSheetContentsHandler implements SheetContentsHandler  {   @Override   public void startRow(int rowNum) {    currentRow = new SimpleRow(rowNum);   }

@Override   public void endRow() {    if(currentRow != null)    {     while(true){      //try until current row is in rowsQueue      try {       rowsQueue.put(currentRow);       break;      } catch (InterruptedException e) {             }     }    }   }

@Override   public void cell(String cellRef, String formattedValue) {    SimpleCell cell = new SimpleCell(cellRef, formattedValue);    currentRow.addCell(cell);   }

@Override   public void headerFooter(String text, boolean isHeader, String tagName) {    // We don't include headers in the output yet, so ignore   }  }    protected class ScrollableXSSFSheetXMLHandler extends XSSFSheetXMLHandler {

public ScrollableXSSFSheetXMLHandler(StylesTable styles,     ReadOnlySharedStringsTable strings,     SheetContentsHandler sheetContentsHandler) {    super(styles, strings, sheetContentsHandler, false);   }

@Override   public void endElement(String uri, String localName, String name) throws SAXException {    super.endElement(uri, localName, name);    //end sheet    if("sheetData".equals(name))    {     hasNextRow = false;    }   }

}    protected class XMLParser extends Thread{

@Override   public void run() {    try {    ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container);          XSSFReader xssfReader = new XSSFReader(container);          StylesTable styles = xssfReader.getStylesTable();          InputStream  sheetInputStream = xssfReader.getSheet("rId"+sheetId);    InputSource sheetSource = new InputSource(sheetInputStream);             XMLReader sheetParser = SAXHelper.newXMLReader();     SheetContentsHandler sheetContentsExtractor = new ScrollableSheetContentsHandler();     ContentHandler handler = new ScrollableXSSFSheetXMLHandler(styles, strings, sheetContentsExtractor);     sheetParser.setContentHandler(handler);     sheetParser.parse(sheetSource);    } catch (ParserConfigurationException | IOException | SAXException | OpenXML4JException e) {     throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());    }finally{     if (container != null) {      try {       container.close();      } catch (IOException e) {       e.printStackTrace();      }     }    }   }     }

}

package scrollable.excel.reader;

public class SimpleCell {  private String refId;  private String textValue;    public SimpleCell(){}  public SimpleCell(String refId, String textValue){   this.refId = refId;   this.textValue = textValue;  }  public String getRefId() {   return refId;  }  public void setRefId(String refId) {   this.refId = refId;  }  public String getTextValue() {   return textValue;  }  public void setTextValue(String textValue) {   this.textValue = textValue;  }     }

package scrollable.excel.reader;

import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map;

public class SimpleRow {  private Map<String, SimpleCell> cells = new LinkedHashMap<String,SimpleCell>();  private int rowIndex;    public SimpleRow(){}  public SimpleRow(int rowNum){   this.rowIndex = rowNum;  }    public int getRowIndex() {   return rowIndex;  }  public void setRowIndex(int rowIndex) {   this.rowIndex = rowIndex;  }    public void addCell(SimpleCell cell){   this.cells.put(cell.getRefId(), cell);  }    public SimpleCell getCellByRefId(String cellRef)  {   return this.cells.get(cellRef);  }    public Collection<SimpleCell> getCellsInRow(){   return this.cells.values();  }    public String toString(){   StringBuilder sb = new StringBuilder();   sb.append("row ").append(rowIndex);   Collection<SimpleCell> cells = this.getCellsInRow();   for(SimpleCell cell : cells)   {    sb.append("\n").append("\t").append(cell.getRefId()).append("\t").append(cell.getTextValue());   }   return sb.toString();  } }

package block.excel.writer;

import java.io.FileOutputStream; import java.io.IOException; import java.util.Collection; import java.util.List;

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import scrollable.excel.reader.SimpleCell; import scrollable.excel.reader.SimpleRow;

public class SXSSFExcelWritor {  public static final int DEFAULT_CACHED_ROWS = 100;  private int cachedRowSize = DEFAULT_CACHED_ROWS;

private SXSSFWorkbook workbook;  private String fileName;  private Sheet sheet;  private FileOutputStream out;

public SXSSFExcelWritor() {  }

public SXSSFExcelWritor(String fileName) {   this.fileName = fileName;  }

public SXSSFExcelWritor(String fileName, int cachedRowSize) {   this(fileName);   this.cachedRowSize = cachedRowSize;  }    public void init() throws IOException{   workbook= new SXSSFWorkbook(cachedRowSize);   sheet = workbook.createSheet();   out = new FileOutputStream(fileName);  }    public void close(){   if(out != null)   {    try {     workbook.dispose();     out.close();    } catch (IOException e) {     e.printStackTrace();    }   }  }

public void write(List<SimpleRow> rows) throws IOException{   for(SimpleRow row : rows)   {    Row currentRow = sheet.createRow(row.getRowIndex());    Collection<SimpleCell> cells = row.getCellsInRow();    for(SimpleCell cell: cells)    {     CellReference cellRef = new CellReference(cell.getRefId());     int colIndex = cellRef.getCol();          Cell currentCell = currentRow.createCell(colIndex);     currentCell.setCellValue(cell.getTextValue());    }   }   workbook.write(out);   workbook.dispose();  }

public static void main(String[] args) throws Throwable {   SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory,              // exceeding rows will be              // flushed to disk   Sheet sh = wb.createSheet();   for (int rownum = 0; rownum < 100000; rownum++) {    Row row = sh.createRow(rownum);    for (int cellnum = 0; cellnum < 10; cellnum++) {     Cell cell = row.createCell(cellnum);     String address = new CellReference(cell).formatAsString();     cell.setCellValue(address);    }

}

// Rows with rownum < 900 are flushed and not accessible   for (int rownum = 0; rownum < 900; rownum++) {    // Assert.assertNull(sh.getRow(rownum));   }

// ther last 100 rows are still in memory   for (int rownum = 900; rownum < 1000; rownum++) {    // Assert.assertNotNull(sh.getRow(rownum));   }

FileOutputStream out = new FileOutputStream("C:\\Users\\YZM\\Desktop\\test2.xlsx");   wb.write(out);   out.close();

// dispose of temporary files backing this workbook on disk   wb.dispose();  }

}

package ui;

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;

import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.WindowConstants; import javax.swing.border.EmptyBorder;

public class MainWindow extends JFrame{

private static final long serialVersionUID = 1L;  private static final int DEFAULT_WIDTH = 800;  private static final int DEFAULT_HEIGHT = 600;

public MainWindow(String name) {   this.setTitle(name);   this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);  }

public JPanel createPIExtractorContentPanel() {   JPanel piExtractorPanel = new JPanel(new BorderLayout());      JPanel headerPanel = new JPanel();   headerPanel.setLayout(new BoxLayout(headerPanel, BoxLayout.Y_AXIS));   headerPanel.setBorder(new EmptyBorder(10, 10, 10, 10));      JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));   JLabel fileChoserLabel = new JLabel("Please chose a source excel file(support .xlsx file only):");   labelPanel.add(fileChoserLabel);   headerPanel.add(labelPanel);         JPanel fileChoserPanel = new JPanel();   fileChoserPanel.setLayout(new BoxLayout(fileChoserPanel, BoxLayout.X_AXIS));      JTextField fileNameInput = new JTextField();   fileNameInput.setEnabled(false);   fileChoserPanel.add(fileNameInput);      fileChoserPanel.add(Box.createHorizontalStrut(10));      JButton fileChoserButton = new JButton("Chose File");   fileChoserPanel.add(fileChoserButton);      headerPanel.add(fileChoserPanel);

piExtractorPanel.add(headerPanel, BorderLayout.NORTH);      //center   JPanel logPanel = new JPanel();   //JScrollPane scrollPanel=new JScrollPane();   JTextArea textArea = new JTextArea(25,80);   textArea.setLineWrap(true);   textArea.setWrapStyleWord(true);      logPanel.add(new JScrollPane(textArea));      piExtractorPanel.add(logPanel, BorderLayout.CENTER);

return piExtractorPanel;

}

public static void main(String[] args) {   MainWindow mw = new MainWindow("DA Tool");   mw.add(mw.createPIExtractorContentPanel());   mw.setVisible(true);   mw.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  }     }

extractor的更多相关文章

  1. Jmeter组件4. Regular Expression Extractor

    位置:Post-Processors - Regular Expression Extractor 所谓的Post-Processors直译为后处理器,意思是在域内所有Sampler执行完后才会执行, ...

  2. [爬虫学习笔记]用于提取网页中所有链接的 Extractor 模块

            Extractor的工作是从下载的网页中将它包含的所有URL提取出来.这是个细致的工作,你需要考虑到所有可能的url的样式,比如网页中常常会包含相对路径的url,提取的时候需要将它转换 ...

  3. 谷歌开源项目Google Preview Image Extractor(PIEX) (附上完整demo代码)

    前天偶然看到谷歌开源项目中有一个近乎无人问津的项目Google Preview Image Extractor(PIEX) . 项目地址: https://github.com/google/piex ...

  4. Where is "Active Directory Information Extractor"?

    My friend she showed me a screenshot as below yesterday. The name of this document is “EnCase Forens ...

  5. Scala中的Extractor

    Scala中使用unapply方法可以实现三种extractor(另外使用unapplySeq也可以实现extractor) def unapply(object: S): Option[(T1, . ...

  6. Day 16: Goose Extractor —— 好用的文章提取工具

    Day 16: Goose Extractor -- 好用的文章提取工具 Day 16: Goose Extractor -- 好用的文章提取工具

  7. jmeter后置处理器 JSON Extractor取多个变量值

    1.需要获取响应数据的请求右键添加-后置处理器-JSON Extractor 2.如果要获取json响应数据多个值时,设置的Variable names (后续引用变量值的变量名设置)与JSON Pa ...

  8. PyInstaller Extractor安装和使用方法

    PyInstaller Extractor是可以提取出PyInstaller所创建的windows可执行文件的资源内容. 关于PyInstaller的介绍:PyInstaller安装使用方法 使用Py ...

  9. jmeter之regular expression extractor ,并循环调用匹配到的多个值

    jmeter之regular expression extractor 官方介绍:http://jmeter.apache.org/usermanual/regular_expressions.htm ...

  10. JMeter 通过JSON Extractor 插件来提取响应结果

    接口响应结果,通常为HTML.JSON格式的数据,对于HTML的响应结果的提取,可以通过正则表达式,也可以通过XPath 来提取. 对于JSON格式的数据,可以通过正则表达式.JSON Extract ...

随机推荐

  1. django博客功能实现——标签功能

    标签功能添加流程 0.功能概括 标签作为文章中的分类标记,会显示出该文章是关于哪一方面的文章,比如是关于python的还是关于django的. 当我们点击该标签的时候,会出现该博客中所有属于该标签的文 ...

  2. Python-02-基础

    一.数字 int(有符号整型) Python3可以处理任意大小的整数,当然包括负整数. int = 20 print int long(长整型) Python3中不再区分整型和长整型. float(浮 ...

  3. WebApi2跨域问题

    一.跨域问题产生的原因:同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能. 现在所有支持JavaScript 的浏览器都会使用这个策略. 所谓同源是指,域 ...

  4. httpUrlConnection中文乱码

    public void getFeiInfo(String sessionId) throws IOException{ //发送的请求参数,发送的格式也是Json的 String requestSt ...

  5. 配置容器configuring Containsers

    容器可以在运行时配置,相反的也可以通过应用程序的配置文件(或扩展配置文件)来配置. Unity的三个高级功能:泛型装饰链.解析器重写和数组注入. 1.配置开放式泛型来解析封闭式泛型 只要不是为封闭型泛 ...

  6. DbUtility v3 背后的故事

    DbUtility v3 背后的故事 时间 DbUtility v3构思了差不多大半年,真正开发到第一个版本发布到NuGet却只花了50天.中途大量时间在完善 Jumony 3,只有三周来开发DbUt ...

  7. 【OpenJudge 1793】矩形覆盖

    http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...

  8. bzoj4282慎二的随机数列

    海带头又上线了QwQ~ 这是一个奇怪的lis问题 显然一定存在一种最优答案使所有辨认不清的数都在答案中. [为什么呢]因为你完全可以用一个'N'来替换一个'K'啊QwQ~ 那么在选完所有'N'之后,一 ...

  9. ASP.NET MVC中viewData、viewBag和templateData的使用与区别

    一:类型比较 1.1)ViewBag是动态类型(dynamic). 1.2)ViewData是一个字典型的(Dictionary)-->ViewDataDictionary. 1.3)TempD ...

  10. Django学习笔记(现学现写,实时更新)

    说明:我是先上手做一些简单的例子,然后在尝试的过程中理解Django的原理,笔记也是按这个思路来的. 一.Django结构与基本文件介绍 1. django-admin.py 工程管理工具,主要用于创 ...