You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by kruce lee <ll...@gmail.com> on 2006/09/12 04:20:46 UTC

How to print excel files automatically

Hi everybody
How to print a excel file automatically when I get the file path or resource.
Can someone give me a snippet.
Any help would be highly appreciated
kruce.

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: How to print excel files automatically

Posted by kruce lee <ll...@gmail.com>.
Hi, ererybody.
I succeed in printing excel file automatically.
FYI, work fine in windows xp with excel 2003

/**
 * @author Kruce Lee
 * @since 2006-9-13
 */
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class AppExcelPrinter
{
	private ActiveXComponent excel;

	// excel workbooks
	private Dispatch workbooks;

	// excel file varient
	private Variant workbook;

	/**
	 * Constructor
	 *
	 */
	public AppExcelPrinter()
	{
	}

	/*
	 * non-thread-safe
	 *
	 */
	public synchronized void print(String filename)
	{
		try
		{
			// start the Excel
			excel = new ActiveXComponent("Excel.Application");
			
			// first time, we need set the excel to be invisible
			excel.setProperty("Visible", new Variant(false));
			
			// get workbooks
			workbooks = excel.getProperty("WorkBooks").toDispatch();
			workbook = Dispatch.callN(workbooks, "Open", new Object[] { filename });

			// Dispatch.call(Dispatch.get(workbook.toDispatch(),"ActiveSheet").toDispatch(),"PrintOut");
			Dispatch.call(Dispatch.get(workbook.toDispatch(),
"Worksheets").toDispatch(), "PrintOut");
			try
			{
				Thread.sleep(100);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		finally
		{
			// quit the excel application
			excel.invoke("Quit", new Variant[] {});
			
			// invoke the method to count down the numbers of the reference,
			// and release them one by one to kill the excel process finally.
			ComThread.Release();
		}
	}

	/**
	 * @param args
	 * Test suites
	 */
	public static void main(String[] args)
	{
		AppExcelPrinter printer = new AppExcelPrinter();
		printer.print("D:\\coffee.xls");
	}
}

On 9/12/06, Anthony Andrews <py...@yahoo.com> wrote:
> That does sound familiar!
>
> There is one thing I should warn you about however, Microsoft warn against controlling an application such as Excel - and you will need to drive an instance of Excel I think - using COM/OLE from within another application. There are a number of reasons given but the main one seems to be that Excel is designed to be used interactively and becuase there is no easy way to trap and handle the error message it issues then the user could quite quickly run into problems. Having said that, I did it after writing my own OLE code.
>
> kruce lee <ll...@gmail.com> wrote: Hi, Anthony Andrews, Thank you in advance.
> The tool you recommend should be Jacob and I will also dig Mucrosoft's
> COM API for more info.
>
>
> On 9/12/06, Anthony Andrews
>  wrote:
> > As far as I am aware (and bear in mind that I could very well be wrong) HSSF is an API that allows you to generate Excel spreadsheets and does not have any support for printing. I managed to get around exactly the same problem by using OLE but this is a far from perfect solution as it caused almost as many problems as it solved; it is however one possible solution.
> >
> > I know there is a tool that someone wrote that is a wrapper around JNI, making it far more easy to use external applications in a way that mimics Microsoft's COM model. I cannot remember the name at the moment but will have a dig around on my PC as I seem to remember testing it sometime in the past.
> >
> > kruce lee  wrote: Hi everybody
> > How to print a excel file automatically when I get the file path or resource.
> > Can someone give me a snippet.
> > Any help would be highly appreciated
> > kruce.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> > Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> > The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
> >
> >
> >
> >
> > ---------------------------------
> > Get your email and more, right on the  new Yahoo.com
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
>  Everyone is raving about the  all-new Yahoo! Mail.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: How to print excel files automatically

Posted by David Fisher <df...@jmlafferty.com>.
Be very careful if you decide to auto-start PowerPoint or Excel in a  
background user. Make sure you are doing it AFTER the GDI environment  
is setup. To go too early will lead you into Windows restart hell.

Of course, if Microsoft cared to they could make these applications  
wait in this case... instead power-off / safe mode blah blah -  
argghhh! where's my blood pressure medicine.

Good luck.

Dave

On Sep 12, 2006, at 8:53 AM, Anthony Andrews wrote:

> That does sound familiar!
>
> There is one thing I should warn you about however, Microsoft warn  
> against controlling an application such as Excel - and you will  
> need to drive an instance of Excel I think - using COM/OLE from  
> within another application. There are a number of reasons given but  
> the main one seems to be that Excel is designed to be used  
> interactively and becuase there is no easy way to trap and handle  
> the error message it issues then the user could quite quickly run  
> into problems. Having said that, I did it after writing my own OLE  
> code.
>
> kruce lee <ll...@gmail.com> wrote: Hi, Anthony Andrews, Thank  
> you in advance.
> The tool you recommend should be Jacob and I will also dig Mucrosoft's
> COM API for more info.
>
>
> On 9/12/06, Anthony Andrews
>  wrote:
>> As far as I am aware (and bear in mind that I could very well be  
>> wrong) HSSF is an API that allows you to generate Excel  
>> spreadsheets and does not have any support for printing. I managed  
>> to get around exactly the same problem by using OLE but this is a  
>> far from perfect solution as it caused almost as many problems as  
>> it solved; it is however one possible solution.
>>
>> I know there is a tool that someone wrote that is a wrapper around  
>> JNI, making it far more easy to use external applications in a way  
>> that mimics Microsoft's COM model. I cannot remember the name at  
>> the moment but will have a dig around on my PC as I seem to  
>> remember testing it sometime in the past.
>>
>> kruce lee  wrote: Hi everybody
>> How to print a excel file automatically when I get the file path  
>> or resource.
>> Can someone give me a snippet.
>> Any help would be highly appreciated
>> kruce.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
>> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
>> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>>
>>
>>
>>
>> ---------------------------------
>> Get your email and more, right on the  new Yahoo.com
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>
>
>
>  		
> ---------------------------------
> Do you Yahoo!?
>  Everyone is raving about the  all-new Yahoo! Mail.


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


RE: How to print excel files automatically

Posted by Tahir Akhtar <ta...@spectrum-tech.com>.
Another solution would be to add a macro to automatically call the print
function of excel right after loading the excel file. But it will cause a
lot of security warnings to user. 


-----Original Message-----
From: Anthony Andrews [mailto:pythonaddict@yahoo.com] 
Sent: Tuesday, September 12, 2006 6:53 PM
To: POI Users List
Subject: Re: How to print excel files automatically

That does sound familiar!

There is one thing I should warn you about however, Microsoft warn against
controlling an application such as Excel - and you will need to drive an
instance of Excel I think - using COM/OLE from within another application.
There are a number of reasons given but the main one seems to be that Excel
is designed to be used interactively and becuase there is no easy way to
trap and handle the error message it issues then the user could quite
quickly run into problems. Having said that, I did it after writing my own
OLE code.

kruce lee <ll...@gmail.com> wrote: Hi, Anthony Andrews, Thank you in
advance.
The tool you recommend should be Jacob and I will also dig Mucrosoft's
COM API for more info.


On 9/12/06, Anthony Andrews 
 wrote:
> As far as I am aware (and bear in mind that I could very well be wrong)
HSSF is an API that allows you to generate Excel spreadsheets and does not
have any support for printing. I managed to get around exactly the same
problem by using OLE but this is a far from perfect solution as it caused
almost as many problems as it solved; it is however one possible solution.
>
> I know there is a tool that someone wrote that is a wrapper around JNI,
making it far more easy to use external applications in a way that mimics
Microsoft's COM model. I cannot remember the name at the moment but will
have a dig around on my PC as I seem to remember testing it sometime in the
past.
>
> kruce lee  wrote: Hi everybody
> How to print a excel file automatically when I get the file path or
resource.
> Can someone give me a snippet.
> Any help would be highly appreciated
> kruce.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>
>
>
> ---------------------------------
> Get your email and more, right on the  new Yahoo.com
>

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/




 		
---------------------------------
Do you Yahoo!?
 Everyone is raving about the  all-new Yahoo! Mail.

-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.12.3/445 - Release Date: 9/11/2006
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.12.3/445 - Release Date: 9/11/2006
 



---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: How to print excel files automatically

Posted by Anthony Andrews <py...@yahoo.com>.
That does sound familiar!

There is one thing I should warn you about however, Microsoft warn against controlling an application such as Excel - and you will need to drive an instance of Excel I think - using COM/OLE from within another application. There are a number of reasons given but the main one seems to be that Excel is designed to be used interactively and becuase there is no easy way to trap and handle the error message it issues then the user could quite quickly run into problems. Having said that, I did it after writing my own OLE code.

kruce lee <ll...@gmail.com> wrote: Hi, Anthony Andrews, Thank you in advance.
The tool you recommend should be Jacob and I will also dig Mucrosoft's
COM API for more info.


On 9/12/06, Anthony Andrews 
 wrote:
> As far as I am aware (and bear in mind that I could very well be wrong) HSSF is an API that allows you to generate Excel spreadsheets and does not have any support for printing. I managed to get around exactly the same problem by using OLE but this is a far from perfect solution as it caused almost as many problems as it solved; it is however one possible solution.
>
> I know there is a tool that someone wrote that is a wrapper around JNI, making it far more easy to use external applications in a way that mimics Microsoft's COM model. I cannot remember the name at the moment but will have a dig around on my PC as I seem to remember testing it sometime in the past.
>
> kruce lee  wrote: Hi everybody
> How to print a excel file automatically when I get the file path or resource.
> Can someone give me a snippet.
> Any help would be highly appreciated
> kruce.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>
>
>
> ---------------------------------
> Get your email and more, right on the  new Yahoo.com
>

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/




 		
---------------------------------
Do you Yahoo!?
 Everyone is raving about the  all-new Yahoo! Mail.

Re: How to print excel files automatically

Posted by kruce lee <ll...@gmail.com>.
Hi, Anthony Andrews, Thank you in advance.
The tool you recommend should be Jacob and I will also dig Mucrosoft's
COM API for more info.


On 9/12/06, Anthony Andrews <py...@yahoo.com> wrote:
> As far as I am aware (and bear in mind that I could very well be wrong) HSSF is an API that allows you to generate Excel spreadsheets and does not have any support for printing. I managed to get around exactly the same problem by using OLE but this is a far from perfect solution as it caused almost as many problems as it solved; it is however one possible solution.
>
> I know there is a tool that someone wrote that is a wrapper around JNI, making it far more easy to use external applications in a way that mimics Microsoft's COM model. I cannot remember the name at the moment but will have a dig around on my PC as I seem to remember testing it sometime in the past.
>
> kruce lee <ll...@gmail.com> wrote: Hi everybody
> How to print a excel file automatically when I get the file path or resource.
> Can someone give me a snippet.
> Any help would be highly appreciated
> kruce.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>
>
>
> ---------------------------------
> Get your email and more, right on the  new Yahoo.com
>

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: How to print excel files automatically

Posted by David Fisher <df...@jmlafferty.com>.
Kruce,

http://j-integra.intrinsyc.com/

Before we switched to using POI for creation we used this package to  
script the creation of image only PPT by driving PowerPoint via COM.  
It cost $5000 per server 3 years ago.

You might also look into openoffice.org if you are not on a windows  
machine.

Regards,
Dave

On Sep 12, 2006, at 2:43 AM, Anthony Andrews wrote:

> As far as I am aware (and bear in mind that I could very well be  
> wrong) HSSF is an API that allows you to generate Excel  
> spreadsheets and does not have any support for printing. I managed  
> to get around exactly the same problem by using OLE but this is a  
> far from perfect solution as it caused almost as many problems as  
> it solved; it is however one possible solution.
>
> I know there is a tool that someone wrote that is a wrapper around  
> JNI, making it far more easy to use external applications in a way  
> that mimics Microsoft's COM model. I cannot remember the name at  
> the moment but will have a dig around on my PC as I seem to  
> remember testing it sometime in the past.
>
> kruce lee <ll...@gmail.com> wrote: Hi everybody
> How to print a excel file automatically when I get the file path or  
> resource.
> Can someone give me a snippet.
> Any help would be highly appreciated
> kruce.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
> The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/
>
>
>
>  		
> ---------------------------------
> Get your email and more, right on the  new Yahoo.com


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/


Re: How to print excel files automatically

Posted by Anthony Andrews <py...@yahoo.com>.
As far as I am aware (and bear in mind that I could very well be wrong) HSSF is an API that allows you to generate Excel spreadsheets and does not have any support for printing. I managed to get around exactly the same problem by using OLE but this is a far from perfect solution as it caused almost as many problems as it solved; it is however one possible solution.

I know there is a tool that someone wrote that is a wrapper around JNI, making it far more easy to use external applications in a way that mimics Microsoft's COM model. I cannot remember the name at the moment but will have a dig around on my PC as I seem to remember testing it sometime in the past.

kruce lee <ll...@gmail.com> wrote: Hi everybody
How to print a excel file automatically when I get the file path or resource.
Can someone give me a snippet.
Any help would be highly appreciated
kruce.

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/



 		
---------------------------------
Get your email and more, right on the  new Yahoo.com