java 导出word文档

需要的jar包:itext-2.1.7.jar 和  itext-rtf-2.1.7.jar       【下载

 

package test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
 
 
 
public class GetWord {
 private static Document document;
 private static BaseFont baseFont;
 
//创建word,并设置纸张文档
 
 private static void openWordFile(String fileName) throws DocumentException,
   IOException {
  document = new Document(PageSize.A4);
  RtfWriter2.getInstance(document, new FileOutputStream(fileName));
  document.open();
  baseFont = BaseFont.createFont();
 }
 
//设置标题
 
 private static boolean setTitle(String title) throws DocumentException {
  Font font = new Font(baseFont, 12, Font.BOLD);
  Paragraph pTitle = new Paragraph(title + "\n");
  pTitle.setFont(font);
  pTitle.setAlignment(Element.ALIGN_CENTER);
 
  return document.add(pTitle);
 
 }
 
//设置文档内容
 
 private static boolean setContent(String content) throws DocumentException {
  Font font = new Font(baseFont, 10, Font.NORMAL);
  Paragraph pContent = new Paragraph(content);
  pContent.setFont(font);
  pContent.setAlignment(Element.ALIGN_LEFT);
  pContent.setSpacingAfter(5);
  pContent.setFirstLineIndent(20);
  return document.add(pContent);
 }
 
//对外使用的接口
 
 public static boolean CreateWordFile(String url, String title,
   List<String> contents) {
  boolean returnValue = false;
  try {
   openWordFile(url);
   returnValue = setTitle(title);
   for (int i = 0; i < contents.size(); i++) {
    returnValue = returnValue && setContent(contents.get(i));
   }
   document.close();
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  return returnValue;
 
 }
 
//对外使用的接口
 
 public static boolean CreateWordFile(String url, String title,
   Map<problem,List<options>> contents) {
  boolean returnValue = false;
  try {
   openWordFile(url);
   returnValue = setTitle(title);
   
   Integer num = 1;
   for (problem pr : contents.keySet()) {
	   String str = num +"、 "+pr.getQuestion();
	  returnValue = returnValue && setContent(str);
		if (contents.get(pr) != null) {
			for (options op : contents.get(pr)) {
				str = "   " + op.getName() + ": " + op.getContents();
				returnValue = returnValue && setContent(str);
			}
		}else{
			returnValue = returnValue && setContent(null) && setContent(null);
		}
	  num++;
	  returnValue = returnValue && setContent(null);
  }
   document.close();
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  return returnValue;
 
 }
 
 //对外使用的接口
 public static boolean CreateWordFile(String url, String title,
   String content) {
  boolean returnValue = false;
  try {
   openWordFile(url);
   returnValue = setTitle(title);
   returnValue = returnValue && setContent(content);
   document.close();
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  return returnValue;
 
 }
 
public static void main(String[] args ) {
  GetWord wordUtil = new GetWord();
  List<String> strList = new ArrayList<String>();
  strList.add("哈哈哈哈");
  strList.add("呵呵呵呵呵");
  
  Map<problem,List<options>> contents =  new HashMap<problem, List<options>>();
  List<options> olist = new ArrayList<options>();
  olist.add(new options("A", "呵呵 不知道"));
  olist.add(new options("B", "你说过,末日后,一起"));
  
  contents.put(new problem("你喜欢我吗?","b"), olist);
  List<options> list = new ArrayList<options>();
  list.add(new options("A", "呵呵 不知道"));
  list.add(new options("B", "你说过,末日后,一起"));
  list.add(new options("C", "你下吧 ,我不想说了"));
  list.add(new options("D", "或许我们真的不合适"));
  contents.put(new problem("我们之间存在感情吗?","b"), list);
  contents.put(new problem("请解释,爱情到底是什么?","b"), null);
  
//传入内容为String
  wordUtil.CreateWordFile("e:\\word.doc", "感情调查", "我说过的、哼");

//传入内容为map
  wordUtil.CreateWordFile("e:\\word-map.doc", "感情调查", contents);
 
//传入内容为List
  wordUtil.CreateWordFile("e:\\word-list.doc", "标题居中", strList);
 
 }
}

class options{
	private String name;
	private String contents;

	public options(String name, String content){
		this.contents = content;
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContents() {
		return contents;
	}
	public void setContents(String contents) {
		this.contents = contents;
	}
}

class problem{
	private String question;
	private String answer;

	public problem(String q,String a){
		this.question = q;
		this.answer = a;
	}
	
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值