Convert DOC file to DOCX with Java

前端 未结 7 1954
天涯浪人
天涯浪人 2021-02-20 12:43

I need to use DOCX files (actually the XML contained in them) in a Java software I\'m currently developing, but some people in my company still use the DOC format.

Do yo

7条回答
  •  余生分开走
    2021-02-20 13:45

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;
    
    
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.extractor.WordExtractor;
    
    import org.apache.poi.hwpf.usermodel.Range;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    
    
    public class TestCon {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            POIFSFileSystem fs = null;  
            Document document = new Document();
    
            try {  
                System.out.println("Starting the test");  
                fs = new POIFSFileSystem(new FileInputStream("C:/Users/312845/Desktop/a.doc"));  
    
                HWPFDocument doc = new HWPFDocument(fs);  
                WordExtractor we = new WordExtractor(doc);  
    
                OutputStream file = new FileOutputStream(new File("C:/Users/312845/Desktop/test.docx")); 
    
                System.out.println("Document testing completed");  
            } catch (Exception e) {  
                System.out.println("Exception during test");  
                e.printStackTrace();  
            } finally {  
                // close the document  
                document.close();  
            }  
        }  
    }
    

提交回复
热议问题