by prasathm » Fri Feb 10, 2012 12:15 am
package org.FileReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.poifs.filesystem.POIFSDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.employeeDetails.EmployeeDetails;
public class DocReader {
public void readDocFile() {
String filesname = "C:/temp/dotPERFORMANCE_APPRAISAL.dot";
POIFSFileSystem fs = null;
try {
File file = new File(filesname);
fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
String paragraphstr = we.getText();
StringBuilder stringBuilder = new StringBuilder(
paragraphstr.length());
EmployeeDetails details = new EmployeeDetails();
details.setEmployeeId("123456789");
details.setEmployeeName("xxxxxx");
details.setQualification("B.E");
details.setEmployeeJoiningDate("04/01/2012");
details.setEvaluationPeriod("2 years");
details.setCurrentJobTitle("trainee");
details.setDepartment("java");
details.setSupervisor("yyyyy");
details.setEvaluationDate("05/6/2012");
/*
for (int i = 0; i < paragraphstr1.length; i++) { paragraphstr1[i]
= paragraphstr1[i] .replaceAll("<jwEmployeeId>",
details.getEmployeeId()) .replaceAll("<jwEmployeeName>",
details.getEmployeeName()) .replaceAll("<jwQualification>",
details.getQualification()) .replaceAll("<jwEOJDate>",
details.getEmployeeJoiningDate()) .replaceAll("<jwEvalPeriod>",
details.getEvaluationPeriod()) .replaceAll("<jwCurrJob>",
details.getCurrentJobTitle()) .replaceAll("<jwDept>",
details.getDepartment()) .replaceAll("<jwSupervisor>",
details.getSupervisor()) .replaceAll("<jwEvalDate>",
details.getEvaluationDate());
System.out.println(paragraphstr1[i]); }
*/
if (filesname.endsWith(".doc") || filesname.endsWith(".dot")) {
String message = paragraphstr.replaceAll("<jwEmployeeId>",
details.getEmployeeId());
String message1 = message.replaceAll("<jwEmployeeName>",
details.getEmployeeName());
String message2 = message1.replaceAll("<jwQualification>",
details.getQualification());
String message3 = message2.replaceAll("<jwEOJDate>",
details.getEmployeeJoiningDate());
String message4 = message3.replaceAll("<jwEvalPeriod>",
details.getEvaluationPeriod());
String message5 = message4.replaceAll("<jwCurrJob>",
details.getCurrentJobTitle());
String message6 = message5.replaceAll("<jwDept>",
details.getDepartment());
String message7 = message6.replaceAll("<jwSupervisor>",
details.getSupervisor());
String finalMessage = message7.replaceAll("<jwEvalDate>",
details.getEvaluationDate());
finalMessage.trim();
stringBuilder.append(finalMessage);
System.out.println("message" + stringBuilder.toString());
/*String path = file.getCanonicalPath();
System.out.println("abs path" + file.getAbsolutePath());
POIFSDocument event = new POIFSDocument("output.doc",
new FileInputStream(new File(
"c:/temp/output/output1.doc")));
event.getViewableIterator();
doc.getSavedByTable();*/
Range range = doc.getRange();
CharacterRun run = range.insertBefore(finalMessage.toString());
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream(new File(
"c:/temp/output/output1.doc"));
doc.write(out);
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DocReader reader = new DocReader();
reader.readDocFile();
}
}
package org.employeeDetails;
import java.io.Serializable;
public class EmployeeDetails implements Serializable {
private static final long serialVersionUID = -943913338383549645L;
private String employeeId;
private String employeeName;
private String qualification;
private String employeeJoiningDate;
private String evaluationPeriod;
private String currentJobTitle;
private String department;
private String supervisor;
private String evaluationDate;
public final String getEmployeeId() {
return employeeId;
}
public final void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public final String getEmployeeName() {
return employeeName;
}
public final void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public final String getQualification() {
return qualification;
}
public final void setQualification(String qualification) {
this.qualification = qualification;
}
public final String getEmployeeJoiningDate() {
return employeeJoiningDate;
}
public final void setEmployeeJoiningDate(String employeeJoiningDate) {
this.employeeJoiningDate= employeeJoiningDate;
}
public final String getEvaluationPeriod() {
return evaluationPeriod;
}
public final void setEvaluationPeriod(String evaluationPeriod) {
this.evaluationPeriod = evaluationPeriod;
}
public final String getCurrentJobTitle() {
return currentJobTitle;
}
public final void setCurrentJobTitle(String currentJobTitle) {
this.currentJobTitle = currentJobTitle;
}
public final String getDepartment() {
return department;
}
public final void setDepartment(String department) {
this.department = department;
}
public final String getSupervisor() {
return supervisor;
}
public final void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public final String getEvaluationDate() {
return evaluationDate;
}
public final void setEvaluationDate(String evaluationDate) {
this.evaluationDate= evaluationDate;
}
}
construct your own file using these two codes.
also make sure ur input file location
Last edited by
prasathm on Wed Feb 15, 2012 4:35 pm, edited 1 time in total.
prasath..