When Java reads a word document, although there are many plug-ins poi, java2Word, jacob, itext, etc. introduced on the Internet, poi cannot read the format (the new API seems to be still in the research and development stage, not very stable, and I am not afraid to do projects. Use); java2Word, jaco b is easy to report errors and cannot find the registration, which is quite strange. I have tried it on different machines, and the operation methods are exactly the same. Some machines do not report errors, and some report errors. I went to their forum to find an expert to solve it, but I couldn’t tell the reason. Project It's a bit confusing to deploy and use it; itxt seems to be very convenient to write, but I checked the information for a long time and I haven't seen a good way to read it. After some selection, it is best to use RTF as a compromise. After all, RTF is an open source format and does not require any plug-ins. It only requires basic IO operations and encoding conversion. On the surface, rtf format files are no different from doc. They can be opened with word and various formats can be set.
----- Functions implemented: Read the RTF template content (format and text content), replace the changed parts, and form a new RTF document.
----- Implementation idea: The fixed parts in the template are entered manually, and the changed parts are represented by $info$. Just replace $info$.
1. Read the RTF template content in byte form
2. Convert variable content strings to RTF encoding
3. Replace the variable parts in the original text to form a new RTF document
The main procedures are as follows:
public String bin2hex(String bin) { char[] digital = "0123456789ABCDEF".toCharArray(); StringBuffer sb = new StringBuffer(""); byte[] bs = bin.getBytes(); int bit; for (int i = 0; i < bs.length;i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append("//'"); sb.append(digital[bit]); bit = bs[i] & 0x0f; sb.append(digital[bit]); } return sb.toString(); } public String readByteRtf(InputStream ins, String path){ String sourcecontent = ""; try{ ins = new FileInputStream(path); byte[] b = new byte[1024]; if (ins == null) { System.out.println("Source template file does not exist"); } int bytesRead = 0; while (true) { bytesRead = ins.read(b, 0, 1024 ); // return final read bytes counts if(bytesRead == -1) {// end of InputStream System.out.println("End of reading template file"); break; } sourcecontent += new String(b, 0, bytesRead); // convert to string using bytes } }catch(Exception e){ e.printStackTrace(); } return sourcecontent ;}
The above is the core code, and the remaining part is replacement. The String.replace(oldstr, newstr); method in java can be reassembled and implemented, so I won’t post it here. Please see the attachment for source code details.
Prerequisites for running source code:
Create the YQ directory on the c drive, copy the "template.rtf" in the attachment to the YQ directory, and run the OperatorRTF.java file. The file name will be generated in the YQ directory, such as: 21:15:19_cheney_record .rtf files.
package com; import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.text. SimpleDateFormat;import java.util.Date; public class OperatorRTF { public String strToRtf(String content){ char[] digital = "0123456789ABCDEF".toCharArray(); StringBuffer sb = new StringBuffer(""); byte[] bs = content.getBytes(); int bit; for (int i = 0; i < bs .length; i++) { bit = (bs[i] & 0x0f0) >> 4; sb.append("//'"); sb.append(digital[bit]); bit = bs[i] & 0x0f; sb.append(digital[bit]); } return sb.toString(); } public String replaceRTF(String content,String replacecontent,int flag){ String rc = strToRtf(replacecontent); String target = ""; if(flag==0){ target = content.replace("$timetop$",rc); } if(flag==1){ target = content.replace("$info$",rc); } if( flag==2){ target = content.replace("$idea$",rc); } if(flag==3){ target = content.replace("$advice$",rc); } if(flag= =4){ target = content.replace("$infosend$",rc); } return target; } public String getSavePath() { String path = "C://YQ"; File fDirecotry = new File(path); if (!fDirecotry.exists ()) { fDirecotry.mkdirs(); } return path; } public String ToSBC(String input){ char[] c = input.toCharArray(); for (int i = 0; i < c.length; i++){ if (c[i] == 32){ c[i] = (char) 12288; continue; } if (c[i] < 127){ c [i] = (char) (c[i] + 65248); } } return new String(c); } public void rgModel(String username, String content) { // TODO Auto-generated method stub Date current=new Date(); SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String targetname = sdf.format(current).substring(11 ,13) + "hour"; targetname += sdf.format(current).substring(14,16) + "minute"; targetname += sdf.format(current).substring(17,19) + "seconds"; targetname += "_" + username +"_record.rtf"; String strpath = getSavePath(); String sourname = strpath+"// "+"template.rtf"; String sourcecontent = ""; InputStream ins = null; try{ ins = new FileInputStream(sourname); byte[] b = new byte[1024]; if (ins == null) { System.out.println("Source template file does not exist"); } int bytesRead = 0; while (true) { bytesRead = ins.read(b, 0, 1024 ); // return final read bytes counts if(bytesRead == -1) {// end of InputStream System.out.println("End of reading template file"); break; } sourcecontent += new String(b, 0, bytesRead); // convert to string using bytes } }catch(Exception e){ e.printStackTrace(); } String targetcontent = ""; String array[] = content.split(" ~"); for(int i=0;i<array.length;i++){ if(i==0){ targetcontent = replaceRTF(sourcecontent, array[i], i); }else{ targetcontent = replaceRTF(targetcontent, array[i], i); } } try { FileWriter fw = new FileWriter(getSavePath()+"//" + targetname,true); PrintWriter out = new PrintWriter(fw); if(targetcontent.equals ("")||targetcontent==""){ out.println(sourcecontent); }else{ out.println(targetcontent); } out.close(); fw.close(); System.out.println(getSavePath()+" File generated in this directory" + targetname + "Success"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub OperatorRTF oRTF = new OperatorRTF(); String content = "From 9:00 on October 12, 2008 to 6:00 on October 12, 2008 ~ we refer to the method of testing drugs ~ we refer to the method of testing drugs ~ we refer to the method of testing drugs ~ we refer to testing Drug method"; oRTF.rgModel("cheney",content); }}
Example of using POI to read tabular data from a word file:
<span style="font-size:14px;">package com.poi.world; import java.io.FileInputStream; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Paragraph ; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.hwpf.usermodel.TableRow ; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class POI_Word{ public static void main(String[] args){ try { String[] s=new String[20]; FileInputStream in=new FileInputStream("D://mayi.doc"); POIFSFileSystem pfs=new POIFSFileSystem(in); HWPFDocument hwpf= new HWPFDocument(pfs); Range range =hwpf.getRange(); TableIterator it=new TableIterator(range); int index=0; while(it.hasNext()){ Table tb=(Table)it.next(); for(int i=0;i<tb.numRows();i++){ //System.out. println("Numrows :"+tb.numRows()); TableRow tr=tb.getRow(i); for(int j=0;j<tr.numCells();j++){ //System.out.println("numCells :"+tr.numCells()); // System.out.println("j :"+j) ; TableCell td=tr.getCell(j); for(int k=0;k<td.numParagraphs();k++){ //System.out.println("numParagraphs :"+td.numParagraphs()); Paragraph para=td.getParagraph(k); s[index]=para.text().trim(); index++; } } } } // System.out.println(s .toString()); for(int i=0;i<s.length;i++){ System.out.println(s[i]); } } catch (Exception e) { e.printStackTrace(); } } }</span>