`
bewithme
  • 浏览: 423268 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java xlsx转html

阅读更多

 

       poi-scratchpad  包中提供了ExcelToHtmlConverter工具类可以实现xls文件转换为html的功能,但是无法实现xlsx转html。

       本代码重写了一遍ExcelToHtmlConverter,实现了xlsx转html。入口类为XssfExcelToHtmlConverter

 可以直接使用这个类中的main方法进行测试。源码在附件中,只要修改相应的包名即可,还要引入poi-scratchpad 和poi包。

 

 public static void main( String[] args )throws IOException, ParserConfigurationException, TransformerException{
        
    	String inputPath="/Users/xuwenfeng/Desktop/testexcel.xlsx";
        String outputPath="/Users/xuwenfeng/Desktop/testexcel.html";
    	
        args=new String[]{inputPath,outputPath};
        
    	if ( args.length < 2 )
        {
            System.err.println( "Usage: ExcelToHtmlConverter <inputFile.xls> <saveTo.html>" );
            return;
        }

        System.out.println( "Converting " + args[0] );
        System.out.println( "Saving output to " + args[1] );

        Document doc = XssfExcelToHtmlConverter.process( new File( args[0] ) );

        DOMSource domSource = new DOMSource( doc );
        StreamResult streamResult = new StreamResult( new File(args[1]) );

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        // TODO set encoding from a command argument
        serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
        serializer.setOutputProperty( OutputKeys.INDENT, "no" );
        serializer.setOutputProperty( OutputKeys.METHOD, "html" );
        serializer.transform( domSource, streamResult );
    }

 

 

 


有任何问题请联系微信 

如果您觉得我的文章给了您帮助,请为我买一杯饮料吧!以下是我的支付宝,意思一下我将非常感激!
  
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics