poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Majumdar, Sanjib (CORP, GEITC, Contractor)" <sanjib.majum...@ge.com>
Subject Writing to Excel file using POI
Date Thu, 23 Oct 2003 15:04:31 GMT
Hi,
  
   I am trying to input some data in a template Excel file. 
   While reading the file I am not getting any problem but the file is getting corrupted and
unable to open after I am trying to write some data inside that.
   
   My question, is it possible to write some rows of data in a pre-defined Excel file ?
  
   I am sending my Program.
 
   It will be great benifitial if I get any good suggestion to implement sucessfully.
 
Sanjib 
 
 
import org.apache.poi.hssf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;

import java.util.ResourceBundle;
import java.util.Properties;
import java.util.Iterator;
 
import report.PLUtil;
 

public class TestExcel
{
 HSSFWorkbook wb;
    HSSFSheet sheet;
 
 int totRows = 65;
    HSSFRow rows[] = new HSSFRow[totRows];
 
 //****************************************************************
    private static final String fileDir = PLUtil.FILE_DIR;
    private static final String fileContext = PLUtil.FILE_CONTEXT;
    private static final String fileLocn = PLUtil.REPORT_FILE_DIR;
    private static final String filePath = fileDir + fileContext + fileLocn;
    //****************************************************************
    private static final String prop_class = "config";
    private static final ResourceBundle oProp = ResourceBundle.getBundle(prop_class);
    private static final Properties props = System.getProperties();
    //****************************************************************
 

    TestExcel() {
     try {
        String file = "Template1.xls";
        FileInputStream fileIn = null;
        fileIn = new FileInputStream(filePath + "/" + file);
        wb = new HSSFWorkbook(fileIn);
        fileIn.close();
     } catch(Exception ex) {
      System.err.println("Error in Constructor :: " + ex);
     }
     }
 
 public static void main(String[] args) {
  System.out.println("Processing ...");
  new TestExcel().readExcelData(null);
  System.out.println("Done !!");
 }
 
 public void readExcelData(ArrayList list) {
  sheet = wb.getSheet("COMP CARD");
  
  drawData();
  writeExcelFile(wb, filePath);
 
    }
 
   private void writeExcelFile(HSSFWorkbook wb, String dirName)
   {
  try {
   System.out.println("Reading the Excel File...");
            String file = "Template1.xls";
            FileOutputStream fileOut = null;
            fileOut = new FileOutputStream(dirName + "/" + file);
   wb.write(fileOut);
            fileOut.close();
   System.out.println("Excel File has been read");
        } catch (IOException ioe) {
            System.err.println("Test::writeExcelFile : " + ioe);
        }
   }
 
   private HSSFRow[] getRows(int howmany) {
        for (int i = 0; i < howmany; ++i) {
            rows[i] = sheet.getRow(i);
        }
        return rows;
      }
    private HSSFCell printData(int rowno, int startCellno, String data) {
          HSSFCell cell = rows[rowno].getCell((short) startCellno);
          cell.setCellValue(data);
           return cell;
       }
 
     private void drawData() {
      System.out.println("Writing Data...");
            rows = getRows((short)10);
            HSSFCell cell;
            cell = printData(7, 0, "Testing");
 
     }
 
 }

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message