官网
spire.office 官网
提供的网址有教如何导入maven,这里就省略了
效果
java 本地导出
@Test
public void testWord() {
try {
String date = "2019-10-30";
Document document = new Document();
Section section = document.addSection();
// 顶部标题样式
ParagraphStyle hTitleStyle = new ParagraphStyle(document);
hTitleStyle.setName("hTitleStyle");
hTitleStyle.getCharacterFormat().setBold(true);
hTitleStyle.getCharacterFormat().setFontName("宋体");
hTitleStyle.getCharacterFormat().setFontSize(28f);
hTitleStyle.getCharacterFormat().setTextBackgroundColor(Color.LIGHT_GRAY);
document.getStyles().add(hTitleStyle);
// 普通标题样式
ParagraphStyle titleStyle = new ParagraphStyle(document);
titleStyle.setName("titleStyle");
titleStyle.getCharacterFormat().setBold(true);
titleStyle.getCharacterFormat().setFontName("宋体");
titleStyle.getCharacterFormat().setFontSize(14f);
document.getStyles().add(titleStyle);
//正文样式
ParagraphStyle fontStyle = new ParagraphStyle(document);
fontStyle.setName("fontStyle");
fontStyle.getCharacterFormat().setFontName("宋体");
fontStyle.getCharacterFormat().setFontSize(12f);
document.getStyles().add(fontStyle);
//总标题
Paragraph pph1 = section.addParagraph();
pph1.appendText("spire office导出word文字段落和表格的模板");
pph1.applyStyle("hTitleStyle");
pph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
pph1.getFormat().setAfterSpacing(15f);
// 副标题 - 时间
//报表时间
StringBuffer titleDate = new StringBuffer();
SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日");
titleDate.append(sd.format(sd1.parse(date)));
Paragraph pph2 = section.addParagraph();
pph2.appendText(titleDate.toString());
pph2.applyStyle("titleStyle");
pph2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
pph2.getFormat().setAfterSpacing(15f);
// 1.总体进展
// 标题
Paragraph pph3 = section.addParagraph();
TextRange textRange = pph3.appendText("标题一:");
textRange.getCharacterFormat().setBold(true);
textRange.getCharacterFormat().setFontSize(14f);
//内容
pph3.appendText("不要温和地走进那良夜,老年应当在日暮时燃烧咆哮;咆哮吧咆哮,痛斥那光的退缩。");
pph3.getFormat().setAfterSpacing(5f);
// 2.
Paragraph pph5 = section.addParagraph();
textRange = pph5.appendText("标题二:");
textRange.getCharacterFormat().setBold(true);
textRange.getCharacterFormat().setFontSize(14f);
pph5.appendText("这是段文字用于测试内容");
pph5.getFormat().setAfterSpacing(5f);
// 表格
// 表头的数据
String[] interviewHeader = new String[]{"销售片区", "片区负责人", "任务栋数"};
Table table1 = section.addTable(true);
// 行 和 列
// 这里是先生成表格, 然后再定位表格行和列 , 插入数据
table1.resetCells(3, interviewHeader.length);
// 要合并单元格
// 合并列
table1.applyHorizontalMerge(2,0,1);
// 合并行
table1.applyVerticalMerge(2,1,2);
// 表头
TableRow row = table1.getRows().get(0);
row.isHeader(true);
row.setHeightType(TableRowHeightType.Exactly);
// 填充表头数据
for (int i = 0; i < interviewHeader.length; i++) {
putTableData(row, i, interviewHeader[i], null);
}
// 表体 - 数据填充
int j = 0;
TableRow dataRow = table1.getRows().get(1);
dataRow.setHeightType(TableRowHeightType.Exactly);
putTableData(dataRow, j++, "第一片区", null);
putTableData(dataRow, j++, "张三", null);
putTableData(dataRow, j++, "31", null);
// 充值
j = 0;
dataRow = table1.getRows().get(2);
dataRow.setHeightType(TableRowHeightType.Exactly);
// 最后一行
putTableData(dataRow, j, "总计", null);
//导出文档-静态代码
document.saveToFile("D:\\work\\2019\\idea\\company\\zq\\export\\导出word文档.docx", FileFormat.Docx);
document.dispose();
// 导出文档-web端
// document.saveToFile("导出word文档.docx", FileFormat.Docx);
// doExport(fileName, fileName, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 填充word表格数据
*/
private static void putTableData(TableRow row, int index, String data, Float width) {
row.getCells().get(index).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
if (null != width) {
row.getCells().get(index).setWidth(width);
}
Paragraph p = row.getCells().get(index).addParagraph();
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
p.applyStyle("fontStyle");
p.appendText(data);
}
web端导出的方法
private static void doExport(String aFileName, String aFilePath, HttpServletRequest request, HttpServletResponse response) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = new File(aFilePath);
try {
request.setCharacterEncoding("UTF-8");
String agent = request.getHeader("User-Agent").toUpperCase();
if ((agent.indexOf("MSIE") > 0) || ((agent.indexOf("RV") != -1) && (agent.indexOf("FIREFOX") == -1)))
aFileName = URLEncoder.encode(aFileName, "UTF-8");
else {
aFileName = new String(aFileName.getBytes("UTF-8"), "ISO8859-1");
}
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + aFileName);
response.setHeader("Content-Length", String.valueOf(file.length()));
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
bos.write(buff, 0, bytesRead);
logger.info("<--------------------------报表Word导出成功------------------------------>");
bos.flush();
} catch (Exception e) {
// TODO: handle exception
logger.error("报表导出word文件失败!:" + e.getMessage());
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
file.delete();
} catch (Exception e) {
logger.error("报表导出文件关闭流出错!", e);
}
}
}