POI word operations

1 篇文章 0 订阅

使用POI 3.10,jar list: 

poi-3.10-FINAL-20140208.jar

poi-ooxml-3.10-FINAL.jar

poi-ooxml-schemas-3.10-FINAL.jar

xmlbeans-2.3.0.jar

ooxml-schemas-1.1.jar


code: 

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.LineSpacingRule;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

public class POIWordUtils {
	public final static String DEFAULT_FONT_FAMILY = "Calibri";
	public final static int DEFAULT_FONT_SIZE = 11;
	public final static long DEFAULT_PAGE_LANDSCAPE_HEIGHT = 12240L;
	public final static long DEFAULT_PAGE_LANDSCAPE_WEIGHT = 20160L;
	public final static long DEFAULT_PAGE_MARGIN_LEFT = 720L;
	public final static long DEFAULT_PAGE_MARGIN_TOP = 720L;
	public final static long DEFAULT_PAGE_MARGIN_RIGHT = 720L;
	public final static long DEFAULT_PAGE_MARGIN_BOTTOM = 720L;
	public final static long DEFAULT_TABLE_WIDTH = 18720L;

	//public final static int MAX_IMAGE_WIDTH = 840;
	//public final static int MAX_IMAGE_HEIGHT = 500;
	public final static int MAX_IMAGE_WIDTH = 840;
	public final static int MAX_IMAGE_HEIGHT = 438;

	public static XWPFDocument createDocument() {
		XWPFDocument doc = new XWPFDocument();
		return doc;
	}

	public static void setPageSetting(XWPFDocument doc, boolean isLandscape) {
		long weight = DEFAULT_PAGE_LANDSCAPE_WEIGHT, height = DEFAULT_PAGE_LANDSCAPE_HEIGHT;
		if (!isLandscape) {
			weight = DEFAULT_PAGE_LANDSCAPE_HEIGHT;
			height = DEFAULT_PAGE_LANDSCAPE_WEIGHT * 11 / 14;
		}
		setPageSetting(doc, isLandscape, height, weight, DEFAULT_PAGE_MARGIN_LEFT, DEFAULT_PAGE_MARGIN_TOP, DEFAULT_PAGE_MARGIN_RIGHT,
				DEFAULT_PAGE_MARGIN_BOTTOM);
	}

	public static void setPageSetting(XWPFDocument doc, boolean isLandscape, long weight, long height, long left, long top, long right, long bottom) {
		CTSectPr sectPr = doc.getDocument().getBody().isSetSectPr() ? doc.getDocument().getBody().getSectPr() : doc.getDocument().getBody().addNewSectPr();
		CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
		pgsz.setH(BigInteger.valueOf(weight));
		pgsz.setW(BigInteger.valueOf(height));
		if (isLandscape) {
			pgsz.setOrient(STPageOrientation.LANDSCAPE);
		} else {
			pgsz.setOrient(STPageOrientation.PORTRAIT);
		}

		setDocumentMargin(doc, left, top, right, bottom);
	}

	public static void setDocumentMargin(XWPFDocument doc, long left, long top, long right, long bottom) {
		CTSectPr sectPr = doc.getDocument().getBody().isSetSectPr() ? doc.getDocument().getBody().getSectPr() : doc.getDocument().getBody().addNewSectPr();
		CTPageMar ctpagemar = sectPr.addNewPgMar();
		ctpagemar.setLeft(BigInteger.valueOf(left));
		ctpagemar.setTop(BigInteger.valueOf(top));
		ctpagemar.setRight(BigInteger.valueOf(right));
		ctpagemar.setBottom(BigInteger.valueOf(bottom));
	}

	public static void writeTextContent(XWPFDocument doc, String textContent) {
		writeTextContent(doc, textContent, true, false, false, DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, false, false, "000000");
	}

	public static void writeTextContent(XWPFDocument doc, String textContent, boolean isWordWrap, boolean isPageBreak, boolean isAlignmentCenter,
			String fontFamily, int fontSize, boolean isFontBold, boolean isUnderline, String color) {
		if (doc == null) {
			return;
		}
		XWPFParagraph p = null;
		if (isWordWrap) {
			p = doc.createParagraph();
		} else {
			p = doc.getLastParagraph();
		}
		if (isPageBreak) {
			p.setPageBreak(true);
		}
		p.setWordWrap(true);
		if (isAlignmentCenter) {
			p.setAlignment(ParagraphAlignment.CENTER);
		} else {
			p.setAlignment(ParagraphAlignment.LEFT);
		}
		p.setSpacingLineRule(LineSpacingRule.EXACT);
		XWPFRun r = p.createRun();
		if (isUnderline) {
			r.setUnderline(UnderlinePatterns.SINGLE);
		}
		r.setBold(isFontBold);
		r.setFontFamily(fontFamily);
		r.setFontSize(fontSize);
		r.setColor(color);
		r.setText(textContent);
	}

	public static XWPFTable writeTable(Table tb) throws InvalidFormatException, IOException {
		XWPFTable table = tb.getDoc().createTable(tb.getRowCount(), tb.getCellCount());
		setTableData(table, tb);
		return table;
	}

	private static void setTableData(XWPFTable table, Table tb) throws InvalidFormatException, IOException {
		XWPFTableRow row;
		XWPFTableCell col;
		int i, j;
		for (i = 0; i < tb.getRowCount(); i++) {
			TableRow tableRow = tb.getRows().get(i);
			List<TableCell> tbRow = tableRow.getCells();
			row = table.getRow(i);
			if(tableRow.getHeight()!=0){
				row.setHeight(tableRow.getHeight());
			}
			for (j = 0; j < tb.getCellCount() && j < tbRow.size(); j++) {
				TableCell cellData = tbRow.get(j);
				col = row.getCell(j);
				CTTc cttc = col.getCTTc();
				CTTcPr cellPr = cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr();;
				CTShd ctshd =  cellPr.isSetShd() ? cellPr.getShd() : cellPr.addNewShd();;
				if (null != cellData.getBgcolor()) {
					ctshd.setFill(cellData.getBgcolor());
				}
				if (null != cellData.getFontColor()) {
					ctshd.setColor(cellData.getFontColor());
				}
				cellPr.addNewTcW().setW(BigInteger.valueOf(cellData.getWidth()));
				if (cellData.isCenter()) {
					cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
					cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
				}
				if (cellData.isImage()) {
					setImage(col, cellData.getValue());
				} else if (cellData.getValue() instanceof String) {
					XWPFParagraph p = col.getParagraphs().get(0);
					p.setSpacingAfter(0);
					XWPFRun r = p.createRun();
					if (cellData.isBold())
						r.setBold(cellData.isBold());
					if (0 != cellData.getFontSize()) {
						r.setFontSize(cellData.getFontSize());
					}
					r.setText((String) cellData.getValue());
				} else {
					XWPFRun r = col.getParagraphs().get(0).createRun();
					if (0 != cellData.getFontSize()) {
						r.setFontSize(cellData.getFontSize());
					}
					r.setText("");
				}
			}
		}
	}

	private static void setImage(XWPFTableCell col, Object object) throws InvalidFormatException, IOException {
		XWPFParagraph p = col.getParagraphs().get(0);
		p.setAlignment(ParagraphAlignment.CENTER);
		p.setVerticalAlignment(TextAlignment.CENTER);
		XWPFRun r = p.createRun();
		String imageFile = "";
		r.setText(imageFile);
		InputStream is = null;
		try {
			Iterator<ImageReader> iter = null;
			int width, height;
			String imageType = "";
			Object input = null;
			File file = null;
			if (object instanceof String) {
				file = new File((String) object);
				input = ImageIO.createImageInputStream(file);
				is = new FileInputStream(file);
			} else if (object instanceof byte[]) {
				is = new ByteArrayInputStream((byte[]) object);
				input = ImageIO.createImageInputStream(is);
				is = new ByteArrayInputStream((byte[]) object);
			}
			iter = ImageIO.getImageReaders(input);
			if (!iter.hasNext()) {
				throw new RuntimeException("No readers found!");
			}
			ImageReader reader = iter.next();
			reader.setInput(input, true);
			width = reader.getWidth(0);
			height = reader.getHeight(0);
			imageType = reader.getFormatName();

			int format = getImageType(imageType);
			width = width > MAX_IMAGE_WIDTH ? MAX_IMAGE_WIDTH : width;
			height = height > MAX_IMAGE_HEIGHT ? MAX_IMAGE_HEIGHT : height;
			r.addPicture(is, format, imageFile, Units.toEMU(width), Units.toEMU(height));
			r.addBreak(BreakType.PAGE);
		} catch (Exception e) {
		} finally {
			if (is != null) {
				is.close();
			}
		}
	}

	private static int getImageType(String imageType) {
		int format = XWPFDocument.PICTURE_TYPE_PNG;
		if (imageType.equalsIgnoreCase("emf"))
			format = XWPFDocument.PICTURE_TYPE_EMF;
		else if (imageType.equalsIgnoreCase("wmf"))
			format = XWPFDocument.PICTURE_TYPE_WMF;
		else if (imageType.equalsIgnoreCase("pict"))
			format = XWPFDocument.PICTURE_TYPE_PICT;
		else if (imageType.equalsIgnoreCase("jpeg") || imageType.equalsIgnoreCase("jpg"))
			format = XWPFDocument.PICTURE_TYPE_JPEG;
		else if (imageType.equalsIgnoreCase("png"))
			format = XWPFDocument.PICTURE_TYPE_PNG;
		else if (imageType.equalsIgnoreCase("dib"))
			format = XWPFDocument.PICTURE_TYPE_DIB;
		else if (imageType.equalsIgnoreCase("gif"))
			format = XWPFDocument.PICTURE_TYPE_GIF;
		else if (imageType.equalsIgnoreCase("tiff"))
			format = XWPFDocument.PICTURE_TYPE_TIFF;
		else if (imageType.equalsIgnoreCase("eps"))
			format = XWPFDocument.PICTURE_TYPE_EPS;
		else if (imageType.equalsIgnoreCase("bmp"))
			format = XWPFDocument.PICTURE_TYPE_BMP;
		else if (imageType.equalsIgnoreCase("wpg"))
			format = XWPFDocument.PICTURE_TYPE_WPG;
		else {
			// logger.error("Unsupported picture: " + imageType +
			// ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
		}
		return format;
	}
	
	public static void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
		for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
			XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
			CTTc cttc = cell.getCTTc();
			CTTcPr tcPr = cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr();
			if (cellIndex == fromCell) {
				tcPr.addNewHMerge().setVal(STMerge.RESTART);
			} else {
				tcPr.addNewHMerge().setVal(STMerge.CONTINUE);
			}
		}
	}

	public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
		for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
			XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
			CTTc cttc = cell.getCTTc();
			CTTcPr tcPr = cttc.isSetTcPr() ? cttc.getTcPr() : cttc.addNewTcPr();
			if (rowIndex == fromRow) {
				tcPr.addNewVMerge().setVal(STMerge.RESTART);
			} else {
				tcPr.addNewVMerge().setVal(STMerge.CONTINUE);
			}
		}
	}
	
	public static void writeToFile(XWPFDocument doc, String file) throws IOException {
		FileOutputStream out = new FileOutputStream(file);
		doc.write(out);
		out.close();
	}
	
	public static class TableRow {
		private List<TableCell> cells;
		private int height;
		public List<TableCell> getCells() {
			return cells;
		}
		public void setCells(List<TableCell> cells) {
			this.cells = cells;
		}
		public int getHeight() {
			return height;
		}
		public void setHeight(int height) {
			this.height = height;
		}
		
	}
	public static class TableCell {
		private int width;
		private Object value;
		private boolean isImage;
		private boolean isCenter;
		private String bgcolor;
		private String fontColor;
		private int fontSize;
		private boolean isBold;

		public TableCell() {

		}
		
		public TableCell(Object value, int width, boolean isImage, boolean isCenter, String bgcolor, String fontColor, int fontSize, boolean isBold) {
			this.width = width;
			this.value = value;
			this.isImage = isImage;
			this.isCenter = isCenter;
			this.bgcolor = bgcolor;
			this.fontColor = fontColor;
			this.fontSize = fontSize;
			this.isBold = isBold;
		}

		public int getWidth() {
			return width;
		}

		public void setColWidth(int width) {
			this.width = width;
		}

		public Object getValue() {
			return value;
		}

		public void setObject(Object value) {
			this.value = value;
		}

		public boolean isImage() {
			return isImage;
		}

		public void setImage(boolean isImage) {
			this.isImage = isImage;
		}

		public boolean isCenter() {
			return isCenter;
		}

		public void setCenter(boolean isCenter) {
			this.isCenter = isCenter;
		}

		public String getBgcolor() {
			return bgcolor;
		}

		public void setBgcolor(String bgcolor) {
			this.bgcolor = bgcolor;
		}

		public String getFontColor() {
			return fontColor;
		}

		public void setFontColor(String fontColor) {
			this.fontColor = fontColor;
		}

		public int getFontSize() {
			return fontSize;
		}

		public void setFontSize(int fontSize) {
			this.fontSize = fontSize;
		}

		public boolean isBold() {
			return isBold;
		}

		public void setBold(boolean isBold) {
			this.isBold = isBold;
		}

	}

	public static class Table {
		private XWPFDocument doc;
		private List<TableRow> rows;
		private int rowCount;
		private int cellCount;

		public Table() {
		}

		public Table(XWPFDocument doc, List<TableRow> rows, int rowCount, int cellCount) {
			this.doc = doc;
			this.setRows(rows);
			this.setRowCount(rowCount);
			this.setCellCount(cellCount);
			
		}

		public XWPFDocument getDoc() {
			return doc;
		}

		public void setDoc(XWPFDocument doc) {
			this.doc = doc;
		}

		public int getRowCount() {
			return rowCount;
		}

		public void setRowCount(int rowCount) {
			this.rowCount = rowCount;
		}

		public int getCellCount() {
			return cellCount;
		}

		public void setCellCount(int cellCount) {
			this.cellCount = cellCount;
		}

		public List<TableRow> getRows() {
			return rows;
		}

		public void setRows(List<TableRow> rows) {
			this.rows = rows;
		}

	}

	
	public static void main(String[] args) throws Exception {
		String file = "test.doc";

		XWPFDocument doc = createDocument();

		setPageSetting(doc, false);

		writeTextContent(doc, "Information which must be replaced by corresponding Third Party Data.", true, true, true, DEFAULT_FONT_FAMILY, 16, true, true,
				"000000");
		writeTextContent(doc, "Information which must be replaced by corresponding Third Party Data.", false, true, true, DEFAULT_FONT_FAMILY, 16, true, true,
				"FFFFFF");
		writeTextContent(doc, "Note: LOGO must be provided as picture");
		writeTextContent(doc, "Replacement for all texts below can be provided in text (unformatted). Amount of digits must be the same or less.");
		writeTextContent(doc, "");

		byte[] bytes = { -119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 57, 0, 0, 0, 50, 8, 2, 0, 0, 0, 105, -67, -28, 17, 0, 0, 4,
				83, 73, 68, 65, 84, 120, -38, -19, -104, -25, 79, 83, 81, 20, -64, -3, 123, 4, -9, -118, 49, 14, 18, -115, 113, -60, -103, 8, -88, 8, 78, -120,
				123, 11, 34, 1, -59, 17, 19, -93, 81, -111, 66, 91, 42, 104, -103, 82, 64, -88, 8, 66, 81, -118, -78, -124, -106, 2, 50, 74, 1, -53, 44, 32, 5,
				-54, 40, -48, -27, -95, -89, -34, 60, 69, -6, 90, -75, 15, 98, -34, -51, -7, 112, -33, 57, -9, -66, -4, -6, 122, -49, -70, -13, -26, -17, -117,
				116, -87, 44, -11, -31, 122, 4, -60, -7, -35, 124, 125, 91, 32, 77, -52, -83, -83, -84, -17, -46, -115, -116, 91, 126, -116, 119, -91, -51,
				-114, -65, 106, -34, -65, 37, 115, -9, -30, 120, 4, -68, -16, 9, 77, -65, -55, 43, 20, -66, -83, 46, -83, -23, -24, -20, -43, 25, -116, 38,
				-53, 12, -125, 57, 86, 55, -49, -56, 21, -66, 60, -17, 27, 105, 33, 81, -17, -29, -60, -118, -113, 85, 109, 93, 125, -61, 99, -6, 73, -117,
				-61, -61, 85, -84, 11, -67, -93, -42, 28, 19, -20, -67, -106, 18, -12, 76, 34, -56, -108, 23, -54, -44, -16, -51, -52, 102, -77, -27, 47, -58,
				63, 99, 93, 125, 68, -80, -9, 106, -54, -27, -57, 121, -36, -12, 74, -55, -25, -42, -26, 14, -19, -120, 51, -33, -116, 57, -42, -78, -38, 14,
				-35, -24, -124, -55, 100, -74, -72, 114, 32, 43, -100, -14, 5, 14, -56, -116, -84, -19, 61, 67, 22, -41, 15, 100, 109, 106, -17, 119, 100, 49,
				-53, -54, -78, -78, -84, 78, -78, -66, 120, -93, -128, 9, -83, -52, 9, -42, -65, -115, -81, 44, -21, -17, 89, -27, -115, -35, -125, -61, 122,
				90, 97, 125, 107, 46, -77, 26, 77, -26, 73, -125, 105, 98, -46, 56, 105, 48, 82, -11, -96, 124, -103, 93, -3, 56, -87, 44, -100, 47, -67, -59,
				47, 28, 27, 55, -4, -106, 53, 86, 92, -107, 83, -84, -94, 21, 39, 88, -107, 109, -3, 121, 101, 45, -94, -126, 122, 113, -111, -110, -86, 87,
				-75, 107, -73, 95, 72, -36, -32, 31, -73, -52, -121, -25, 27, -106, 65, 53, 117, -10, -23, -56, 11, -67, -125, 69, -45, 11, 72, 87, -7, -42,
				-93, -124, 82, 52, -83, -9, -113, 53, 82, 74, -3, -44, -126, 58, -78, 11, 76, -93, -108, -70, 81, 42, 83, 19, -45, 29, 65, 17, 115, 113, 32,
				-73, 68, 69, 90, 40, -88, -1, -119, 62, -116, -5, -127, 90, -113, -73, 116, 14, 16, -45, -13, -84, 42, 98, -54, -108, 54, -50, -60, -86, 80,
				106, -96, -2, -92, 21, 39, 88, -43, -35, -125, -48, -76, 96, -21, 82, -39, -48, 77, -12, -5, -126, 82, -87, 27, 115, 74, 84, -60, 20, 28, 89,
				-128, -54, 69, -5, -93, -96, 84, 103, -50, -73, -32, -49, 93, 123, 60, 22, -83, 112, 106, 81, 9, 63, 119, -43, -31, 24, -22, 70, -16, 36, -78,
				-59, 43, 88, -124, -54, 117, 39, 98, -63, -49, -104, 99, 5, -57, 56, 20, -102, -114, -42, 7, -62, 98, 84, 42, -102, 52, -18, -98, 28, -22, 70,
				-1, -69, 98, 18, 28, -56, -49, -16, -65, 39, -74, 19, -77, -96, 123, 123, -13, -79, -119, 86, -100, -117, 89, -32, 31, 104, 61, 121, 63, 27,
				53, -55, -17, 106, 81, -77, -11, 124, 2, 78, 54, -98, 122, 57, 97, -115, 92, 109, -102, 33, 56, -66, -88, -116, 72, 41, 103, 58, -57, -90, 74,
				108, 46, -65, -29, 98, 18, 118, -3, -48, 109, -93, 38, -26, -75, 28, 63, -80, -101, 39, -89, 71, 59, 10, -90, 79, -118, 118, -14, 54, -87, 92,
				-51, 52, 107, 109, 115, -81, -101, 21, 104, -107, 95, -52, -32, -16, 56, 116, -114, -48, -24, 90, 31, -7, -32, -2, 43, 124, 121, -72, -73, -56,
				74, 6, -107, 30, -71, 70, -48, -22, -12, 118, 88, 107, 84, 61, -32, 12, -76, -30, 28, -21, -64, -80, 126, -11, 17, -37, 17, 4, -65, 30, -48,
				-23, 87, -6, -15, 97, -66, -13, 82, 18, -60, -7, -35, 87, 82, -48, 20, -99, 86, 9, -117, 67, -93, 109, -79, 108, -37, -7, -60, -103, -6, 97,
				-41, -26, -40, 93, -105, -109, 113, 65, 94, 89, -77, -68, 81, -125, -13, -64, 8, 9, -104, 110, 112, 108, 17, -22, -62, -61, 92, 120, 60, 16,
				-110, -122, -113, 87, -97, -28, -49, 78, 61, 16, -12, 76, -126, 11, -72, -23, 50, 97, 118, 53, -50, 33, -23, -125, 41, 49, -41, -26, 103, -101,
				-49, -60, 67, -22, -9, 8, -120, -61, 71, -31, -37, 106, -5, -84, -4, 12, 25, 100, 10, 90, 113, -102, -107, -100, 66, -128, 14, -116, -56, -57,
				-29, 88, 81, -33, 53, 21, -65, -108, -74, -49, -68, -28, 32, 87, -47, -44, -77, -8, 64, 52, 62, -62, 124, 118, 106, -19, -14, 47, -99, -72,
				-64, -13, -70, 8, -114, 41, 76, -128, 9, -17, -114, -32, -8, 47, -79, -14, 1, -3, -45, -28, 114, 114, -47, 4, -89, 124, 118, 88, -121, 70, -58,
				-35, -67, -90, 66, 1, 84, 85, 24, -92, -32, 4, -93, 105, 42, 44, 92, 123, -123, -37, -73, -100, -117, -57, -119, -41, 84, 121, 69, 83, -65,
				126, 105, -23, -43, 79, 24, 104, -27, 79, -22, -41, 77, -89, -123, -44, -107, -112, -12, -89, 23, 0, -10, -53, 43, -26, 106, -19, -77, 15, 114,
				-88, 43, 19, 114, 106, -120, 9, -26, -65, -68, 39, -21, -25, 98, -105, 105, -42, 40, 81, 5, 117, 101, -107, 82, 67, 76, -60, -67, 72, -119, 8,
				-107, 56, 45, 43, -60, -29, -116, 15, 13, -76, -14, 39, -84, -17, 43, -66, -110, 101, -53, 15, -15, 32, -127, 17, 83, -1, -48, 24, -75, -20,
				-126, -14, -54, -50, 5, 60, 19, 61, 119, -81, 118, -12, -40, -19, 44, -120, 3, 91, -50, 37, 28, 13, -49, -92, -26, 36, -56, 94, 123, 126, 100,
				47, -112, 19, 119, -59, 115, -30, 126, 0, -80, -128, 114, 122, -14, -108, 53, 116, -65, -54, -81, -29, 103, -56, 31, -58, -105, 72, 62, -73,
				58, -62, 90, -33, -38, 7, 77, 37, -83, -80, -9, 3, -1, 49, 43, 71, 84, 1, 77, 17, -83, -80, 119, 111, -1, 49, 107, -93, -6, 27, 52, -110, -76,
				-62, -6, 22, -53, -54, -78, -70, -122, -11, 59, 92, 7, 100, 17, -67, 77, -87, 70, 0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96, -126 };
		boolean[][] isImage = { { false, false }, { false, false } };
		boolean[][] isCenter = { { true, true }, { false, true } };
		boolean[][] isBold = { { true, true }, { false, false } };
		String[][] bgcolor = { { "000000", "000000" }, { "FFFFFF", "FFFFFF" } };
		String[][] fontColors = { { "FFFFFF", "FFFFFF" }, { "000000", "000000" } };
		int[][] fontSize = { { 20, 20 }, { 15, 15 } };

		Table table = new Table();
		Object[][] objects = { { "Logo", "Test" }, { "Logo", null } };

		int[] widths = { 1000, 17000 };
		List<TableRow> rows = new ArrayList<TableRow>();
		for (int i = 0; i < objects.length; i++) {
			TableRow row = new TableRow();
			row.setHeight(3000);
			List<TableCell> rowData = new ArrayList<TableCell>();
			for (int j = 0; j < 2; j++) {
				TableCell cell = new TableCell(objects[i][j], widths[j], isImage[i][j], isCenter[i][j], bgcolor[i][j], fontColors[i][j], fontSize[i][j],
						isBold[i][j]);
				rowData.add(cell);
			}
			row.setCells(rowData);
			rows.add(row);
		}
		table.setRows(rows);
		table.setDoc(doc);
		table.setRowCount(2);
		table.setCellCount(3);

		XWPFTable tb = writeTable(table);
		//mergeCellsHorizontal(tb, 0, 0, 1);
		// XWPFTable tb = writeTable(table);
		mergeCellsVertically(tb, 1, 0, 1);

		writeToFile(doc, file);
	//	System.out.println("generate document success.");
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值