HWPF操作Word
来源:互联网 发布:电脑视频剪辑软件中文 编辑:程序博客网 时间:2024/06/18 04:07
1、HWPFDocument通过读入一个已存在DOC文件,加载一个Word文档【似乎没有发现直接创建一个文档的方法】
如:
FileInputStream in = new FileInputStream("C:\\blank.doc");
HWPFDocument doc = new HWPFDocument(in);
2、Range、Paragraph、Section是操作Word重要的区域操作对象【具体说明待更新】
Range一般用于比较大的范围
Paragraph是一个段落
Section任意的范围如:Range range = doc.getRange();
3、Word有很多样式,CharacterProperties、ParagraphProperties【具体说明待更新】
其中:
CharacterProperties对应===>
ParagraphProperties对应===>
4、标题多级样式需要用HWPFList来实现【具体说明待更新】
下面是一个简单的例子,简单说明如何用HWPF操作Word的样式
引用一下:http://bbs.csdn.net/topics/290046899中6楼songnian1983的例子
结果在一个空白的文档输出一下内容:
例子代码引用:http://bbs.csdn.net/topics/290046899【6楼】
public static void main(String[] args) throws Exception {
try {
//加载一个空白的Word文档
FileInputStream in = new FileInputStream("file/123.doc");
HWPFDocument doc = new HWPFDocument(in);
//获取文档内容区域
Range range = doc.getRange();
//新建字体样式
CharacterProperties props = new CharacterProperties();
Range currentRange = range;
// Slowly increase the font size
for (int x = 8; x <= 64; x += 4) {
// Set the half point size of the font
props.setFontSize(x);
currentRange = currentRange.insertAfter(" Hello World!", props);
}
// Display Bold characters
props.setBold(true);
currentRange = currentRange.insertAfter(" Bold", props);
// Display Italic characters
props.setItalic(true);
currentRange = currentRange.insertAfter(" Italic", props);
// Display charcters with a Double Strikethrough
props.setDoubleStrikeThrough(true);
currentRange = currentRange.insertAfter(" Double Strikethrough",props);
// Insert an empty paragraph for readability
currentRange = currentRange.insertAfter(new ParagraphProperties(),0);
// Reset the character properties
props = new CharacterProperties();
props.setFontSize(32);
// Create a numbered list
HWPFList list = new HWPFList(true,doc.getStyleSheet());
int listID = doc.registerList(list);
/**
*底层包poi-scratchpad-3.9-20121203.jar
*类org.apache.poi.hwpf.model.PlfLfo @方法 void add( LFO lfo, LFOData lfoData)
*_rgLfo[_lfoMac + 1] = lfo;
*_rgLfoData[_lfoMac + 1] = lfoData;
*这两句代码有明显BUG,需修改之
**/
// Insert a list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
props.setIco24(0xff0000);
currentRange = currentRange.insertAfter(" Blue list entry", props);
// Insert another list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(),
listID, 1, 0);
props.setIco24(0xff);
props.setFontSize(38);
props.setCapitalized(true);
currentRange = currentRange.insertAfter(" larger red capitalized",props);
// Last list entry
currentRange = currentRange.insertAfter(new ParagraphProperties(),listID, 1, 0);
props.setIco24(0);
props.setCapitalized(false);
props.setCharacterSpacing(150);
props.setOutline(true);
currentRange = currentRange.insertAfter(" Large character spacing",props);
// Write out the document
FileOutputStream out = new FileOutputStream("file/hello.doc");
doc.write(out);
out.flush();
out.close();
}
catch (Throwable t){
t.printStackTrace();
}
}
- HWPF操作Word
- Apache POI库简化,仅保留hwpf部分(word文档处理,可用于android)
- Apache POI库简化,仅保留hwpf部分(word文档处理,可用于android)
- 操作WORD
- Word操作
- 操作word
- word操作
- word 操作
- word操作
- Word操作
- Word 操作
- 操作Word
- VC操作操作Word
- WORD VBA 操作WORD 文本框
- POI组件介绍:HWPF 和 XWPF
- lotusscript操作word文件
- 操作Word的例子
- Java操作Excel,Word
- Galileo Ganymede Europa 代表不同的版本
- cocos2d-x在win32测试时响应键盘消息
- 如何创建一个链表
- 数组定义和值初始化的语法形式混淆
- [Qt5.1]libusb-win32应用
- HWPF操作Word
- mysql查询命令
- Oracle查看当前登陆的用户
- java 学习
- 如何成为Android高手
- css和js文件的架构设计及优化
- 分清函数指针和指针函数
- POJ 2676 && POJ 2918 数独 DFS
- java数组乱序