java swing word_现在要用Java Swing写一个可以在swing中打开office的软件,目前只要打开显示出来就行...

展开全部

先不说swing,首先你要做的32313133353236313431303231363533e4b893e5b19e31333262366439是在Java中把office的文档读出来,这里通常需要使用的是poi.

以word为例。

public class WordExtractor {

public WordExtractor() {

}

public String extractText(InputStream in) throws IOException {

ArrayList text = new ArrayList();

POIFSFileSystem fsys = new POIFSFileSystem(in);

DocumentEntry headerProps = (DocumentEntry) fsys.getRoot().getEntry("WordDocument");

DocumentInputStream din = fsys.createDocumentInputStream("WordDocument");

byte[] header = new byte[headerProps.getSize()];

din.read(header);

din.close();

// Prende le informazioni dall'header del documento

int info = LittleEndian.getShort(header, 0xa);

boolean useTable1 = (info & 0x200) != 0;

//boolean useTable1 = true;

// Prende informazioni dalla piece table

int complexOffset = LittleEndian.getInt(header, 0x1a2);

//int complexOffset = LittleEndian.getInt(header);

String tableName = null;

if (useTable1) {

tableName = "1Table";

} else {

tableName = "0Table";

}

DocumentEntry table = (DocumentEntry) fsys.getRoot().getEntry(tableName);

byte[] tableStream = new byte[table.getSize()];

din = fsys.createDocumentInputStream(tableName);

din.read(tableStream);

din.close();

din = null;

fsys = null;

table = null;

headerProps = null;

int multiple = findText(tableStream, complexOffset, text);

StringBuffer sb = new StringBuffer();

int size = text.size();

tableStream = null;

for (int x = 0; x < size; x++) {

WordTextPiece nextPiece = (WordTextPiece) text.get(x);

int start = nextPiece.getStart();

int length = nextPiece.getLength();

boolean unicode = nextPiece.usesUnicode();

String toStr = null;

if (unicode) {

toStr = new String(header, start, length * multiple, "UTF-16LE");

} else {

toStr = new String(header, start, length, "ISO-8859-1");

}

sb.append(toStr).append(" ");

}

return sb.toString();

}

private static int findText(byte[] tableStream, int complexOffset, ArrayList text)

throws IOException {

//actual text

int pos = complexOffset;

int multiple = 2;

//skips through the prms before we reach the piece table. These contain data

//for actual fast saved files

while (tableStream[pos] == 1) {

pos++;

int skip = LittleEndian.getShort(tableStream, pos);

pos += 2 + skip;

}

if (tableStream[pos] != 2) {

throw new IOException("corrupted Word file");

} else {

//parse out the text pieces

int pieceTableSize = LittleEndian.getInt(tableStream, ++pos);

pos += 4;

int pieces = (pieceTableSize - 4) / 12;

for (int x = 0; x < pieces; x++) {

int filePos =

LittleEndian.getInt(tableStream, pos + ((pieces + 1) * 4) + (x * 8) + 2);

boolean unicode = false;

if ((filePos & 0x40000000) == 0) {

unicode = true;

} else {

unicode = false;

multiple = 1;

filePos &= ~(0x40000000); //gives me FC in doc stream

filePos /= 2;

}

int totLength =

LittleEndian.getInt(tableStream, pos + (x + 1) * 4)

- LittleEndian.getInt(tableStream, pos + (x * 4));

WordTextPiece piece = new WordTextPiece(filePos, totLength, unicode);

text.add(piece);

}

}

return multiple;

}

public static void main(String[] args){

WordExtractor w = new WordExtractor();

POIFSFileSystem ps = new POIFSFileSystem();

try{

File file = new File("C:\\test.doc");

InputStream in = new FileInputStream(file);

String s = w.extractText(in);

System.out.println(s);

}catch(Exception e){

e.printStackTrace();

}

}

}

class WordTextPiece {

private int _fcStart;

private boolean _usesUnicode;

private int _length;

public WordTextPiece(int start, int length, boolean unicode) {

_usesUnicode = unicode;

_length = length;

_fcStart = start;

}

public boolean usesUnicode() {

return _usesUnicode;

}

public int getStart() {

return _fcStart;

}

public int getLength() {

return _length;

}

}

内容都取出来了,在Swing文档上append String不就好了

那就有些麻烦了,像Eclipse这种做的这么好的编辑器,打开word文件都是直接启动office去打开的,你这个可能有些麻烦。

本回答被网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值