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 = "<p style='font-size:29.3px;text-align:center;font-family:方正小标宋简体;line-height:37.3px;margin: 0px 0px'>南海</p>"
				+ "		<h1 style='font-size:21.33px;text-align:left;font-family:黑体;font-weight:bold;line-height:37.3px;margin: 0px 0px'>南海介绍</h1>"
				+ "	<p style='font-size:21.33px;font-family:仿宋_GB2312;line-height:37.3px;font-family:仿宋_GB2312;margin: 0px 0px'>“涨海”之称一直延续到南北朝,而《梁书》卷54《海南诸国列传》:“干陁国在"
				+ "		南海洲上”(干陁国故地在今苏门答腊岛),已开始使用南海名称,至唐宋时期南海之称渐多,初唐被流放越南的诗人沈佺期有“身投"
				+ "		南海诸岛泛称“涨海崎头”、“珊瑚洲”;而以“磁石”指称暗礁暗滩,其含意是暗礁暗滩多,来往船只搁浅难脱,象被磁石吸住一样。</p>"
				+ "	<p style='font-size:21.33px;font-family:仿宋_GB2312;line-height:37.3px;font-family:仿宋_GB2312;margin: 25px 80px;text-indent:32px;'>"
				+ "		因此,中国必然要采取积极的应对措施,敦促相关国共同遵守《南海各方行为宣言》,并实质性进入南海,进行“南海海洋救助基地”等。</p>";

		String content = "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>"
				+ "<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office'"
				+ "xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml'"
				+ "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head>"
				+ "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
				+ "<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val='Cambria Math'/><m:brkBin m:val='before'/><m:brkBinSub m:val='--'/><m:smallFrac m:val='off'/><m:dispDef/><m:lMargin m:val='0'/> <m:rMargin m:val='0'/><m:defJc m:val='centerGroup'/><m:wrapIndent m:val='1440'/><m:intLim m:val='subSup'/><m:naryLim m:val='undOvr'/></m:mathPr></w:WordDocument></xml><![endif]-->"
				+ "<meta http-equiv=Content-Type content='text/html; charset=UTF-8'>"
				+ "<meta name=Generator content='Microsoft Word 14 (filtered)'>" + "</head>" + "<body lang=ZH-CN>"
				+ html 
				+ "</body>" + "</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表示设置文本前后距离。如下图。
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值