将Enum作为参数传递给Java中的方法?

时间:2014-06-17 16:36:02

标签: java apache-poi openxml docx4j xmlbeans

我试图在接口CTTextParagraphProperties中调用docx4j方法“setAlgn”,根据我使用的docx4j jar,编译器将Enum类型作为参数。我正在传递实际参数STTextAlignType.CTR,我相信它应该解析为值'ctr'(引用:http://grepcode.com/file/repo1.maven.org/maven2/org.docx4j/docx4j/3.0.1/org/docx4j/dml/STTextAlignType.java?av=f,我正在运行相同的代码)。

这是我的代码:

import java.lang.Enum;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFChildAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFShapeGroup;
import org.apache.poi.xssf.usermodel.XSSFTextBox;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.docx4j.dml.*;


        public static XSSFTextBox createTextBox(XSSFSheet sh, String message, int row1, int col1, int row2, int col2, boolean is_bold, boolean is_italics, boolean is_underline, boolean centered, int fontSize){

        //Various apache-poi stuff
        XSSFWorkbook wb = sh.getWorkbook();
        XSSFDrawing drawing = sh.createDrawingPatriarch();
        XSSFClientAnchor clientanchor = new XSSFClientAnchor(0,0,0,0,(short)col1,row1,(short)col2,row2);
        XSSFChildAnchor childanchor = new XSSFChildAnchor(0,0,0,0);
        XSSFShapeGroup group = drawing.createGroup(clientanchor);
        XSSFTextBox textbox = group.createTextbox(childanchor);
        XSSFRichTextString richMessage = new XSSFRichTextString(message);
        XSSFFont textFont = wb.createFont();
        textFont.setFontHeightInPoints((short)fontSize);
        textFont.setFontName("Verdana");
        if(is_bold){
            textFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        }
        textFont.setItalic(is_italics);
        if(is_underline){
            textFont.setUnderline(XSSFFont.U_SINGLE);
        }
        if(centered){
        //Here is the code in question. 
        textbox.getCTShape().getTxBody().getPArray(0).getPPr().setAlgn(STTextAlignType.CTR);
        }
        richMessage.applyFont(textFont);
        textbox.setText(richMessage);
        return textbox;
    }

我的编译器返回以下错误消息:

com\tem\POIStuff.java:1105: error: method setAlgn in interface CTTextParagraphProperties cannot be applied to given types;
                    textbox.getCTShape().getTxBody().getPArray(0).getPPr().setAlgn(STTextAlignType.CTR);

最后,我的问题是如何让“setAlgn”方法接受'STTextAlignType.CTR'作为枚举而不是作为对象类型'STTextAlignType'?非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

问题实际上在您的代码段的第一行!你的问题是

import java.lang.Enum;

CTTextParagraphProperties.setAlgn确实采用类型为Enum的类,但它不是那种枚举。它必须是org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType.Enum

我建议您将导入切换为:

import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextAlignType.Enum; 

然后,您可以使用STTextAlignType.L之类的内容设置对齐方式,并且它可以正常工作