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. UVA 11859 Division Game[Nim游戏]

    题意:给定一个N*M的矩阵,每次可以选择同一行中的若干个数,把它们变成它们的质因子.问说先手的可否获胜. 同一行相当于1堆,数量就是所有数的质因子个数之和 #include <iostream& ...

  2. [No00006A]Js的addEventListener()及attachEvent()区别分析【js中的事件监听】

    1.添加时间监听: Chrom中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target ...

  3. linux系统下的权限知识梳理

    下面对linux系统下的有关权限操作命令进行了梳理总结,并配合简单实例进行说明.linux中除了常见的读(r).写(w).执行(x)权限以外,还有其他的一些特殊或隐藏权限,熟练掌握这些权限知识的使用, ...

  4. 最新JavaScript、Ajax典藏级学习资料下载分类汇总 (2011年12月21日更新)

    其他网站开发相关资料            超强HTML和xhtml,CSS精品学习资料下载汇总                                               最新htm ...

  5. 1264: [AHOI2006]基因匹配Match

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 982  Solved: 635[Submit][S ...

  6. C#进阶系列——AOP?AOP!

    前言:今天大阅兵,可是苦逼的博主还得坐在电脑前写博客,为了弄清楚AOP,博主也是拼了.这篇打算写写AOP,说起AOP,其实博主接触这个概念也才几个月,了解后才知道,原来之前自己写的好多代码原理就是基于 ...

  7. 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验

    20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验 20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验

  8. tomcat远程部署应用

    Tomcat安装成功后,在ip地址:8080上就可以看见熟悉的首页,在这个首页中,上方有一个manage app按钮,点击就可以进行应用管理了.这样就不需要使用ftp把war包传上去了. 要想远程部署 ...

  9. mysql select

    select 查询: 赋值:赋值不能应用在where中,因为where操作的是磁盘上的文件,可以应用在having筛选中. 例:select (market_price-shop_price) as ...

  10. iOS小知识点(非UI部分)

    1. _cmd 表示当前方法的@SEL指针, - (void)putString{} 对于这个函数_cmd 等效于@selector(putString)