需要导入几个包:
代码如下:
/**
* Word文档wps、2003,2007获取文本,待优化
* @param path
* @return
* @throws Exception
*/
public static String readWordFile(String path) throws Exception
{
String docContent = null;
FileInputStream in2003=null;
FileInputStream in2007=null;
try
{
in2003 = new FileInputStream(new File(path));
WordExtractor word2003 = new WordExtractor(in2003);
docContent = word2003.getText();
}
catch (OfficeXmlFileException e)
{
try
{
in2007 = new FileInputStream(new File(path));
XWPFDocument doc2007=new XWPFDocument(in2007);
XWPFWordExtractor word2007=new XWPFWordExtractor(doc2007);
docContent=word2007.getText();
/*或者
OPCPackage opcPack=POIXMLDocument.openPackage(path);
POIXMLTextExtractor text2=new XWPFWordExtractor(opcPack);
String text=text2.getText();
*/
}
catch (Exception e2) {e2.printStackTrace();}
}
catch (IOException e)
{
e.printStackTrace();
if(in2003!=null)
{
in2003.close();
}
}
finally
{
if(in2007!=null)
{
in2007.close();
}
}
System.out.println(docContent);
return docContent;
}
</pre><pre>
/**
* 判断文件后缀
* @param path
* @param target目标
* @return
*/
public static boolean isWord(String fileName,String target)
{
if(fileName.equals(""))
{
return false;
}
String prefix=fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
if(prefix.equals(target))
{
return true;
}
return false;
}
完整包: