Apache - POI, how to get a whole content of file?

By : Oleg1n
Source: Stackoverflow.com
Question!

I try to read file .docx with help java api Apache POI. I use:

public static String view(String nameDoc){
    String text = null;
    try{
        XWPFDocument docx = new XWPFDocument(
                new FileInputStream(nameDoc));
        XWPFWordExtractor we = new XWPFWordExtractor(docx);
        text = we.getText();
        we.close();
        docx.close();
    }catch (Exception e){
        e.printStackTrace();
    }
    return text;
}

In this case i get only a text of file, but my file includes a text, table, pictures... How can i get full content of file?

By : Oleg1n


Answers
String contents = "";

     try {  
         System.out.println("Starting the test");  
         POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("D:/Resume.doc"));  
         HWPFDocument doc = new HWPFDocument(fs);  
         WordExtractor we = new WordExtractor(doc);  
         OutputStream file = new FileOutputStream(new File("D:/test.pdf")); 
         PdfWriter parser = PdfWriter.getInstance(doc, file);  
         parser.parse(); 
         PDDocument pdfDocument = parser.getPDDocument(); 
         PDFTextStripper stripper = new PDFTextStripper(); 
         contents = stripper.getText(pdfDocument); 
         pdfDocument.close();

     } catch (Exception e) {
        logger.error(e.getMessage());
     }

In contents you get full content of file.



Here is this code that i wrote for a project..the values outside for loop ( "WSDL Name" and "Tags" are the column header and list will contain values to be inserted). It will generate test.csv

import java.io.FileWriter;
import java.io.IOException;

public class genCSV
{
   public static void main(String [] args)
   {
       generateCsvFile("c:\\test.csv"); 
   }

   private static void generateCsvFile(String sFileName)
   {
    try
    {
        FileWriter writer = new FileWriter(sFileName);

    writer.append("WSDLName");
    writer.append("Tags");
    writer.append('\n');

   for(int i=0;i<list.size();i++){
       writer.append(list.getKey);
        writer.append(list.getValueForKey);
        writer.append('\n');
   }

    //generate whatever data you want

    writer.flush();
    writer.close();
}
catch(IOException e)
{
     e.printStackTrace();
} 
}
}
By : phoenix


There may be a chance of string value in the column RVU.MA. Try out with the below query to find out such descrepancies.

   SELECT *
   FROM RVU
   WHERE ISNUMERIC (RVU.MA)!=1


This video can help you solving your question :)
By: admin