java使用poi导出word

1.代码如下

package doc;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExportDoc {
	public static void main(String[] args) throws Exception {
		//要导出的内容。若没有样式,可以自己使用标签进行拼接
		String html = "

南海

" + "

南海介绍

" + "

“涨海”之称一直延续到南北朝,而《梁书》卷54《海南诸国列传》:“干陁国在" + " 南海洲上”(干陁国故地在今苏门答腊岛),已开始使用南海名称,至唐宋时期南海之称渐多,初唐被流放越南的诗人沈佺期有“身投" + " 南海诸岛泛称“涨海崎头”、“珊瑚洲”;而以“磁石”指称暗礁暗滩,其含意是暗礁暗滩多,来往船只搁浅难脱,象被磁石吸住一样。

" + "

" + " 因此,中国必然要采取积极的应对措施,敦促相关国共同遵守《南海各方行为宣言》,并实质性进入南海,进行“南海海洋救助基地”等。

"; String content = "" + "" + "" + "" + "" + "" + "" + "" + "" + html + "" + ""; System.out.println(content); byte b[] = content.getBytes("UTF-8"); ByteArrayInputStream bai = new ByteArrayInputStream(b); POIFSFileSystem poifsFileSystem = new POIFSFileSystem(); DirectoryEntry directoryEntry = poifsFileSystem.getRoot(); directoryEntry.createDocument("WordDocument", bai); File file = new File("C:\\Users\\hasee\\Desktop\\导出的文档.doc"); FileOutputStream oStream = new FileOutputStream(file.getPath()); System.out.println(file.getPath()); poifsFileSystem.writeFilesystem(oStream); bai.close(); oStream.close(); } }

2.详细样式介绍

text-align:center:对其方式,中心对齐
text-indent:32px:首行缩进,也可以写成text-indent:2em
line-hegiht:设置行间距
font-family:方正小标宋简体 :设置字体,若word没有这种字体需要下载
margin: 25px 80px :25px表示设置段前、段后 距离。在line-hegiht相同的情况下,段前、段后距离会影响行间距;80px表示设置文本前后距离。如下图。
java使用poi导出word_第1张图片

你可能感兴趣的:(java,java)