Java poi设置打开模式_java使用poi将html导出word,默认打开页面视图

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

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.springframework.stereotype.Service;

/**

* @ClassName: HtmlToWord

* @Description: TODO(将html文件导出为word)

* @author xsw

* @date 2016-12-28 上午11:41:19

*

*/

@Service

public class HtmlToWord {

/**

*

*(srcPath html文件 fileName保存的doc文件)

* @param TODO

* @return void 返回类型

* @author xsw

* @2016-12-28上午11:47:27

*/

public void htmlToWord(String srcPath,String fileName) throws Exception {

ByteArrayInputStream bais = null;

FileOutputStream fos = null;

try {

if (!"".equals(fileName)) {

File fileDir = new File(fileName);

if (fileDir.exists()) {

String content = readFile(srcPath);

byte b[] = content.getBytes();

bais = new ByteArrayInputStream(b);

POIFSFileSystem poifs = new POIFSFileSystem();

DirectoryEntry directory = poifs.getRoot();

DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);

fos = new FileOutputStream(fileName);

poifs.writeFilesystem(fos);

bais.close();

fos.close();

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if(fos != null) fos.close();

if(bais != null) bais.close();

}

}

/**

* 读取html文件到字符串

* @param filename

* @return

* @throws Exception

*/

public String readFile(String filename) throws Exception {

StringBuffer buffer = new StringBuffer("");

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(filename));

buffer = new StringBuffer();

while (br.ready())

buffer.append((char) br.read());

} catch (Exception e) {

e.printStackTrace();

} finally {

if(br!=null) br.close();

}

return buffer.toString();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值