Ms Word 에서 Java
-
21-08-2019 - |
문제
내가 노력하고 열려있는 MS Word2003 문서에서 java 검색,지정된 문자열을 대체하는 새로운 문자열입니다.내가 사용하는 APACHE POI 습니다.내 코드는 다음과 같이 하나:
public void searchAndReplace(String inputFilename, String outputFilename,
HashMap<String, String> replacements) {
File outputFile = null;
File inputFile = null;
FileInputStream fileIStream = null;
FileOutputStream fileOStream = null;
BufferedInputStream bufIStream = null;
BufferedOutputStream bufOStream = null;
POIFSFileSystem fileSystem = null;
HWPFDocument document = null;
Range docRange = null;
Paragraph paragraph = null;
CharacterRun charRun = null;
Set<String> keySet = null;
Iterator<String> keySetIterator = null;
int numParagraphs = 0;
int numCharRuns = 0;
String text = null;
String key = null;
String value = null;
try {
// Create an instance of the POIFSFileSystem class and
// attach it to the Word document using an InputStream.
inputFile = new File(inputFilename);
fileIStream = new FileInputStream(inputFile);
bufIStream = new BufferedInputStream(fileIStream);
fileSystem = new POIFSFileSystem(bufIStream);
document = new HWPFDocument(fileSystem);
docRange = document.getRange();
numParagraphs = docRange.numParagraphs();
keySet = replacements.keySet();
for (int i = 0; i < numParagraphs; i++) {
paragraph = docRange.getParagraph(i);
text = paragraph.text();
numCharRuns = paragraph.numCharacterRuns();
for (int j = 0; j < numCharRuns; j++) {
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
System.out.println("Character Run text: " + text);
keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
key = keySetIterator.next();
if (text.contains(key)) {
value = replacements.get(key);
charRun.replaceText(key, value);
docRange = document.getRange();
paragraph = docRange.getParagraph(i);
charRun = paragraph.getCharacterRun(j);
text = charRun.text();
}
}
}
}
bufIStream.close();
bufIStream = null;
outputFile = new File(outputFilename);
fileOStream = new FileOutputStream(outputFile);
bufOStream = new BufferedOutputStream(fileOStream);
document.write(bufOStream);
} catch (Exception ex) {
System.out.println("Caught an: " + ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows.............");
ex.printStackTrace(System.out);
}
}
나는 이 함수를 호출된 다음 인수:
HashMap<String, String> replacements = new HashMap<String, String>();
replacements.put("AAA", "BBB");
searchAndReplace("C:/Test.doc", "C:/Test1.doc", replacements);
면 Test.doc 파일 포함한 다음과 같은 줄:"AAA EEE"그 성공적으로 작동하지만,때 사용하여 복잡한 파일은 콘텐츠를 읽을 성공적으로 생성 Test1.doc 파일을 열려고 할 때 그것은,그것이 나에게 다음과 같은 오류가:
말씀을 읽을 수 없습니다.그것과 함께 사용할 수 없습니다.중 하나 이상을 시도해 보십시오 다음과 같다:*열고 파일을 복구 할 수 없습니다.*파일을 열고 텍스트 복 변환기입니다.(C: est1.doc)
시 말해,무엇 때문에 저는 초보자에서 POI 발견하지 않았습니다 좋습니다.
해결책
당신은 시도 할 수 있습니다 OpenOffice API, 그러나 사용 방법을 알려주는 많은 자원이 있습니다.
다른 팁
우선 문서를 닫아야합니다.
그 외에도, 내가 제안하는 것은 원래 Word 문서를 단어 XML 문서로 다시 찾은 다음 확장을 수동으로 .xml에서 .doc으로 변경하는 것입니다. 그런 다음 작업중 인 실제 문서의 XML을보고 컨텐츠를 추적하여 실수로 16 진수 값을 편집하지 않도록하십시오 (AAA 및 EEE는 다른 필드에서 16 진수가 될 수 있음).
실제 Word 문서를 보지 않고 무슨 일이 일어나고 있는지 말하기는 어렵습니다.
POI에 대한 문서는 전혀 없습니다. 특히 불행히도 Word 문서의 경우.
나도 몰라:은 그것의 확인하는 나 자신에 대답하지만,단지식을 공유하는,나는 나 자신에 대답.
후에 웹을 탐색하고,최종 해결책을 발견입니다:이 라이브러리 docx4j 매우 좋을 다루는 MS word 파일이 있지만,그 문서는 충분하지 않은 지금까지 그는 포럼에서 여전히 시작 단계이지만,전반적으로 도움을 나에게 무엇이 필요..
감사 4 모든 사람에게 도움이다.
당신은 또한 이것을 시도 할 수 있습니다 : http://www.dancrintea.ro/doc-to-pdf/
보입니다 이것 문제가 될 수 있습니다.