本文目录一览:

java导出word表格

首先我用的技术是 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, MapString, String data) {

FileOutputStream out = null;

XWPFDocument doc = null;

try {

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

ListXWPFRun listRun;

ListXWPFParagraph 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, MapString, 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 {

MapString, String maps = new HashMapString, 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 新生成的地址。

Java 将智能生成的试卷信息从数据库导出并生成word文档

直接在网上找个试卷模版,打开后另存为xml,重命名为***.jsp 在需要输出的地方使用%=user.name %输出,然后在末尾加这段代码:%@page contentType="application/msword; charset=UTF-8" %

注:xml格式的word文件中首行内容

?xml version="1.0" encoding="UTF-8" standalone="yes"?

必须保证在jsp文件的第一行,否则导出的word将无法打开,提示格式错误

java导出word文档,但当实例化一个table跟Cell的时候,居然编译不通过

package com.rye.test;

import java.awt.Color;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Font;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Table;

import com.lowagie.text.rtf.RtfWriter2;

/**

* 创建word文档 步骤:

* 1,建立文档

* 2,创建一个书写器

* 3,打开文档

* 4,向文档中写入数据

* 5,关闭文档

*/

public class WordDemo {

public WordDemo() {

}

/**

* @param args

*/

public static void main(String[] args) {

// 创建word文档,并设置纸张的大小

Document document = new Document(PageSize.A4);

try {

RtfWriter2.getInstance(document,

new FileOutputStream("E:/word.doc"));

document.open();

//设置合同头

Paragraph ph = new Paragraph();

Font f = new Font();

Paragraph p = new Paragraph("出口合同",

new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(0, 0, 0)) );

p.setAlignment(1);

document.add(p);

ph.setFont(f);

// 设置中文字体

// BaseFont bfFont =

// BaseFont.createFont("STSongStd-Light",

"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

// Font chinaFont = new Font();

/*

* 创建有三列的表格

*/

Table table = new Table(4);

document.add(new Paragraph("生成表格"));

table.setBorderWidth(1);

table.setBorderColor(Color.BLACK);

table.setPadding(0);

table.setSpacing(0);

/*

* 添加表头的元素

*/

Cell cell = new Cell("表头");//单元格

cell.setHeader(true);

cell.setColspan(3);//设置表格为三列

cell.setRowspan(3);//设置表格为三行

table.addCell(cell);

table.endHeaders();// 表头结束

// 表格的主体

cell = new Cell("Example cell 2");

cell.setRowspan(2);//当前单元格占两行,纵向跨度

table.addCell(cell);

table.addCell("1,1");

table.addCell("1,2");

table.addCell("1,3");

table.addCell("1,4");

table.addCell("1,5");

table.addCell(new Paragraph("用java生成的表格1"));

table.addCell(new Paragraph("用java生成的表格2"));

table.addCell(new Paragraph("用java生成的表格3"));

table.addCell(new Paragraph("用java生成的表格4"));

document.add(new Paragraph("用java生成word文件"));

document.add(table);

document.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

看看有没有导错包

导包快捷键 Ctrl + o

java导出word,默认打开时doc或者docx格式

到控制面板的功能和程序,点击2003,点击更改,修复,或者直接点击2003的安装程序进行修复也可以,这样默认就是2003,而docx只有2010才能打开。

java freemarker引擎 导出word文档

要生成的数据,在java后台必须是用Map格式,这个你检查一下有没有问题。如果有list,也要放到map里面去。

HashMapString, Object dataMap = new HashMapString, Object();

Configuration conf = new Configuration();

Template t = conf.getTemplate(fileName);

File docFile = new File(filePath + "/" + docName);

out = new OutputStreamWriter(new FileOutputStream(docFile), "UTF-8");

t.process(dataMap, out);

java程序导出word文档,需要样式设置为紧缩,跪求高手指点,代码如下,求补充

1-apache的POI,此方法对Excel的导出做的很好,目前对Word的导出方面的功能尚未完全。

2-纯JavaScript脚本实现。主要通过客户端调用本机Office组件来实现。

3-在JSP页面引入头文件实现。

纯JavaScript脚本实现细节方面大体是创建一个word组件ActiveXObject('Word.Application'),用js通过表ID取得表内容然后保存到word,要注意的是js实现有很多不好的地方,例如Internet选项需要把ActiveX空间全部启用,安全级别设置为中。这样的话岂不是每台机器都要配置一下。其次每次生成word文档以后弹出对话框(无法保存此文件,因为它已在别处打开(C:/.../STARTUP/Powerword.dot)),出现此问题就需要把C:/Documents and Settings/当前用户名/Application Data/Microsoft/Word/STARTUP下的Powerword.dot文件删除,每次遇到此问题就需要删除文件来解决,十分不方便。

JSP页面引入来实现Word保存就方便多了,但是也有不足的地方,首先如果需要引入

meta http-equiv="Content-Type" content="application/msword; charset=gb2312" /

如果需要下载的话就引入

%@ page contentType="application/msword; charset=gb2312" %

其实如果大家用框架做就方便多了,比如Struts2。在Action里直接写如下代码:

if(out!=null){

String fileName="";

fileName+="评价报告.doc";

try {

HttpServletResponse response = ServletActionContext.getResponse();

response.setHeader("Content-disposition","attachment; filename="+new String(fileName.getBytes("GB2312"), "8859_1"));

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

out是jsp页面表单元素,一个button,用于提交表单到相应Action进行Word下载。Action设置jsp页面头文件。这样每次点击button就可以把相应jsp页面的内容保存到Word中并且支持下载,Word中内容并且是可编辑状态。

不足的地方在于由于表内容是动态生成,有的需要先查看在下载Word,就需要另外建立一个新JSP页面进行Word下载,当然首先要在struts.xml里配置好页面转向。

新建立的页面传值同查看页面要保持一样。