POI Word document (Letter Template)

Ads
 

POI Word document (Letter Template)

Dear Team,

  • i need code for generating word document(letter format).
  • i am unable to get the code for formats, font settings, letter type settings.

    please help me for the same.

Thanks in Advance.

View Answers

July 20, 2011 at 4:07 PM

import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class WriteInDocument{
public static void main(String[] args)throws IOException{
File file = new File("C:/sample.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!");
run.setFontSize(2 * 18);
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream(new File("C:/new.doc"));
doc.write(out);
out.flush();
out.close();
 }
}

July 21, 2011 at 12:47 PM

HI, Thank you for you reply,

  • Here Presently downloaded the jar file "poi-scratchpad-3.5-FINAL " from POI site.
  • This jar file doesn't having the package "poifs.filesystem"
  • Which jar file i need to consider from where ??
  • I am also generating the Excels, with this jar file, package "poi.hssf.usermodel" , here i don't have any problem

Regards, Shashi Kanth


July 21, 2011 at 4:28 PM

File sample_file = new File("sample.doc");
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(sample_file));
    HWPFDocument doc = new HWPFDocument(fs);"
--am getting following error at web page : 


      cannot access org.apache.poi.POIDocument file org\apache\poi\POIDocument.class not found 
HWPFDocument doc = new HWPFDocument(fs); ^ 1

July 22, 2011 at 11:15 AM

Hi , System still showing the same error,

**POIDocument file org\apache\poi\POIDocument.class not found HWPFDocument**

Eclipse not showing any cause/error , am getting this error while running my application, control not even entering in to the first jsp line too.

I think here am using the wrong jar file(Version difference), please let me know, Which jar file we need to put in our application to Use the above code and to avoid this error ?

Thanks in Advance ,

Shashi


April 6, 2012 at 12:06 PM

Hi, I am getting following Exception when trying to run this code.

java.lang.NoSuchMethodError: org.apache.poi.POIDocument.< init >

Code Snippet:

try {

            File file = new File(externalPath + "/abc.doc");
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
            HWPFDocument doc = new HWPFDocument(fs);
            Range range = doc.getRange();
            CharacterRun run = range.insertAfter("Hello World!");
            run.setFontSize(2 * 18);
            run.setBold(true);
            run.setItalic(true);
            run.setCapitalized(true);
            OutputStream out = new FileOutputStream(new File(externalPath + "/agnew.doc"));
            doc.write(out);
            out.flush();
            out.close();

        } catch (Exception ex) {
            Log.e("Exception==","=="+ex.toString());
              ex.printStackTrace();
        }

Logcat : FATAL EXCEPTION: main : java.lang.NoSuchMethodError: org.apache.poi.POIDocument. : at org.apache.poi.hwpf.HWPFDocumentCore.(HWPFDocumentCore.java:145) : at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:218) : at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:186) : at com.vikas.prudent.CreateDocument.onCreate(CreateDocument.java:45) : at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) : at android.app.ActivityThread.access$2300(ActivityThread.java:125) : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) : at android.os.Handler.dispatchMessage(Handler.java:99) : at android.os.Looper.loop(Looper.java:123) : at android.app.ActivityThread.main(ActivityThread.java:4627) : at java.lang.reflect.Method.invokeNative(Native Method) : at java.lang.reflect.Method.invoke(Method.java:521) : at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) : at dalvik.system.NativeStart.main(Native Method)

Ads









Related Tutorials/Questions & Answers:
POI Word document (Letter Template)
POI Word document (Letter Template)  Dear Team, i need code for generating word document(letter format). i am unable to get the code for formats, font settings, letter type settings. please help me for the same. Thanks
Edit word document Headers and Footers using POI Library in java?
Edit word document Headers and Footers using POI Library in java?  Hi All, I need code to modify the Header and Footer contents of a ms-word template using POI or JACOB libraries. Could any one help me? Thanks Advance
Advertisements
How to hide text in Word Document using Apache POI library
How to hide text in Word Document using Apache POI library  Is it possible to hide text in MS Word Document using Apache POI library? If yes, please let me know how. I am able to create and read the document using POI library
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
Write String in Word document
Write String in Word document  How to read and write strings in word document
how to delete a letter in a word?
how to delete a letter in a word?  how to delete a letter in a word? for example if i enter= roseindia, i want to delete 's', then output= roeindia
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
maven dependency for apache poi word
to pom.xml file in my project for getting the apache poi word library? I am searching for maven dependency for apache poi word. Thanks   Hi, The apache poi word library is distributed as zip download and also on the public maven
convert word document to pdf
convert word document to pdf  Hi, Could you please help me write a java code that converts the word docuemnt into PDF? I have to save the generated PDFs also. I got a code like the below, and i am unable to download the office
store form data into word document
store form data into word document  i want form page data, ex username phone number.. to store it in a word document file
letter counting for the smallest word
letter counting for the smallest word  How do I program my java... and it works EXCEPT that it counts all of the letters not just the smallest word...(); int count=ch.length; System.out.println("Count of smallest word
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
java code to open and display MS-word document
java code to open and display MS-word document  java code to open and display MS-word document
upload and retrieve a word document - JSP-Servlet
upload and retrieve a word document  Hi, I have a problem with uploading a word document to My sql database to a BLOB data type variable. And also unable to retrieve the word document from database. I am writing the code
ModuleNotFoundError: No module named 'document-template'
ModuleNotFoundError: No module named 'document-template'  Hi, My... named 'document-template' How to remove the ModuleNotFoundError: No module named 'document-template' error? Thanks   Hi, In your
open document
open document  open a word document using jsp   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
How to set a microsoft word document PASSWORD by using Visual Basic 2008 express?
How to set a microsoft word document PASSWORD by using Visual Basic 2008 express?   How to set a microsoft word document PASSWORD by using Visual Basic 2008 express
Will i be able to get the content of a webpage in a word document (Note:i don t have access to the source code)through java code?
Will i be able to get the content of a webpage in a word document (Note:i don t... am getting output to this code as html tags.... But what i want is in a word document to the webpage(for which i don t have access to source code
Create Excel(.xlsx) document using Apache POI
Create Excel(.xlsx) document using Apache POI In this section, you will learn how to create a Excel sheet having .xlsx extension using Apache POI library. In the given below example, we will going to create excel document having one
Read Excel(.xlsx) document using Apache POI
Read Excel(.xlsx) document using Apache POI In this section, you will learn how to read Excel file having .xlsx extension using Apache POI library. In the below example, we will read excel document having one sheet named as "new
Version of poi>poi dependency
List of Version of poi>poi dependency
Create Simple Excel(.xls) document using Apache POI
Create Simple Excel(.xls) document using Apache POI In this section, you... POI library. In the given below example, we will going to create a simple excel document having one sheet named as "FIRST SHEET" which have values
Version of poi>poi-contrib dependency
List of Version of poi>poi-contrib dependency
Version of poi>poi-contrib-2.5.1-final dependency
List of Version of poi>poi-contrib-2.5.1-final dependency
Version of poi>poi-scratchpad dependency
List of Version of poi>poi-scratchpad dependency
Version of poi>poi-scratchpad-2.5.1-final dependency
List of Version of poi>poi-scratchpad-2.5.1-final dependency
Read Simple Excel(.xls) document using Apache POI
Read Simple Excel(.xls) document using Apache POI In this section, you will learn how to read Excel file having .xls extension using Apache POI library. In the below example, we will read excel document having one sheet named as "

Ads