使用itext导出word文档

1 篇文章 0 订阅
1 篇文章 0 订阅
package com.aspsystem.util;

import java.io.FileOutputStream;
import java.util.List;

import com.aspsystem.entity.Exam;
import com.aspsystem.entity.Questions;
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.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;

public class WordUtil {
	private Exam exam;
	private List<Questions> questions;
	private String separator;
	private String Path;
	private int i=0;
	private String type;

	
	public WordUtil(){}
	
	public WordUtil(Exam exam,List<Questions> questions,String separator,String Path){
		this.exam=exam;
		this.questions=questions;
		this.separator=separator;
		this.Path=Path;
	}
	
	public void WriteWord(){
		Document document ;
		try {
		document = new Document(PageSize.A2.rotate(),80,80,80,80);
		RtfWriter2.getInstance(document, new FileOutputStream(Path+exam.getExam_wordPath()+".doc"));		
		document.open();
		Font titleFont = new Font(Font.STRIKETHRU,14, Font.BOLD);
		/* 设置标题1格式 */
		RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_NORMAL;
		rtfGsBt1.setAlignment(Element.ALIGN_CENTER);
		rtfGsBt1.setStyle(Font.BOLD);
		rtfGsBt1.setSize(12);
		/* 设置正文格式 */
		RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_2;
		rtfGsBt2.setAlignment(Element.ALIGN_LEFT);
		rtfGsBt2.setStyle(Font.NORMAL);
		rtfGsBt2.setSize(10);
		  
		  
		Paragraph title = new Paragraph(exam.getExam_name());
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		document.add(title);
		  //正文
		//setParagraph(document,rtfGsBt2,"编号:"+exam.getExam_id());
		setParagraph(document,rtfGsBt2,"名称:"+exam.getExam_name());  
		setParagraph(document,rtfGsBt2,"科目:"+exam.getCourse().getCourse_name()); 
		//setParagraph(document,rtfGsBt2,"考试班级:"+exam.getClass_id());   

				for(Questions question:questions){
					if(question.getType()==1){
						type="单选题";
					}else if(question.getType()==2){
						type="多选题";
					}else{
						type="不定选题";
					}
					  Paragraph Topic=new Paragraph(++i +"."+question.getTopic()+"("+type+")");
					  Topic.setFont(rtfGsBt2);					
					  document.add(Topic);					  				
					  List<String> list=StringUtil.getOption(question.getOptions(),separator);					  
					  for(String option:list){						
						  Paragraph Options=new Paragraph(option);
						  Options.setFont(rtfGsBt2);
						  document.add(Options); 
					  }	
					  list.clear();
					  Paragraph answer=new Paragraph("答案:"+question.getAnswer());
					  answer.setFont(rtfGsBt2);
					  document.add(answer);
					  
					  Paragraph analysis=new Paragraph("问题解析:"+question.getAnalysis());
					  analysis.setFont(rtfGsBt2);
					  document.add(analysis);
				}
//			}
			
//		}
		  document.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block			
			e.printStackTrace();
		}
	}

	public void setParagraph(Document document,RtfParagraphStyle rtfGsBt,String message) throws DocumentException{
		Paragraph paragraph = new Paragraph(message);
		paragraph.setFont(rtfGsBt);
		document.add(paragraph);
	}
	
//	public List<String> getOption(String options,String separator){
//		int index=0;
//		String option;
//
//		while(index<options.lastIndexOf(separator)){			
//			option=options.substring(index, options.indexOf(separator,index)).trim();
//			list.add(option);
//			index= options.indexOf(separator,index)+1;
//			if((index-1)==options.lastIndexOf(separator)){
//				option=options.substring(index).trim();
//				list.add(option);
//			}				
//		}
//		return list;
//	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值