java xlsx transform html

 

       The ExcelToHtmlConverter tool class provided in the poi-scratchpad package can realize the function of converting xls files to html, but cannot realize xlsx to html.

       This code rewrites ExcelToHtmlConverter to realize xlsx to html. The entry class is XssfExcelToHtmlConverter

 You can use the main method in this class directly for testing. The source code is in the attachment, just modify the corresponding package name, and also import the poi-scratchpad and poi packages.

 

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 );
    }

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326188148&siteId=291194637
Recommended