1 Packagecom.linkonworks.df.busi.utils;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 ImportJava.io.StringWriter;8 9 Importjavax.xml.parsers.DocumentBuilderFactory;Ten Importjavax.xml.parsers.ParserConfigurationException; One ImportJavax.xml.transform.OutputKeys; A ImportJavax.xml.transform.Transformer; - Importjavax.xml.transform.TransformerException; - Importjavax.xml.transform.TransformerFactory; the ImportJavax.xml.transform.dom.DOMSource; - ImportJavax.xml.transform.stream.StreamResult; - - ImportOrg.apache.poi.hssf.converter.ExcelToHtmlConverter; + ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; - Importorg.apache.poi.openxml4j.exceptions.InvalidFormatException; + ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; A at - /** - * <p> - * @ClassName: Filepreview - * </p> - * <p> in * description:excel file to HTML element - * </p> to * + * @authorZhou Xuan - * @date December 21, 2015 the */ * Public classFilepreview { $ /**Panax Notoginseng * <p> - * title:convertexceltohtml the * </p> + * <p> A * Description: Convert version 2003 Excel file to HTML tag element the * </p> + * - * @authorZhou Xuan $ * @paramExcelfile $ * @return - * @throwsIOException - * @throwsparserconfigurationexception the * @throwstransformerexception - * @throwsinvalidformatexceptionWuyi */ the Public StaticString convertexceltohtml (String path)throwsioexception,parserconfigurationexception, transformerexception,invalidformatexception { -Hssfworkbook WorkBook =NULL; WuString content =NULL; -StringWriter writer =NULL; AboutFile Excelfile =NewFile (path); $InputStream is =NewFileInputStream (excelfile); - //determine if the Excel file is version 2003 or version 2007 -String suffix = path.substring (Path.lastindexof ("."))); - if(Suffix.equals (". xlsx"))){ A //Convert version 07 to version 03 +XSSF2HSSF Xlsx2xls =NewXSSF2HSSF (); theXssfworkbook Xssfworkbook =NewXssfworkbook (IS); -WorkBook =NewHssfworkbook (); $ XLSX2XLS.TRANSFORMXSSF (Xssfworkbook, workBook); the the}Else{ theWorkBook =NewHssfworkbook (IS); the } - Try { inExceltohtmlconverter converter =NewExceltohtmlconverter (Documentbuilderfactory.newinstance (). Newdocumentbuilder (). NewDocument ()); theConverter.setoutputcolumnheaders (false);//header for columns not displayed theConverter.setoutputrownumbers (false);//headers that do not display rows About Converter.processworkbook (workBook); the thewriter =NewStringWriter (); theTransformer serializer =transformerfactory.newinstance (). Newtransformer (); +Serializer.setoutputproperty (outputkeys.encoding, "UTF-8"); -Serializer.setoutputproperty (outputkeys.indent, "yes"); theSerializer.setoutputproperty (Outputkeys.method, "HTML");BayiSerializer.transform (NewDomsource (Converter.getdocument ()), the NewStreamresult (writer)); the -Content =writer.tostring (); - writer.close (); the}finally { the Try { the if(Is! =NULL) { the is.close (); - } the if(Writer! =NULL) { the writer.close (); the }94}Catch(IOException e) { the e.printstacktrace (); the } the }98 returncontent; About } - 101 102 103}
Excel file Preview in Java Project