用户登录
用户注册

分享至

java导出word印章 java导出word表格

  • 作者: 零116904441
  • 来源: 51数据库
  • 2020-04-21

首先我用的技术是 poi

这是代码,一个工具类得调用

public class WordUtil {

/**

* 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效

*

* @param templatePath

* 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件

* @param destFilePath

* 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx

* @param data

* 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同

*/

public static void exportWordByTemplate(String templatePath,

String destFilePath, Map<String, String> data) {

FileOutputStream out = null;

XWPFDocument doc = null;

try {

doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath));

List<XWPFRun> listRun;

List<XWPFParagraph> listParagraphs = doc.getParagraphs();

for (int i = 0; i < listParagraphs.size(); i++) {

listRun = listParagraphs.get(i).getRuns();

for (int j = 0; j < listRun.size(); j++) {

if (data.get(listRun.get(j).getText(0)) != null) {

String val = data.get(listRun.get(j).getText(0));

listRun.get(j).setText(val, 0);

}

}

}

File destFile = new File(destFilePath);

if (!destFile.getParentFile().exists()) {

destFile.getParentFile().mkdirs();

}

out = new FileOutputStream(destFilePath);

doc.write(out);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (out != null)

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 基于模板文件导出 word 文档,该方法支持03格式,但是此方法只能保留文档内容,不能保留文档中的样式和图片,建议将模板使用 07 的格式保存

*

* @param templatePath

* 模板文件的路径

* @param destFilePath

* 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.doc

* @param data

* 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同

*/

public static void export03WordByTemplate(String templatePath,

String destFilePath, Map<String, String> data) {

try {

WordExtractor doc = new WordExtractor(new FileInputStream(

templatePath));

String content = doc.getText();

for (String key : data.keySet()) {

content = content.replaceAll(key, data.get(key));

}

byte b[] = content.getBytes();

ByteArrayInputStream bais = new ByteArrayInputStream(b);

POIFSFileSystem fs = new POIFSFileSystem();

DirectoryEntry directory = fs.getRoot();

directory.createDocument("WordDocument", bais);

FileOutputStream ostream = new FileOutputStream(destFilePath);

fs.writeFilesystem(ostream);

bais.close();

ostream.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws Exception {

Map<String, String> maps = new HashMap<String, String>();

maps.put("appellation", "万达公寓业主:");

maps.put(

"main_body",

"输出的内容");

maps.put("date", "2013年1月23日");

exportWordByTemplate("E:/sss 2.docx", "E:/test/test.doc", maps);

}

}

"E:/sss 2.docx 模板存放的地址。

E:/test/test.doc 新生成的地址。

阿帕奇的这个支持导PDF格式的吗

java word 导出怎么合并表头 Word poi

给你个例子

iText-2.1.5.jar、iTextAsian.jar、iText-rtf-2.1.4.jar

public String CreateWord() {

try {

Document document = new Document(PageSize.A4);

RtfWriter2.getInstance(document, new FileOutputStream("c:\\"));

document.open();

for (int i = 0; i < datalist.size(); i++) {

Lawmanager lawmanager = (Lawmanager) datalist.get(i);

Paragraph title = new Paragraph("行 政 执 法 人 员 登 记 表");

title.setAlignment(Element.ALIGN_CENTER);

title.setFont(new RtfFont("仿 宋__GB2312", 18, Font.BOLD, Color.BLACK));

document.add(title);

Table table = new Table(7);

table.setWidth(100);

table.setBorderWidth(1);

table.setBorderColor(Color.BLACK);

table.setPadding(0);

table.setSpacing(0);

table.setAlignment("CENTER");

table.setWidths(new float[]{16.0f,16.0f,15.0f,15.0f,16.0f,22.0f,0.0f});

table.setAutoFillEmptyCells(true);

RtfFont font = new RtfFont("仿 宋__GB2312", 12, Font.COURIER, Color.BLACK);

Cell cell = new Cell(new Phrase("姓 名", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(lawmanager.getLawname(), font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setColspan(2);

table.addCell(cell);

cell = new Cell(new Phrase("性 别", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(lawmanager.getLawsex(), font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

try {

Image lawPic = Image.getInstance(publicTools.getPath() + "/" + lawmanager.getLawpic());

lawPic.scaleAbsolute(105, 135);

cell = new Cell(lawPic);

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setRowspan(4);

table.addCell(cell);

} catch (IOException e) {

publicTools.OutputLog(e.toString(), "ERROR", 0);

Image lawPic = Image.getInstance(publicTools.getPath("upfile/pic") + "pic.jpg");

lawPic.scaleAbsolute(105, 135);

cell = new Cell(lawPic);

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setRowspan(4);

table.addCell(cell);

}

cell = new Cell("\n\n");

cell.setBorderColor(new Color(255, 255, 255));

cell.setBackgroundColor(new Color(255, 255, 255));

table.addCell(cell);

cell = new Cell(new Phrase("出生年月日", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(lawmanager.getLawbirthdate(), font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setColspan(2);

table.addCell(cell);

cell = new Cell(new Phrase("民 族", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(lawmanager.getVolkdictionaryname(), font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell("\n\n");

cell.setBorderColor(new Color(255, 255, 255));

cell.setBackgroundColor(new Color(255, 255, 255));

table.addCell(cell);

cell = new Cell(new Phrase("工作单位", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(" " + lawmanager.getDeptname(), font));

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setColspan(4);

table.addCell(cell);

cell = new Cell("\n\n");

cell.setBorderColor(new Color(255, 255, 255));

cell.setBackgroundColor(new Color(255, 255, 255));

table.addCell(cell);

cell = new Cell(new Phrase("职 务", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(lawmanager.getPostdictionaryname(), font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase("参加工作时间", font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

table.addCell(cell);

cell = new Cell(new Phrase(lawmanager.getLawworkdate(), font));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

cell.setColspan(2);

table.addCell(cell);

cell = new Cell("\n\n");

cell.setBorderColor(new Color(255, 255, 255));

cell.setBackgroundColor(new Color(255, 255, 255));

table.addCell(cell);

document.add(table);

Phrase phrase = new Phrase("\n注:如未取得行政执法证件、行政执法资格的,在表中“行政执法证件编号”、“行政执法资格编号”栏中显示“0”。", new RtfFont("黑 体", 10, Font.TIMES_ROMAN, Color.red));

document.add(phrase);

if(i < datalist.size() - 1){

document.newPage();

}

}

document.close();

publicTools.insertLog("5", "下载Word文件", userSession);

} catch (FileNotFoundException e) {

publicTools.OutputLog(e.toString(), "ERROR", 0);

e.printStackTrace();

} catch (DocumentException e) {

publicTools.OutputLog(e.toString(), "ERROR", 0);

e.printStackTrace();

} catch (MalformedURLException e) {

publicTools.OutputLog(e.toString(), "ERROR", 0);

e.printStackTrace();

} catch (IOException e) {

publicTools.OutputLog(e.toString(), "ERROR", 0);

e.printStackTrace();

}

return "upfile/download/" + fileName;

}

java编程:有人做过导出word文件,并且word中附带excel文件的那种模式吗?

假如是.docx文件,用XWPFDocument 这个就可以,poi里面的类

然后是XWPFTable,XWPFTableRow,XWPFTableCell类。

分别获得表格,表格的行,表格的格

doc就多了,网上随便找

但是假如又有段落还有表格,我也卡在这了,假如找到方法,麻烦提醒我一下!!

java导出word之后,打开word之后

给出这个图片的话,只能自己去找了,按详细信息找。

建议把怎么实现导出的代码贴上来

setContentType中改成application/msword试一下

改成它也不行

你是要实现功能导出,

还是在浏览器中打开,再另存为,再用word打开?---如果是这种就按一图的不加

请教java html导出word如何实现

java将html导出word不用忘记<html></html>这对标签

//换页

<span style='font-size:16px;line-height:150%;font-family:"Times New Roman";

mso-fareast-font-family:宋体;mso-font-kerning:1px;mso-ansi-language:EN-US;

mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='mso-special-character:page-break;page-break-before:always'>

</span>

//换行

<p style='line-height:150%'><span style='font-size:16px;line-height:150%'><o:p> </o:p></span></p>

查看的话 打开word 视图——页面 就能看出看出效果

[java] view plain copy print?

ArrayList records = form.getRecords();//获取数据库数据

if(null!=records&&0!=records.size()){

//html拼接出word内容

String content="<html>";

for (int i = 0; i < records.size(); i++) {

Record record =(Record) records.get(i);

//从数据库中获得数据,将oracle中的clob数据类型转换成string类型

Method method = record.get("CONTENT").getClass().getMethod("getVendorObj",new Class[]{});

CLOB clob = (CLOB)method.invoke(record.get("CONTENT"));

String cx = clob.getSubString((long) 1, (int) clob.length());

String title= (String) record.get("TITLE");

//html拼接出word内容

content+="<div style=\"text-align: center\"><span style=\"font-size: 24px\"><span style=\"font-family: 黑体\">"+title+"<br /> <br /> </span></span></div>";

content+="<div style=\"text-align: left\"><span >"+cx+"<br /> <br /> </span></span></div>";

//插入分页符

content+="<span lang=EN-US style='font-size:16px;line-height:150%;mso-fareast-font-family:宋体;mso-font-kerning:1px;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'></span>";

content+="<p class=MsoNormal style='line-height:150%'><span lang=EN-US style='font-size:16px;line-height:150%'><o:p> </o:p></span></p>";

}

content += "</html>";

byte b[] = content.getBytes();

ByteArrayInputStream bais = new ByteArrayInputStream(b);

POIFSFileSystem poifs = new POIFSFileSystem();

DirectoryEntry directory = poifs.getRoot();

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

//输出文件

String name="导出知识";

response.reset();

response.setHeader("Content-Disposition",

"attachment;filename=" +

new String( (name + ".doc").getBytes(),

"iso-8859-1"));

response.setContentType("application/msword");

OutputStream ostream = response.getOutputStream();

//输出文件的话,new一个文件流

//FileOutputStream ostream = new FileOutputStream(path+ fileName);

poifs.writeFilesystem(ostream);

ostream.flush();

ostream.close();

bais.close();

java中如何导出word

像javareport它可以支持各种文档的导出。但是现在还在开发中。

转载请注明出处51数据库 » java导出word印章 java导出word表格

软件
前端设计
程序设计
Java相关