public static void exportWordDoc(XWPFDocument doc , String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) { Assert.notNull(temDir,"临时文件路径不能为空"); Assert.notNull(fileName,"导出文件名不能为空"); Assert.isTrue(fileName.endsWith(".docx"),"word导出请使用docx格式"); if (!temDir.endsWith("/")){ temDir = temDir + File.separator; } File dir = new File(temDir); if (!dir.exists()) { dir.mkdirs(); } try { String userAgent = request.getHeader("user-agent").toLowerCase(); if (userAgent.contains("msie") || userAgent.contains("like gecko")) { fileName = URLEncoder.encode(fileName, "UTF-8"); } else { fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1"); } WordExportUtil.exportWord07(doc, params); String tmpPath = temDir + fileName; FileOutputStream fos = new FileOutputStream(tmpPath); doc.write(fos); // 设置强制下载不打开 response.setContentType("application/force-download"); // 设置文件名 response.addHeader("Content-Disposition", "attachment;fileName=" + fileName); OutputStream out = response.getOutputStream(); doc.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { delFileWord(temDir,fileName);//这一步看具体需求,要不要删 } }这块代码怎么将字体设置成仿宋
时间: 2024-02-14 18:28:19 浏览: 118
在上述代码中,你可以通过以下方式将字体设置为仿宋:
1. 导入必要的类:
```java
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
```
2. 获取文档的段落列表,并为每个段落设置字体样式:
```java
XWPFDocument doc = new XWPFDocument();
// ...
// 获取段落列表
List<XWPFParagraph> paragraphs = doc.getParagraphs();
// 遍历段落列表
for (XWPFParagraph paragraph : paragraphs) {
// 设置段落对齐方式为左对齐
paragraph.setAlignment(ParagraphAlignment.LEFT);
// 创建一个新的运行对象
XWPFRun run = paragraph.createRun();
// 设置字体为仿宋
run.setFontFamily("仿宋");
// 其他设置...
}
```
3. 调用导出方法将设置好字体的文档导出为Word文档:
```java
WordExportUtil.exportWord07(doc, params);
```
以上代码将会将整个文档中的字体设置为仿宋。你可以根据需要设置其他字体样式、段落样式等。请注意确保你的系统中存在仿宋字体。
阅读全文