码迷,mamicode.com
首页 > 其他好文 > 详细

groovy : poi ExcelExtractor

时间:2014-10-05 17:34:48      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:java   groovy   poi   excel   

参考 file://poi-3.10-FINAL/docs/spreadsheet/quick-guide.html#TextExtraction

Excel2txt.groovy 代码如下

import java.io.File;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;

class Excel2txt {
	
	static void main(args) {
		if (args.size()!=1){
			println ' usage: groovy Excel2txt file.xls '
			return;
		} else {
			def filename = args[0];
			if (filename==null || filename=='') return;
			def idx = filename.lastIndexOf('.');
			def ext = filename.substring(idx);
			if (ext != '.xls'){
				println ' filename is not .xls ?'
				return;
			}
			def fis = new FileInputStream(filename);
			def outfile = new File(filename.replace('xls','txt'));
			def fp = outfile.newPrintWriter();
			def wb = new HSSFWorkbook(new POIFSFileSystem(fis));
			def extractor = new ExcelExtractor(wb);
			
			extractor.setFormulasNotResults(true);
			extractor.setIncludeSheetNames(false);
			extractor.setIncludeCellComments(true);
			String text = extractor.getText();
			fp.println text
			fp.flush();
			fp.close();
		}
	}
}


groovy : poi ExcelExtractor

标签:java   groovy   poi   excel   

原文地址:http://blog.csdn.net/belldeep/article/details/39803775

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!