Women in Technology

Hear us Roar



Article:
  Opening Microsoft File Formats to Java
Subject:   how to use poi package in my project
Date:   2005-09-23 03:15:46
From:   janaki1
Hai all,
This IS Chaitanya
I down load the "poi-bin-2.5.1-final-20040804.ZIP
And I wrote the program like this


import com.sun.rsasign.s;
import java.io.*;
import java.util.*;


public class Rdata {

/** Creates a new instance of Rdata */
public Rdata() {
}
public static void main(String a[])throws Exception
{
FileReader fr=new FileReader("c:\\java\\abc.doc");
FileWriter fw=new FileWriter("C:\\java\\a.txt");
FileWriter fw1=new FileWriter("C:\\java\\b.doc");
int i;
char ch;
String str="",f="";
while((i=fr.read())!=-1)


{
ch=(char)i;
if(ch!='\n')
{
str=str+ch;
}
else
{
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens())
{
f=st.nextToken();
f.trim();
f.toLowerCase();
{
while((i=fr.read())!=-1)
{
ch=(char)i;
fw.write(ch);
fw.write("\r\n");

}
}
{
fw1.write(ch);
fw1.write("\r\n");
}
}
str="";
}

}


fw.close();
fw1.close();
}

}
BUT WHEN I AM OEN THE .DOC FILRS Unexpected garbage values.How to use this poi package in my program.I want Immediate solution to this can Any body help me plz.
thanks in advance.

Full Threads Newest First

Showing messages 1 through 3 of 3.

  • how to use poi
    2006-01-02 01:57:35  kalaisai [View]

    hai ,
    i'm new to this POI in java.i know JSF.how to run a small prg in this.plz give idea.what are the files needed.how to run it etc.
    plz do so help me itz urgent.
    thanks in advance
    by
    kalaivani





    • how to use poi
      2006-12-13 02:11:32  VasanthaKumar [View]

      Hi,

      I am working in JSF, i displayed the list of records by using datatable, now i want to convert this list of records into Excel Sheet, if u know how to convert into Excel please let me know.

      Thanks
      Vasanth
    • how to use poi
      2007-08-02 23:40:44  TARZAN [View]

      Hi...
      i am new to this forum and this is the easy way to use poi package.example code is below which reads the data from "new.txt"(data is:aa/bb/cc/dd/4Mhz/66/gg/hh/ii/jj/kk/ll/44GHZ/123/oo/) and stores this data into the "today.xls" under C drive. i am extracted this data based on key "/" and each data is stored in one cell liek aa in (0,0)and bb in (0,1)like wise.

      once you compile and run new file will be created and data will be written that is "aa
      aa" in two cells and again this code is executed it appends the new data which is read from "new.txt" with old data in same file.

      **********************************************
      import java.io.*;
      import org.apache.poi.poifs.filesystem.*;
      import org.apache.poi.hssf.usermodel.*;
      import org.apache.poi.hssf.usermodel.HSSFCellStyle;
      import org.apache.poi.hssf.util.*;
      //import org.apache.poi.hssf.util.HSSFColor;


      class exceltest{

      public static void main(String arg[])throws IOException{


      File f=new File("c:\\today.xls");


      if(f.exists()){

      FileInputStream fin=new FileInputStream(f);
      FileInputStream fin1=new FileInputStream("c:\\new.txt");
      int size=fin.available();
      byte arr[]=new byte[size];
      fin1.read(arr,0,size-2);
      String str=new String(arr);
      String strarr[]=new String[14];
      strarr=str.split("/");

      HSSFWorkbook wb=new HSSFWorkbook(fin);
      HSSFColor col=new HSSFColor.SEA_GREEN();


      HSSFCellStyle cs=wb.createCellStyle();
      HSSFFont style= wb.createFont();
      style.setFontHeightInPoints((short)10);

      cs.setFillPattern(HSSFCellStyle.FINE_DOTS);
      //cs.setFillBackgroundColor(col.getIndex());
      style.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
      style.setColor(HSSFFont.COLOR_RED);
      HSSFSheet ws1=wb.getSheet("January");
      HSSFRow wr1=null;

      Integer c=ws1.getLastRowNum();
      int count=c;
      Integer c1=new Integer(1);
      System.out.println("Before : "+c.toString(c));
      short rownum1=c.shortValue();
      short rownum2;
      rownum2=c1.shortValue();
      rownum1=(short)(rownum1+rownum2);
      System.out.println("After : "+Short.toString(rownum1));

      cs.setFont(style);

      wr1=ws1.createRow(rownum1);
      for(short cellnum1=0;cellnum1 HSSFCell wc1=wr1.createCell(cellnum1);
      //wc1.setCellStyle(cs);

      String ss=strarr[cellnum1].trim();
      if(ss.equals("66")||ss.equals("123")){
      System.out.println(ss);
      wc1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

      }
      else{
      wc1.setCellType(HSSFCell.CELL_TYPE_STRING);

      }
      wc1.setCellValue(strarr[cellnum1].trim());
      }

      FileOutputStream fout=new FileOutputStream(f);
      wb.write(fout);
      fout.close();
      }
      else{

      FileOutputStream fout=new FileOutputStream("c:\\today.xls");
      HSSFWorkbook wb=new HSSFWorkbook();
      System.out.println("Writing...");
      HSSFSheet ws=wb.createSheet("January");
      HSSFRow wr=null;

      for(short rownum=0;rownum<2;rownum++){
      wr=ws.createRow(rownum);
      for(short cellnum=0;cellnum<2;cellnum++){
      HSSFCell wc=wr.createCell(cellnum);
      wc.setCellValue("aa");
      }
      }

      wb.write(fout);
      fout.close();
      }

      }

      }

      before executing this code you require jakarta POIpackage any version but i checked with poi 3.0
      steps:
      1.download the package and extract the file andyou may find the file like "poi-3.0-rc4-20070503.jar" and put it in c drive or whichever you feel

      2.compile the code (Note: specifying the jar file is must otherwise may get error like package HSSFWorkbook does not exit and etc..)

      javac -Xlint -classpath c:\poi-3.0-rc4-20070503.jar exceltest.java

      3.Run the application
      java -cp c:\poi-3.0-rc4-20070503.jar;. exceltest

      Note: this gives the 2-warnings