954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Apache Poi for doc

Hello Friends...
I am new to jsp and i need to edit a .doc file present in a server using jsp!
i tried googling and i got to know about "apache poi".
this is working great for opening a doc file and also for writing a doc file but my problem is i have to edit a doc file and i have to save that file with same name!
but i have two codes one to display doc file in textarea and another one to write one doc file to another one
now i need both at one place that is it has to open a file and it has to edit and save as same as original file!!
here are two codes i have!!

This is for displaying doc file

<%@ page import="java.io.*,java.io.*,org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.extractor.WordExtractor" %>


<%
File file = null;
WordExtractor extractor = null ;
String res=null;

try {

file = new File("xyz.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)

res+=fileData[i];

}
%>
<form>

<textarea >
<%out.println(res);%>
</textarea>
<input type="submit" value="submit"/>
</form>
<%

}
catch(Exception exep)
{

out.println(exep.getMessage());
}
%>


This is for Saving one file to another

<%@ page import="java.io.File,java.io.FileInputStream,java.io.FileOutputStream,java.io.IOException,java.io.OutputStream,org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.usermodel.CharacterRun,org.apache.poi.hwpf.usermodel.Range,org.apache.poi.poifs.filesystem.POIFSFileSystem"%>


<%
File file = new File("xyz.doc");

try {

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!!!");
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream outa = new FileOutputStream(new File("D:/sample2.doc"));
doc.write(outa);
out.println(outa);
out.flush();
out.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}



%>

They both are working perfectly but i need to edit file taken from a file name and to save it again...
please friends help me!!!
thanking u !!!

shivarocks
Newbie Poster
10 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

hello sir,
i know how to write one .doc file content into another .doc file using poi in jsp but i want to write some text into a .doc file using poi,
so please can anyone help me in doing that
code for writing to one file to another file using jsp is below!!!

<%@ page import="java.io.File,java.io.FileInputStream,java.io.FileOutputStream,java.io.IOException,java.io.OutputStream,org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.usermodel.CharacterRun,org.apache.poi.hwpf.usermodel.Range,org.apache.poi.poifs.filesystem.POIFSFileSystem"%>


<%
File file = new File("xyz.doc");

try {

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!!!");
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream outa = new FileOutputStream(new File("D:/sample.doc"));
doc.write(outa);
out.println(outa);
out.flush();
out.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}



%>


the above code is used to save file contents of xyz.doc to sample.doc but i need to write a string in that page and i have to store it in sample.jsp ,please help me in doing that!!!
thanking u!!!

shivarocks
Newbie Poster
10 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

1. Just because you didn't get answer in 24 hours you feel like you are entitled to create another new thread?
2. Why are you misusing JSP for something that is not supposed to do? Do document manipulation in servlet and just forward necessary data to page view.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

sir thanks for your reply can you help me how to do that i am new to jsp and i didnt started servlets...so please help me!!!

shivarocks
Newbie Poster
10 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: