58,452
社区成员
发帖
与我相关
我的任务
分享
public static void print(String path)throws SystemException {
String PRINT_NAME="xxxxxxxxx";//打印机名称
//初始化COM线程
ComThread.InitSTA();
ActiveXComponent xl=new ActiveXComponent("Excel.Application");
try {
//设置是否显示打开Excel
Dispatch.put(xl, "Visible", new Variant(true));
//打开具体的工作簿
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
Dispatch excel=Dispatch.call(workbooks,"Open",path).toDispatch();
//设置打印属性并打印
Dispatch.callN(excel,"PrintOut",new Object[]{Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),
new Boolean(false),PRINT_NAME, new Boolean(true),Variant.VT_MISSING, ""});
//关闭文档
Dispatch.call(excel, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
throw new SystemException(e.getMessage());
} finally{
xl.invoke("Quit",new Variant[0]);
//始终释放资源
ComThread.Release();
}
}
这段代码里。换成word时,
//设置打印属性并打印
Dispatch.callN(excel,"PrintOut",new Object[]{Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),
new Boolean(false),PRINT_NAME, new Boolean(true),Variant.VT_MISSING, ""});
这里设置打印机属性需要改动么
public class SaveWordAspdf {
public static void saveWordAspdf(String filePath, String outFile) throws Exception {
ComThread.InitSTA();
ActiveXComponent actcom = new ActiveXComponent("Word.Application");
Dispatch documents = null;
Dispatch document = null;
try {
actcom.setProperty("Visible", new Variant(false));
documents = actcom.getProperty("Documents").toDispatch();
document = Dispatch.invoke(documents,"Open", Dispatch.Method, new Object[] {
filePath, new Variant(false), new Variant(false)
}, new int[17]).toDispatch();
Dispatch.invoke(document, "SaveAs", Dispatch.Method, new Object[] {
outFile,new Variant(false),new Variant(false)},new int[17]);
Dispatch.call(document, "Close", new Object[] { new Variant(false) });
if (actcom != null) {
actcom.invoke("Quit", new Variant[] {});
actcom = null;
}
ComThread.Release();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (actcom != null) {
actcom.invoke("Quit", new Variant[] {});
actcom = null;
}
ComThread.Release();
}
}
public static void print(String path)throws Exception {
String PRINT_NAME="pdfFactory Pro";//打印机名称
//初始化COM线程
ComThread.InitSTA();
ActiveXComponent doc=new ActiveXComponent("Word.Application");
try {
//设置是否显示打开word
Dispatch.put(doc, "Visible", new Variant(true));
//打开具体的工作簿
Dispatch documents = doc.getProperty("Documents").toDispatch();
Dispatch word = Dispatch.call(documents,"Open",path).toDispatch();
//设置打印属性并打印
Dispatch.callN(word,"PrintOut",new Object[]{Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),
new Boolean(false),PRINT_NAME, new Boolean(true),Variant.VT_MISSING, ""});
//关闭文档
Dispatch.call(word, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
} finally{
doc.invoke("Quit",new Variant[0]);
//释放资源
ComThread.Release();
}
}
public static void main(String[] args) {
try {
System.out.println(0);
saveWordAspdf("d:/1234.doc","d:/1234.pdf");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}