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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
> 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: [EMAIL PROTECTED]
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: [EMAIL PROTECTED]
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/