导航:首页 > 文件教程 > java抽取word

java抽取word

发布时间:2024-07-20 00:30:43

java有什么好的方法可以将word里的文本读取出来

你用免费版的Free Spire.Doc for Java可以直接读取Word文档里面的文本,参考代码

import com.spire.doc.Document;

import java.io.FileWriter;

import java.io.IOException;

public class ExtractText {

public static void main(String[] args) throws IOException {

//加载Word文档

Document document = new Document();

document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");

//获取文档中的文本保存为String

String text=document.getText();

//将String写入Txt文件

writeStringToTxt(text,"ExtractedText.txt");

}

public static void writeStringToTxt(String content, String txtFileName) throws IOException {

FileWriter fWriter= new FileWriter(txtFileName,true);

try {

fWriter.write(content);

}catch(IOException ex){

ex.printStackTrace();

}finally{

try{

fWriter.flush();

fWriter.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

参考自官网原文。

㈡ java word中提取关键字

给个思路吧。
读取word用doc4j,然后就是读成字符串进行处理了。
提取关键字首先是中文分词技术,就是把一段话划分成多个组成的词语,然后统计词语的出现次数,这个是主要依据。这个是有实现的jar包的,可以去搜,搜java 中文分词就行。
分词之后,记录词语出现位置,这个是辅助的依据,记录词语一句话中的位置,越靠前越像关键字,权重越高。
甚至可能需要建立一个权重体系,次数设置一个权重,整体位置设置一个权重,不同位置权重也不同。不了解权重可以理解成系数(百分比的,然后计算那个词是关键词)。

同时需要注意,可能需要排除一些常用词,哪些次需要排除,这个需要根据程序反复运行,读取不同word文章的结果来定。
不明白的话在问吧。

㈢ 怎么用java导出word

java导出word代码如下:

package com.bank.util;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class WordTools {
public void createDocContext(String file) throws DocumentException,
IOException {
// 设置纸张大小
Document document = new Document(PageSize.A4);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
// 设置中文字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 标题字体风格
Font titleFont = new Font(bfChinese, 12, Font.BOLD);
// 正文字体风格
Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
Paragraph title = new Paragraph("标题");
// 设置标题格式对齐方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString = "iText是一个能够快速产生PDF文件的java类库。"
+ " \n"// 换行
+ "iText的java类对于那些要产生包含文本,"
+ "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。"
+ "使用iText与PDF能够使你正确的控制Servlet的输出。";
Paragraph context = new Paragraph(contextString);
// 正文格式左对齐
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
// 离上一段落(标题)空的行数
context.setSpacingBefore(5);
// 设置第一行空的列数
context.setFirstLineIndent(20);
document.add(context);
//利用类FontFactory结合Font和Color可以设置各种各样字体样式
/**
* Font.UNDERLINE 下划线,Font.BOLD 粗体
*/
Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE,
new Color(0, 0, 255)));
document.add(underline);

// 设置 Table 表格
Table aTable = new Table(3);
int width[] = {25,25,50};
aTable.setWidths(width);//设置每列所占比例
aTable.setWidth(90); // 占页面宽度 90%
aTable.setAlignment(Element.ALIGN_CENTER);//居中显示
aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
aTable.setAutoFillEmptyCells(true); //自动填满
aTable.setBorderWidth(1); //边框宽度
aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色
aTable.setPadding(0);//衬距,看效果就知道什么意思了
aTable.setSpacing(0);//即单元格之间的间距
aTable.setBorder(2);//边框
//设置表头
/**
* cell.setHeader(true);是将该单元格作为表头信息显示;
* cell.setColspan(3);指定了该单元格占3列;
* 为表格添加表头信息时,要注意的是一旦表头信息添加完了之后, \
* 必须调用 endHeaders()方法,否则当表格跨页后,表头信息不会再显示
*/
Cell haderCell = new Cell("表格表头");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese ));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0, 0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new Cell("#1"));
aTable.addCell(new Cell("#2"));
aTable.addCell(new Cell("#3"));
aTable.addCell(new Cell("#4"));
Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese ));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new Paragraph("\n"));
//添加图片
// Image img=Image.getInstance("http://127.0.0.1:8080/testSystem/images/1_r1_c1.png");
// img.setAbsolutePosition(0, 0);
// img.setAlignment(Image.RIGHT);//设置图片显示位置
// img.scaleAbsolute(12,35);//直接设定显示尺寸
// img.scalePercent(50);//表示显示的大小为原尺寸的50%
// img.scalePercent(25, 12);//图像高宽的显示比例
// img.setRotation(30);//图像旋转一定角度
// document.add(img);
document.close();
}
public static void main(String[] args){
WordTools b=new WordTools();
try {
b.createDocContext("d:/demo.doc");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

㈣ 紧急求助如何用java实现word文档的导入,请大家踊跃发言,谢谢大家了!!!

环境支持

1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/

1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件

package com.ray.poi.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.textmining.text.extraction.WordExtractor;

/**
* 读写doc
* @author wangzonghao
*
*/
public class POIWordUtil {
/**
* 读入doc
* @param doc
* @return
* @throws Exception
*/
public static String readDoc(String doc) throws Exception {
// 创建输入流读取DOC文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对DOC文件进行提取
text = extractor.extractText(in);
return text;
}
/**
* 写出doc
* @param path
* @param content
* @return
*/
public static boolean writeDoc(String path, String content) {
boolean w = false;
try {

// byte b[] = content.getBytes("ISO-8859-1");
byte b[] = content.getBytes();

ByteArrayInputStream s = new ByteArrayInputStream(b);

POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();

DocumentEntry de = directory.createDocument("WordDocument", s);

FileOutputStream ostream = new FileOutputStream(path);

fs.writeFilesystem(ostream);

s.close();
ostream.close();

} catch (IOException e) {
e.printStackTrace();
}
return w;
}

}
测试

package com.ray.poi.util;

import junit.framework.TestCase;

public class POIUtilTest extends TestCase {

public void testReadDoc() {
try{
String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc");
System.out.println(text);
}catch(Exception e){
e.printStackTrace();
}

}

public void testWriteDoc() {
String wr;
try {
wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc");

boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

㈤ java如何做用户自定义模版,导出word

用freemarker,可以自定义模板,然后用变量替换的方式。你可以看一下这篇文章(不是我写的)http://www.havenliu.com/java/514.html。
其实word是支持html的, 你可以试试把.html的文件名改成.doc。

㈥ java 怎么读取服务器上的word文件中的内容

通过流来读取,例如:

阅读全文

与java抽取word相关的资料

热点内容
手机锁屏后的文件在哪里 浏览:282
枪神纪母猴飞天教程 浏览:516
快捷建立文件夹 浏览:477
exe视频文件没有注册类 浏览:451
unix批量删除文件名前缀 浏览:541
k750s升级bios 浏览:147
学韩语用什么app 浏览:478
110固态硬盘分区win10 浏览:317
怎么恢复系统文件 浏览:358
数据线转换器多少钱 浏览:274
美国队长qq皮肤多少钱 浏览:630
win8word文档在哪 浏览:180
甘肃省人事局文件在哪里 浏览:689
spss安装输入代码 浏览:546
网络语言知乎 浏览:596
iphoneicloud无法显示 浏览:112
ict程序包 浏览:729
java有哪些条件语句 浏览:345
冒险岛120级去哪里升级 浏览:511
手机输入法声音文件 浏览:876

友情链接