3

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

我想要这样的输出。

在此处输入图像描述

4

2 回答 2

1

如果您正在谈论 Office Word 文档,您应该查看Apache POI。它可以让您从 Java 编辑 MS Office 文件。

于 2013-10-14T09:39:29.630 回答
1

莎拉,下面应该做的是获取您的字符串,获取我们想要着色的单词的索引点(就像以前一样),然后它将创建一个新的 ArrayList ,其中将包含您的字符串的字母以及我们想要着色的单词. 例如,如果您的字符串是“abi​​gdog”,并且您想要大颜色,我制作了一个 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

}
}

祝你好运!

于 2013-10-24T10:51:23.190 回答