Excel file Preview in Java Project

Source: Internet
Author: User

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

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.