Bug 19083

Summary: [NEEDS-INFO] Error Opening Excel File
Product: POI Reporter: Feng Yu <fy7175>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major CC: fy7175
Priority: P3    
Version: 1.0.2   
Target Milestone: ---   
Hardware: Other   
OS: All   

Description Feng Yu 2003-04-16 17:52:57 UTC
Hi, 

This bug is for POI HSSF project (in both release versions and most recent dev 
versions).

I use HSSFWorkbook to create 5 spreadsheets and save string values
to these 5 spreadsheets in a single Excel file. The Java program runs ok 
without any error. However, when I open the Excel file manually from my 384M 
RAM NT machine I receive "Not enough memory" message from Excel 97. I try to 
open the same file from 512M or above RAM W2K machine I get "Errors were 
detected but Microsoft was able to open the file by making repairs" message. In 
both cases, I look at the file opened in Excel 97, it seems that all data were 
there (there may have something corrupted that I did not notice). So why I 
still receive the error messages and  how do I get rid of the error messages?

If I create only one or two spreadsheets of above five at a time, I will not
receive any error message when I manually open the Excel file. I start to 
receive above error messages when I try to create three or more of them. 

I attached my source codes here.


Thanks,

Feng

-------------------------------------------------------------------------------

import java.lang.reflect.*;
import java.util.*;
import java.io.*;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;


/**
 *
 * =============================================================================
 * Description: This agent converts a table or set of tables into an Excel file,
 * 		which the user can then save on their hard drive.
 *
 */
public class HSSF
{
	
	private String fileName  = null;
	
	
// =============================================================================
	public HSSF(String fileName)
		throws IOException 
		
    	{
    		this.fileName = fileName;
    	}

	
// =============================================================================
	public void writeExcelSpreadsheetOut()
    	{
    	
    		FileOutputStream lOutputStream = null; 
    		BufferedOutputStream lBufferedStream = null;
    	
    		try
    		{
	    		// Create a new file
	    		// throws FileNotFoundException
	    		lOutputStream = new FileOutputStream( 
	    			this.fileName );
				
			// Create a buffered output stream
			lBufferedStream = new BufferedOutputStream( 
				lOutputStream );
				
			// create a new workbook
			HSSFWorkbook lWorkbook = new HSSFWorkbook( );
			
			// Create the sheets in the workbook.
			writeOneTable( lWorkbook );			
			writeOneTable( lWorkbook );
			writeOneTable( lWorkbook  );
			writeOneTable( lWorkbook  );
			writeOneTable( lWorkbook  );
			
			// Set the sheet names.
			lWorkbook.setSheetName( 0, "PB/NB" ); 
			lWorkbook.setSheetName( 1, "SWBT" ); 
			lWorkbook.setSheetName( 2, "SNET" ); 
			lWorkbook.setSheetName( 3, "AIT" ); 
			lWorkbook.setSheetName( 4, "Revenue Summary" ); 

			// throws IOException
			lWorkbook.write( lBufferedStream );
					
	   	}
    		catch ( FileNotFoundException e )
    		{
    			e.printStackTrace();
    		}
    	
    		catch ( IOException e )
    		{
    			e.printStackTrace();
    		}
    	
    		finally
        	{			
        		// Close the streams.
        		if ( lBufferedStream != null )
            		{
            			try
                		{
                			// throws IOException
                			lBufferedStream.close( );
               			}
                		catch ( IOException e )
                		{
                   			e.printStackTrace();
               	 		}
            		}
            
            		// Close the streams.
            		if ( lOutputStream != null )
            		{
            			try
                		{
                			// throws IOException
                			lOutputStream.close( );
                		}
                		catch ( IOException e )
                		{
                    			e.printStackTrace();
                		}
            	
        		}
   		 }
	}
    
// =============================================================================
	private void writeOneTable(HSSFWorkbook aWorkbook)
    	{
    	    	try
    		{
    			// create a new sheet
			HSSFSheet lSheet = aWorkbook.createSheet( );
			
			int lNumberOfRows = 10;
	
			// Get the number of columns from the table.
			int lColumnCount = 90;
			
			
			// Create the first row on the sheet.
			HSSFRow lFirstRow = lSheet.createRow( ( short )0 );
				
			
			
			// Loop through the columns and write the column 
headers to the 
			// first row.
			// Make sure to calculate the width needed.
			for( int lColumnHeaderIndex = 0; lColumnHeaderIndex < 
lColumnCount;
				lColumnHeaderIndex++ )
			{
				HSSFCell lColumnCell = lFirstRow.createCell( 
					( short )lColumnHeaderIndex ); 
				
				// Create a cell style
				HSSFCellStyle lCellStyle = 
aWorkbook.createCellStyle( );
				
				// Create a font.
				HSSFFont lFont = aWorkbook.createFont( );
				
				// Set the font to have bold characteristics.
				lFont.setBoldweight( HSSFFont.BOLDWEIGHT_BOLD );
				
				
				lCellStyle.setFont( lFont );
				
				// Set this for the cell.
				lColumnCell.setCellStyle( lCellStyle );
				
					
				lColumnCell.setCellValue( "This is a Test. " );
								 
				
				// Set the correct width using the multiplier
				lSheet.setColumnWidth( ( short )
lColumnHeaderIndex, 
					(short) 2000 );
	
			}			
			
			// Write a for loop here.  Start with the second row to 
accomodate the
			// column headers in the first row.  
			// For every row, and every cell
			
			
			for( int lRowIndex = 1; lRowIndex <= lNumberOfRows; 
lRowIndex++ )
			{
				// Create a new row on the sheet.
				HSSFRow lRow = lSheet.createRow( ( short )
lRowIndex );
				
				// Loop through each column and assign to a 
cell.
				for( int lColumnIndex = 0; lColumnIndex < 
lColumnCount;	
					lColumnIndex++ )
				{	
					// Create a cell for each column
					HSSFCell lCell = lRow.createCell( ( 
short )lColumnIndex ); 
						
					// Fill the cell with data.  We get 
this from the table and not
					// the model since we are representing 
what the user sees on 
					// the screen.
						
					lCell.setCellValue( "123456" ); 
				}		
			}
			
			
    	}
  	catch ( Exception e )
    	{
    		e.printStackTrace();
    	}
    }


    public static void main(String [] args)
    {

    	try
	{
		System.out.println("Start creating xls file.");
		HSSF hssf = new HSSF("MochaTest.xls");
		System.out.println("In process...");
		hssf.writeExcelSpreadsheetOut();
		System.out.println("File created!");
	}
	catch (Exception e )
	{
	   	e.printStackTrace();
    	}
    }

}
Comment 1 Andy Oliver 2003-07-24 16:16:24 UTC
can you be more specific on what versions?  To be honest I never read this
because I saw the 1.0.2 at the top.  Can you attach the code via create new
attachment, most folks can't cut and paste it, it gets mangled.  Leaving open
for more info.  Please remove [NEEDS-INFO] from the subj once its added
Comment 2 Jason Height 2004-10-14 06:51:45 UTC
This is fixed in HEAD. Maybe it has been fixed for some time.

It might be because, Using the code below i got an IllegalArgumentException
because the sheet name contained a '/' maybe that had something to do with it.
We have only recently begun throwing this as an error.

Jason