importorg.apache.poi.openxml4j.exceptions.OpenXML4JException;importorg.apache.poi.openxml4j.opc.OPCPackage;importorg.apache.poi.openxml4j.opc.PackageAccess;importorg.apache.poi.ss.usermodel.BuiltinFormats;importorg.apache.poi.ss.usermodel.DataFormatter;importorg.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;importorg.apache.poi.xssf.eventusermodel.XSSFReader;importorg.apache.poi.xssf.model.StylesTable;importorg.apache.poi.xssf.usermodel.XSSFCellStyle;importorg.apache.poi.xssf.usermodel.XSSFRichTextString;importorg.xml.sax.*;importorg.xml.sax.helpers.DefaultHandler;importjavax.xml.parsers.ParserConfigurationException;importjavax.xml.parsers.SAXParser;importjavax.xml.parsers.SAXParserFactory;importjava.io.IOException;importjava.io.InputStream;importjava.io.PrintStream;publicclass XLSX2CSV {
/**
* The type of the data value is indicated by an attribute on the cell. The
* value is usually in a "v" element within the cell.
*/enum xssfDataType {
BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER,}classMyXSSFSheetHandlerextendsDefaultHandler{
/**
* Table with styles
*/privateStylesTable stylesTable;/**
* Table with unique strings
*/privateReadOnlySharedStringsTable sharedStringsTable;/**
* Destination for data
*/privatefinalPrintStream output;/**
* Number of columns to read starting with leftmost
*/privatefinalint minColumnCount;// Set when V start element is seenprivateboolean vIsOpen;// Set when cell start element is seen;// used when cell close element is seen.private xssfDataType nextDataType;// Used to format numeric cell values.privateshort formatIndex;privateString formatString;privatefinalDataFormatter formatter;privateint thisColumn =-1;// The last column printed to the output streamprivateint lastColumnNumber =-1;// Gathers characters as they are seen.privateStringBuffer value;publicMyXSSFSheetHandler(StylesTable styles,ReadOnlySharedStringsTable strings,int cols,PrintStream target){
this.stylesTable = styles;this.sharedStringsTable = strings;this.minColumnCount = cols;this.output = target;this.value =newStringBuffer();this.nextDataType = xssfDataType.NUMBER;this.formatter =newDataFormatter();}publicvoidstartElement(String uri,String localName,String name,Attributes attributes)throwsSAXException{
if("inlineStr".equals(name