Java 突出显示word中的子字符串

Java 突出显示word中的子字符串,java,regex,ms-word,highlight,Java,Regex,Ms Word,Highlight,我想使用java代码突出显示word文档中的字符串模式。我已经在java中识别了该模式,现在我想在word文档中突出显示该模式。我想在word文件中写入字符串,并突出显示字符串中的模式 我希望我的输出像这样 如果您正在谈论Office Word文档,您应该签出。它将允许您从Java编辑MS Office文件。Sarah,下面应该做的是获取您的字符串,获取我们想要着色的单词的索引点(如前所述),然后它将创建一个新的ArrayList,其中将包含您的字符串字母和我们想要着色的单词。例如,如果您的字符

我想使用java代码突出显示word文档中的字符串模式。我已经在java中识别了该模式,现在我想在word文档中突出显示该模式。我想在word文件中写入字符串,并突出显示字符串中的模式

我希望我的输出像这样


如果您正在谈论Office Word文档,您应该签出。它将允许您从Java编辑MS Office文件。

Sarah,下面应该做的是获取您的字符串,获取我们想要着色的单词的索引点(如前所述),然后它将创建一个新的ArrayList,其中将包含您的字符串字母和我们想要着色的单词。例如,如果您的字符串是“abigdog”,并且您想将其涂成大颜色,那么我制作了一个ArrayList,如下所示:

element1: a
element2: big
element3: d
element4: o
element5: g
在创建角色运行时,我们迭代ArrayList,根据需要为相关元素/单词着色

public class SO4 {
public static void main(String[] args) {

    String words = "adaahhhccadcdaccaaddadddcchah";  //String from doc
    String[] wordsToColour = {"ahhh", "acca", "adda", "hah" };   //Words we want to find
    ArrayList<Integer> points = new ArrayList<Integer>(); //Hold indexes of words

    //Get the points of the words to colour first and add them to points arrayList
    int j, k =0, count = 0;
    String checker = "";
    while(count < wordsToColour.length){ //For each search word
        j = 0; //start of search word
        k = wordsToColour[count].length();  //end of current search word
        while(k < words.length() + 1){ //we will search whole string
            checker = words.substring(j, k);  //String equal to a substring the same length as the word we are searching for 
                if(checker.equals(wordsToColour[count])){ //If we find our word
                    points.add(Integer.valueOf(j)); //Store co-ordinates in arrayList, co-ordinate being the index at which the word is found
                    points.add(Integer.valueOf(k)); //Store co-ordinates in arrayList co-ordinate being the index at which the word is found
                }
            j++;
            k++;
        }//Inner loop
        count++;
    }//Outer loop

    //Checking the arrayList to see if it contains our words. 
/*        k=0; j=1;
     while(j < points.size()){  
         System.out.println(points.get(k) + " " + points.get(j) + " Word: " + words.substring(points.get(k), points.get(j)));
         k = (k+2);
        j = (j + 2);
     }   */


     //This will hold a reference to either a letter or a word to colour 
     ArrayList<String> wordsByElement = new ArrayList<String>(words.length());  
     k = 0; j = 0;
     String storage;  //Will hold reference to current substring
     while(j < words.length()){
         storage = words.substring(j, j+1); //Will iterate over each letter
         if(j == points.get(k)){ //If we hit a point stored in points array
             //Use points in array to add substring word to wordsByElement ArrayList
             wordsByElement.add(words.substring(points.get(k), points.get(k+1))); //Adding
             j = points.get(k + 1) -1; //Set j to continue at the point after the word we just extracted 
             k = (k + 2); //raise k to start point of next word we want
         }/*Inner if */ else { //If we didn't get a match
             wordsByElement.add(storage); //add the letter
         }
     j++;
     }//While loop end


             //This shows what is in the ArrayList and should show what I have done to this point
         for(String s : wordsByElement){
         System.out.println(s); //Checking what is in our ArrayList
     } 

      XWPFDocument newDoc = new XWPFDocument(); //Doc to write new doc to
      XWPFParagraph para = newDoc.createParagraph(); //Paragraph
      XWPFRun run = para.createRun();  //Where the text will be written from
     k=0;

     while(k < wordsByElement.size()){
         run = para.createRun();
         switch (wordsByElement.get(k)) {
        case "ahhh": //If word is ahhh
            run.setColor("ff5400"); //Set Color
            break;
        case "acca": //If word is acca
            run.setColor("a7bf42"); //set color
            break;
        case "adda": //So on
            run.setColor("7b8896"); //so forth
            break;
        case "hah":
            run.setColor("00adee");
            break;
        default: //If word is not one of the above
            run.setColor("000000"); //Color is black
            break;
        }

         run.setText((wordsByElement.get(k))); 
         k++;
     }

     try {
        newDoc.write(new FileOutputStream(new File("D:\\Users\\user277005\\Desktop\\testerFinished.docx")));

        //View our document
        if(Desktop.isDesktopSupported()){
            Desktop.getDesktop().open(new File("D:\\Users\\user277005\\Desktop\\testerFinished.docx"));
        }

    } catch (IOException e) {
        e.printStackTrace();
    } //save

}
}
公共类SO4{
公共静态void main(字符串[]args){
String words=“adaahhcadcdcdcdccaadddddcchah”;//来自文档的字符串
String[]wordstocolor={“ahhh”,“acca”,“adda”,“hah”};//我们要查找的单词
ArrayList points=new ArrayList();//保留单词的索引
//首先获取要着色的单词的点,并将它们添加到点数组列表中
int j,k=0,count=0;
字符串检查器=”;
而(count

祝你好运

我想在word中存储一个字符串。该字符串将突出显示文本。有关详细信息,请参阅如何制作特定的欲望颜色?@Sarah我已重新调整了我的答案,以尝试帮助您更多,请看一看,看看是否有帮助。@Sarah我已进行了另一次重新调整,看起来有点吓人,但我已尝试尽可能多地进行评论。试试看,希望它能满足你的需要。@Sarah Sarah,我又为你重新设计了它,这次我把我先前的答案的一部分用了另一种方式。它应该允许您指定要着色的单词以及所需的颜色。请试一试,如果行得通,我可以为我的努力申请一个绿色的勾号吗?