Java导出word

导出需要jar包:iText-5.0.6.jar,iTextAsian.jar,iText-rtf-2.1.7.jar,jxl.jar

项目中要把一些表单和表格的数据导出成word的文件。目前的的实现了把表单的内容导出到word,但是以表格的方式展现出来的。

exportWord:

package com.sg.ivs.testReport.testReport.testReport.bizc;

import java.awt.Color;

import org.springframework.web.client.RestTemplate;

import com.lowagie.text.Cell;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.style.RtfFont;
import com.lowagie.text.rtf.table.RtfBorder;
import com.lowagie.text.rtf.table.RtfBorderGroup;
import com.lowagie.text.rtf.table.RtfCell;

/**
 * Word模板导出模型
 public class ExportWord {

	// 标题字体为黑色
	private Color color_title = new Color(192, 192, 192);
	// 内容为白色
	private Color color_Context = Color.white;

	public Color getColor_title() {
		return color_title;
	}

	public Color getColor_Context() {
		return color_Context;
	}
	public Cell createCell(String contentValue){
		RtfFont rtf_s_title = new RtfFont("仿 宋", 14.0f, Font.BOLD, Color.BLACK);
		// 标题中括号内字体 仿宋 五号 加粗
		RtfFont rtf_f_title = new RtfFont("仿 宋", 11.0f, Font.BOLD, Color.BLACK);
		// 处理情况 仿宋 五号
		RtfFont rtf_f_contxt = new RtfFont("仿 宋", 11.0f, Font.NORMAL,Color.BLACK);
		Cell cell = null;
		
		
		return cell;
	}
	public Table createTable(String contenvalue) {
		RestTemplate rest = new RestTemplate();
		String[] testa = contenvalue.split(",\\$");
		RtfFont rtf_s_title = new RtfFont("仿 宋", 14.0f, Font.BOLD, Color.BLACK);
		// 标题中括号内字体 仿宋 五号 加粗
		RtfFont rtf_f_title = new RtfFont("仿 宋", 11.0f, Font.BOLD, Color.BLACK);
		// 处理情况 仿宋 五号
		RtfFont rtf_f_contxt = new RtfFont("仿 宋", 11.0f, Font.NORMAL,
				Color.BLACK);
		Table table = null;
		
		try {
			table = new Table(4, 3);
			int width[] = { 2, 3, 2, 3 };
			table.setWidths(width);
			table.setWidth(100);
			for (int i = 0; i < testa.length; i++) {
				
				RtfCell cell1 = generateHeader1(testa[i].split(":")[0], rtf_s_title, 1, 1,
						Element.ALIGN_RIGHT, Element.ALIGN_RIGHT);
				cell1.setBorders(getRtfBorderGroupStyle(1, 1, 1, 1));
				table.addCell(cell1);
				RtfCell cell2 = generateHeader1(testa[i].split(":")[1], rtf_f_contxt, 1, 1,
						Element.ALIGN_LEFT, Element.ALIGN_LEFT);
				cell2.setBorders(getRtfBorderGroupStyle(1, 1, 1, 1));
				table.addCell(cell2);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}
		return table;
	}

	/**
	 * 单元格中样式统一的
	 * 
	 * @param title
	 * @param font
	 * @param rowSpan
	 * @param colSpan
	 * @param halign
	 * @param valign
	 * @return
	 */
	private RtfCell generateHeader1(String title, Font font, int rowSpan,
			int colSpan, int halign, int valign) {
		RtfCell header = new RtfCell();
		Paragraph phead = new Paragraph(title);
		phead.setLeading(17f);
		phead.setIndentationLeft(5f);
		phead.setIndentationRight(5f);
		phead.setFont(font);
		header.add(phead);
		header.setHeader(true);
		header.setRowspan(rowSpan);
		header.setColspan(colSpan);
		header.setHorizontalAlignment(halign);
		header.setVerticalAlignment(valign);
		return header;
	}

	/**
	 * 设置字体样式
	 * 
	 * @param family
	 * @param color
	 * @param size
	 * @param style
	 * @return
	 */
	public Font setFontStyle(String family, Color color, float size, int style) {
		Font font = new Font();
		font.setFamily(family);
		font.setColor(color);
		font.setSize(size);
		font.setStyle(style);
		return font;
	}

	/**
	 * 设置单元格的边框
	 * 
	 * @param top
	 * @param bottom
	 * @param left
	 * @param right
	 * @return
	 */
	public RtfBorderGroup getRtfBorderGroupStyle(int top, int bottom, int left,
			int right) {
		RtfBorderGroup rbg = new RtfBorderGroup();
		switch (top) {
		case 1:
			rbg.addBorder(RtfCell.TOP, RtfBorder.BORDER_SINGLE, 1.0f,
					Color.black);
			break;
		case 2:
			rbg.addBorder(RtfCell.TOP, RtfBorder.BORDER_DOUBLE, 1.0f,
					Color.black);
			break;
		default:
			rbg.addBorder(RtfCell.TOP, RtfBorder.BORDER_NONE, 1.0f, Color.black);
			break;
		}

		switch (bottom) {
		case 1:
			rbg.addBorder(RtfCell.BOTTOM, RtfBorder.BORDER_SINGLE, 1.0f,
					Color.black);
			break;
		case 2:
			rbg.addBorder(RtfCell.BOTTOM, RtfBorder.BORDER_DOUBLE, 1.0f,
					Color.black);
			break;
		default:
			rbg.addBorder(RtfCell.BOTTOM, RtfBorder.BORDER_NONE, 1.0f,
					Color.black);
			break;
		}

		switch (left) {
		case 1:
			rbg.addBorder(RtfCell.LEFT, RtfBorder.BORDER_SINGLE, 1.0f,
					Color.black);
			break;
		case 2:
			rbg.addBorder(RtfCell.LEFT, RtfBorder.BORDER_DOUBLE, 1.0f,
					Color.black);
			break;
		default:
			rbg.addBorder(RtfCell.LEFT, RtfBorder.BORDER_NONE, 1.0f,
					Color.black);
			break;
		}

		switch (right) {
		case 1:
			rbg.addBorder(RtfCell.RIGHT, RtfBorder.BORDER_SINGLE, 1.0f,
					Color.black);
			break;
		case 2:
			rbg.addBorder(RtfCell.RIGHT, RtfBorder.BORDER_DOUBLE, 1.0f,
					Color.black);
			break;
		default:
			rbg.addBorder(RtfCell.RIGHT, RtfBorder.BORDER_NONE, 1.0f,
					Color.black);
			break;
		}

		return rbg;
	}
}

jsp页面中的内容:

<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page language="java" contentType="application/x-msdownload"
	pageEncoding="UTF-8"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="com.lowagie.text.*"%>
<%@page import="com.lowagie.text.rtf.RtfWriter2"%>
<%@page import="com.lowagie.text.rtf.style.*"%>
<%@page import="java.awt.Color"%>
<%@page import="com.lowagie.text.rtf.table.*"%>
<%@page import="com.sg.ivs.testReport.testReport.testReport.bizc.ExportWord"%>


<%
	//关于文件下载时采用文件流输出的方式处理:
	//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;

	response.reset();//可以加也可以不加
	response.setContentType("application/x-download");

	//application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径
	java.io.OutputStream outp = null;
	try {
		
		String filedisplay = "测试报告.doc";
		filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
		response.addHeader("Content-Disposition", "attachment;filename="
				+ filedisplay);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Document document = new Document(PageSize.A4.rotate());
		
		ExportWord exportword = new ExportWord();
		outp = response.getOutputStream();
		try {
			//标题字体风格
			//大标题 黑体 小二 加粗 黑色
			RtfFont rtfFont = new RtfFont("黑 体",18.0f,Font.BOLD,Color.BLACK);
			//表中小标题 仿宋 四号 加粗 黑色
			RtfFont rtf_s_title = new RtfFont("仿 宋",14.0f,Font.BOLD,Color.BLACK);
			//标题中括号内字体 仿宋 五号 加粗 
			RtfFont rtf_f_title = new RtfFont("仿 宋",11.0f,Font.BOLD,Color.BLACK);
			//表中数据默认 宋体 五号 
			RtfFont rtf_def = new RtfFont("宋 体",11.0f,Font.NORMAL,Color.BLACK);
			//编号的字体 Calibri 五号
			RtfFont rtf_cal = new RtfFont("Calibri",11.0f,Font.NORMAL,Color.BLACK);
			//处理情况 仿宋 五号
			RtfFont rtf_f_contxt = new RtfFont("仿 宋",11.0f,Font.NORMAL,Color.BLACK);

			RtfWriter2.getInstance(document, bos);
			document.open();
			Paragraph p = new Paragraph("测试报告信息",rtfFont);
			p.setAlignment(Element.ALIGN_CENTER);
			p.setSpacingBefore(0.0f);
			document.add(p);
			
			document.add(exportword.createTable(request.getParameter("content")));
			document.close();
			outp.write(bos.toByteArray());
		} finally {
			try {
				document.close();
			} catch (Exception e) {

			}
			try {
				bos.close();
			} catch (Exception e) {

			}
		}	
		//  
		outp.flush();
		//要加以下两句话,否则会报错
		//java.lang.IllegalStateException: getOutputStream() has already been called for //this response  
		out.clear();
		out = pageContext.pushBody();
	} catch (Exception e) {
		System.out.println("Error!");
		e.printStackTrace();
	} finally {
	}
%>


调用jsp文件的js代码

 me._export_onclick = function(){
    	 var formData = me.view.getForm().entityContainer.data;
    	 var _tabControl = me.view.getTabControl();
    	 
    	 var mycars = new Array();
    	 mycars[0] = "$简介:" + formData.reportBrief;
    	 mycars[1] = "$充分性评价:" + formData.sufficiencyEstimation;
    	 mycars[2] = "$测试规程:" + formData.testProcess;
    	 mycars[3] = "$测试体制:" + formData.testSystem;
    	 mycars[4] = "$测试结论:" + formData.testConclusion;
    	 


    	 openExportWindow("/ivs/biz/testReport/exportword.jsp", mycars, "导出word");
    	 
    }


前端把页面的数据封装成一种特定的格式,然后在java端把这种特定格式的解析成value和text的格式,然后把数据放到表格中,同时还可以设置字体,表格宽度和高度。

你可能感兴趣的:(Java导出word)