GROOVY: poi read Excel XLSX

Source: Internet
Author: User

Download poi-bin-3.10-FINAL-20140208.zip unzip

Copy
Poi-3.10-FINAL-20140208.jar
Poi-ooxml-3.10-FINAL-20140208.jar
Poi-ooxml-schemas-3.10-FINAL-20140208.jar
Ooxml-Lib \ dom4j-1.6.1.jar
Ooxml-Lib \ dom4j-1.6.1.jar
Ooxml-Lib \ xmlbeans-2.3.0.jar
To D: \ groovy-1.8.9 \ Lib \

The read_xlsx.groovy code is as follows:

import javax.swing.JFileChooserimport javax.swing.filechooser.FileFilterimport javax.swing.filechooser.FileNameExtensionFilterimport java.io.File;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;class ReadExcel {static void main(args) {def getFilename ={def initDir='D:/'def chooser=new JFileChooser(initDir)def filter = new FileNameExtensionFilter('Excel', 'xlsx')chooser.setFileFilter(filter)chooser.setDialogTitle("Open")if(chooser.showSaveDialog() == JFileChooser.APPROVE_OPTION)return chooser.getSelectedFile()else if(chooser.showSaveDialog() == JFileChooser.CANCEL_OPTION)return nullelsereturn null}def filename = getFilename() as Stringdef outfile = new File(filename.replace('xlsx','txt'));def fp = outfile.newPrintWriter();println filenameXSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filename));(0..<wb.numberOfSheets).each{    XSSFSheet sheet = wb.getSheetAt(it)    int rows = sheet.physicalNumberOfRows    println " sheet:\"" + wb.getSheetName(it) + "\" has " + rows + " row(s)."    (0..<rows).each{ r ->        XSSFRow row = sheet.getRow(r)        if (row != null) {            int cells = row.physicalNumberOfCells            // println "\nROW " + row.rowNum + " has " + cells + " cell(s)."            (0..<cells).each{ c ->                XSSFCell cell = row.getCell(c)                if (cell == null)                fp.print '\t';                else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN)                fp.print cell.getBooleanCellValue()+'\t';                else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)                fp.print cell.getNumericCellValue()+'\t';                else                fp.print cell.getStringCellValue()+'\t';            }            fp.println '';        }    }  }fp.flush();fp.close();}}


GROOVY: poi read Excel XLSX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.