poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 51280] New: when we insert a new image to the existing excel file that corrupts the previous images
Date Sat, 28 May 2011 08:52:50 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=51280

             Bug #: 51280
           Summary: when we insert a new image to the existing excel file
                    that corrupts the previous images
           Product: POI
           Version: unspecified
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: major
          Priority: P2
         Component: HSSF
        AssignedTo: dev@poi.apache.org
        ReportedBy: shafiexp@gmail.com
    Classification: Unclassified


Created attachment 27079
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=27079
The Corrupted Excel File

Hi,

I'm opening the existing excel file(Which contains String data and images(.jpg)
and do the following operations.

1.adding new data to the existing file---its adding 
2.adding new image to the existing file -the previous image is corrupted


Give me the solutionnnnnn

The code I have Wriiten


package tfib.excel;

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

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Picture;

public class WriteExcel2 {

    private HSSFWorkbook workBook;
    private HSSFSheet sheet;
    private HSSFRow excelRow;
    private HSSFCell cell;

    private String excelFileName;
    private String sheetName;
    public WriteExcel2(String excelFileName,String sheetName) throws
IOException
    {
        this.excelFileName=excelFileName;
        this.sheetName=sheetName;
        System.out.println("CHecking Whether the File exists");

        File fileIn=new File(excelFileName);
        System.out.println("The Input File Name:"+fileIn);
        FileInputStream fInputStream;
        if(fileIn.exists())
        {
            System.out.println("WorkBook is Exist / Getting the Existing
Workbook");


                fInputStream = new FileInputStream(fileIn);
                POIFSFileSystem poiStream=new POIFSFileSystem(fInputStream);
                workBook=new HSSFWorkbook(poiStream);
                System.out.println("Getting The Existing Sheet From the
Existing Workbook");
                sheet=workBook.getSheet(sheetName);
                System.out.println("THe WorkBook: "+workBook);
                System.out.println("THe Sheet: "+sheet);
//                System.out.println("THe Sheet: "+sheet.getSheetName());
//                sheet.getDrawingPatriarch();
                if(sheet==null)
                {
                    System.out.println("Creating New Sheet in the Existing
Workbook");
                    sheet=workBook.createSheet(sheetName);
                }


        }
        else
        {
            System.out.println("Creating New WorkBook and New Sheet");
            workBook=new HSSFWorkbook();
            sheet=workBook.createSheet(sheetName);
            System.out.println("THe WorkBook: "+workBook);
            System.out.println("THe Sheet: "+sheet);
            System.out.println("THe Sheet: "+sheet.getSheetName());
        }
    }
    public void insertData(int row,int col,String data)
    {
        System.out.println("Creating Row,Cell and inserting Data into
WorkSheet");

            if(sheet!=null)
            {
                if(sheet.getRow(row)!=null)
                {
                    excelRow=sheet.getRow(row);
                }
                else
                {
                    excelRow=sheet.createRow(row);
                }

                cell=excelRow.createCell(col);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(new HSSFRichTextString(data));
                sheet.autoSizeColumn(col);
                System.out.println("The Data is inserted successfully in
WorkSheet");
            }
    }
    public void insertImage(int startRow,int startCol,int endRow,int
endCol,String imgPath) throws IOException
    {
        System.out.println("Inserting Picture into WorkSheet");
        ClientAnchor anchor;
        HSSFPatriarch patriarch;
        Picture picture;
        FileInputStream fileInput=new FileInputStream(imgPath);
        ByteArrayOutputStream img_bytes=new ByteArrayOutputStream();
        int b;
        while((b=fileInput.read())!=-1)
        img_bytes.write(b);
        fileInput.close();
        anchor=workBook.getCreationHelper().createClientAnchor();
        int picIndex=workBook.addPicture(img_bytes.toByteArray(),
HSSFWorkbook.PICTURE_TYPE_JPEG);
        anchor.setCol1(startCol);
        anchor.setRow1(startRow);
        anchor.setCol2(endCol);
        anchor.setRow2(endRow);
        anchor.setAnchorType(1);

        System.out.println("The Worbook  : "+workBook);
        System.out.println("The Sheet  : "+sheet);

        System.out.println("THe Existing Sheet Patriarch:
"+sheet.getDrawingPatriarch());

        patriarch=sheet.createDrawingPatriarch();

        System.out.println("THe PAtrich Val: "+patriarch);

        picture=patriarch.createPicture(anchor, picIndex);
//        pic.resize();
        System.out.println("The Image is inserted Successfully in workSheet");
    }

    public void writeDataToExcel() throws IOException 
    {    
        FileOutputStream outputExcelFile;
        System.out.println("Writing the workbook(cintains data) to Output Excel
File");
            System.out.println(excelFileName);
            System.out.println("The length of excel file :
"+excelFileName.length());
            outputExcelFile=new FileOutputStream(new File(this.excelFileName));
            workBook.write(outputExcelFile);
            System.out.println("THe Workbook is succesfully written to Output
Excel File");    

            if(outputExcelFile!=null)
            {
                outputExcelFile.flush();
                outputExcelFile.close();
            } 
        }
    }

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Mime
View raw message