1. 程式人生 > >自定義World文件

自定義World文件

package com.qixin.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;

public class SimpleDocument {
	
	/**
	 * sd
	 */
	public static void main(String[] args) throws Exception {
		XWPFDocument doc = new XWPFDocument();
		XWPFParagraph p1 = doc.createParagraph();

		XWPFTable table = doc.createTable(11, 2);
		// 設定上下左右四個方向的距離,可以將表格撐大
		table.setCellMargins(20, 20, 20, 20);
		// table.set
		List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
		tableCells.get(0).setText("第一行第一列的資料");
		tableCells.get(1).setText("第一行第二列的資料");

		List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
		tableCellsq.get(0).setText("第二行第二列的資料");
		tableCellsq.get(1).setText("第二行第二列的資料");

		// 設定字型對齊方式
		p1.setAlignment(ParagraphAlignment.CENTER);
		p1.setVerticalAlignment(TextAlignment.TOP);
		// 第一頁要使用p1所定義的屬性
		XWPFRun r1 = p1.createRun();
		// 設定字型是否加粗
		r1.setBold(true);
		r1.setFontSize(20);
		// 設定使用何種字型
		r1.setFontFamily("Courier");
		// 設定上下兩行之間的間距
		r1.setTextPosition(20);
		r1.setText("公司招聘會入場須知");

		// 設定個人資訊
		XWPFParagraph p2 = doc.createParagraph();
		p2.setAlignment(ParagraphAlignment.LEFT);
		XWPFRun r2 = p2.createRun();
		r2.setTextPosition(15);
		r2.setText("姓名" + "                    " + "張三");
		r2.addCarriageReturn();
		r2.setText("性別" + "                    " + "女");
		r2.addCarriageReturn();
		r2.setText("手機號" + "               " + "12345678965");
		r2.addCarriageReturn();
		r2.setText("郵箱" + "                    " + "
[email protected]
"); r2.addCarriageReturn(); r2.setText("開始時間" + " " + "2013-05-28 12:30"); r2.addCarriageReturn(); r2.setText("結束時間" + " " + "2013-05-28 13:20"); r2.addCarriageReturn(); // 存放試題資訊 XWPFParagraph p3 = doc.createParagraph(); p3.setWordWrap(true); XWPFRun r3 = p3.createRun(); r3.setTextPosition(10); r3.setFontSize(15); r3.setText("一、選擇題(共50分)"); // 題目和選項 XWPFParagraph p4 = doc.createParagraph(); p4.setWordWrap(true); XWPFRun r4 = p4.createRun(); r4.setTextPosition(13); r4.setText(" 1、下面說法正確的是?(3分)"); r4.addCarriageReturn(); r4.setText(" A:子類如果使用父類的方法必須使用super關鍵字"); r4.addCarriageReturn(); r4.setText(" B:子類如果使用父類的方法必須使用super關鍵字"); r4.addCarriageReturn(); r4.setText(" C:子類如果使用父類的方法必須使用super關鍵字"); r4.addCarriageReturn(); r4.setText(" D:子類如果使用父類的方法必須使用super關鍵字"); r4.addCarriageReturn(); r4.setText("正確答案:ABCD"); r4.setText("選擇答案:AC"); // 判斷題 XWPFParagraph p5 = doc.createParagraph(); p5.setWordWrap(true); XWPFRun r5 = p5.createRun(); r5.setTextPosition(10); r5.setFontSize(15); r5.setText("一、判斷題(共50分)"); XWPFParagraph p6 = doc.createParagraph(); p6.setWordWrap(true); // 題目 int i; for (i = 0; i < 5; i++) { XWPFRun r6 = p6.createRun(); r6.setTextPosition(13); r6.setText("1、子類如果使用父類的方法必須使用super關鍵字(5分)"); r6.addCarriageReturn(); r6.setText("正確答案:對"); r6.setText(" "); r6.setSubscript(VerticalAlign.BASELINE); r6.setText("選擇答案:"); XWPFRun r7 = p6.createRun(); r7.setTextPosition(13); // 控制某一個字型顏色為紅色 if (i == 3) { r7.setColor("FF0000"); } r7.setText("錯"); r7.addCarriageReturn(); } InputStream is = new FileInputStream(new File("D:\\2000893685.jpg")); InputStream is1 = new FileInputStream(new File("D:\\2000893685.jpg")); XWPFTable tab = doc.createTable(1, 1); tab.setCellMargins(20, 20, 20, 20); List<XWPFTableCell> tabCells = tab.getRow(0).getTableCells(); tabCells.get(0).setText("這裡放圖片"); XWPFParagraph parapictest = doc.createParagraph(); XWPFRun runtest = parapictest.createRun(); runtest.setText("圖片:"); XWPFRun pictest = doc.createParagraph().createRun(); XWPFPicture picture = pictest.addPicture(is, Document.PICTURE_TYPE_JPEG, "D:\\2000893685.jpg", 1000*360*10,1000*360*10); XWPFPicture picture2 = pictest.addPicture(is1, Document.PICTURE_TYPE_JPEG, "D:\\2000893685.jpg", 1000*360*10,1000*360*10); XWPFParagraph p7 = doc.createParagraph(); p7.setWordWrap(true); FileOutputStream out = new FileOutputStream("D:\\simple.docx"); doc.write(out); out.close(); System.out.println("success"); } }