com.lowagie.text 导出数据为word文档

private Document document;
	private BaseFont bfChinese;
	
	public testWord() {
		this.document = new Document(PageSize.A4);
    	this.document.setMargins(90, 90, 99, 72);
	}

	public void expStuInfo(HttpServletResponse response,
			HttpServletRequest request,
			HashMap map) throws Exception {
		ByteArrayOutputStream ba = new ByteArrayOutputStream();
		String filepath="毕业设计(论文)任务书.doc";
		// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
		RtfWriter2.getInstance(this.document, ba);
		this.document.open();
		// 设置中文字体
		this.setBfChinese(BaseFont.createFont(
				"C:\\Users\\Admin\\Desktop\\精楷简体.ttf", BaseFont.IDENTITY_H,
				BaseFont.NOT_EMBEDDED));
		// 正文字体风格
		Font contextFont = new Font(bfChinese, 6, Font.NORMAL);
		Paragraph context = new Paragraph("");
		// 设置行距
		context.setLeading(10);
		// 正文格式左对齐
		context.setAlignment(Element.ALIGN_LEFT);
		context.setFont(contextFont);
		// 离上一段落(标题)空的行数
		context.setSpacingBefore(5);
		// 设置第一行空的列数
		context.setFirstLineIndent(20);
		document.add(context);
		Font titleFont = new Font(this.bfChinese, 26, Font.BOLD);
		Paragraph title = new Paragraph("毕业设计(论文)任务书");
		// 设置标题格式对齐方式
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		this.document.add(title);
		
		Table aTable = new Table(18, 3); // 3--> 导出的数据行数
		int width[] = { 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25 };
		aTable.setWidths(width);// 设置每列所占比例
		aTable.setWidth(100); // 占页面宽度 90%
		aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
		aTable.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示
		aTable.setAutoFillEmptyCells(true); // 自动填满
		aTable.setBorderWidth(0); // 边框宽度
		aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色

		// 标题
		Font fontChinese = new Font(bfChinese, 10, Font.BOLD);
		Cell cell = new Cell(new Phrase("课题名", fontChinese));
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setBorderColor(new Color(0, 0, 0));
		cell.setBackgroundColor(new Color(204, 153, 255));
		aTable.addCell(cell);

		Cell cell1 = new Cell(new Phrase("课题性质名称", fontChinese));
		cell1.setVerticalAlignment(Element.ALIGN_CENTER);
		cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell1.setBorderColor(new Color(0, 0, 0));
		cell1.setBackgroundColor(new Color(204, 153, 255));
		aTable.addCell(cell1);

		Cell cell2 = new Cell(new Phrase("课题类别名称", fontChinese));
		cell2.setVerticalAlignment(Element.ALIGN_CENTER);
		cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell2.setBorderColor(new Color(0, 0, 0));
		cell2.setBackgroundColor(new Color(204, 153, 255));
		aTable.addCell(cell2);

  		for (int i = 0; i < 3; i++) {
			aTable.addCell(new Cell(i+ ""));
   		}
		try {
			document.add(aTable);
			document.add(new Paragraph("\n"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.document.close();
		response.setContentType("application/vnd.ms-word");  
		response.setHeader("Content-disposition","attachment; filename="
		+new String(filepath.getBytes("GB2312"), "ISO8859-1"));   
		ServletOutputStream out= response.getOutputStream();
		ba.writeTo(out);      
		out.flush();


你可能感兴趣的:(Java学习)