java横向导出excel_java调用jacob生成pdf,word,excel横向

/** 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[]*/

public byte[] jacob_Office2Pdf(byte[] srcFileBytes, String postfix) {

if (srcFileBytes == null || srcFileBytes.length == 0

|| postfix.equals("") || postfix == null) {

return null;

} else {

String officeTmplPath = Consts.getTempPath()

+ UUID.randomUUID().toString() + "." + postfix;

System.out.println(officeTmplPath);

String pdfTmplPath = generateDefaultOutputFilePath(officeTmplPath);

System.out.println(pdfTmplPath);

FileOutputStream outf = null;

BufferedOutputStream bufferout = null;

try {

outf = new FileOutputStream(officeTmplPath);

bufferout = new BufferedOutputStream(outf);

bufferout.write(srcFileBytes);

bufferout.flush();

bufferout.close();

outf.close();

} catch (FileNotFoundException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

} catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

}

File f = new File(officeTmplPath);

if (postfix.equalsIgnoreCase("doc")

|| postfix.equalsIgnoreCase("docx")) {

ComThread.InitSTA();

ActiveXComponent app = new ActiveXComponent("Word.Application");

app.setProperty("Visible", false);

Dispatch docs = app.getProperty("Documents").toDispatch();

Dispatch doc = Dispatch.call(docs,// "Open", // officeTmplPath,//FileName

false,//ConfirmConversions

true //ReadOnly

).toDispatch();

Dispatch.call(doc,// "SaveAs", // pdfTmplPath, //FileName

wdFormatPDF);

Dispatch.call(doc, "Close", false);

if (app != null) {

app.invoke("Quit", new Variant[] {});

app = null;

}

ComThread.Release();

if (f.exists()) {

f.delete();

}

byte[] content;

try {

BufferedInputStream in = new BufferedInputStream(

new FileInputStream(pdfTmplPath));

ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

byte[] temp = new byte[1024];

int size = 0;

while ((size = in.read(temp)) != -1) {

out.write(temp, 0, size);

}

in.close();

content = out.toByteArray();

out.close();

File pdfFile = new File(pdfTmplPath);

if (pdfFile.exists()) {

pdfFile.delete();

}

return content;

} catch (FileNotFoundException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

} catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

}

} else if (postfix.equalsIgnoreCase("xls")

|| postfix.equalsIgnoreCase("xlsx")) {

ComThread.InitSTA();

ActiveXComponent app = new ActiveXComponent("Excel.Application");

app.setProperty("Visible", new Variant(false));

Object excels = app.getProperty("Workbooks").toDispatch();

Object excel = Dispatch.invoke(

(Dispatch) excels,

"Open",

Dispatch.Method,

new Object[] { officeTmplPath, new Variant(false),

new Variant(true) }, new int[1]).toDispatch();

//new Object[] { officeTmplPath, new Variant(false),//new Variant(false) }, new int[9]).toDispatch();//横向打印(2013/05/24)//获取activate表格

Dispatch currentSheet = Dispatch.get((Dispatch) excel,

"ActiveSheet").toDispatch();

Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup")

.toDispatch();

Dispatch.put(pageSetup, "Orientation", new Variant(2));

//Dispatch.invoke((Dispatch)excel, "SaveAs", Dispatch.Method,//new Object[] {//pdfTmplPath, new Variant(57), new Variant(false),//new Variant(57), new Variant(57), new Variant(false),//new Variant(true), new Variant(57), new Variant(false),//new Variant(true), new Variant(false) }, new int[1]);

try {

//如果第一个sheet为空则会抛出异常

Dispatch.call(currentSheet, "SaveAs", pdfTmplPath,

new Variant(57));

} catch (Exception e1) {

//TODO Auto-generated catch block//e1.printStackTrace();//自动调用第二个sheet

Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets")

.toDispatch();

//获得几个sheet//int count = Dispatch.get(sheets, "Count").getInt();//System.out.println(count);

Dispatch sheet = Dispatch.invoke(sheets, "Item",

Dispatch.Get, new Object[] { new Integer(2) },

new int[1]).toDispatch();

pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch();

Dispatch.put(pageSetup, "Orientation", new Variant(2));

Dispatch.call(sheet, "SaveAs", pdfTmplPath, new Variant(57));

Dispatch.call((Dispatch) excel, "Close", new Variant(false));

byte[] content;

try {

BufferedInputStream in = new BufferedInputStream(

new FileInputStream(pdfTmplPath));

ByteArrayOutputStream out = new ByteArrayOutputStream(

1024);

byte[] temp = new byte[1024];

int size = 0;

while ((size = in.read(temp)) != -1) {

out.write(temp, 0, size);

}

in.close();

content = out.toByteArray();

out.close();

File pdfFile = new File(pdfTmplPath);

if (pdfFile.exists()) {

pdfFile.delete();

}

return content;

} catch (FileNotFoundException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

} catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

}

} finally {

if (app != null) {

app.invoke("Quit", new Variant[] {});

app = null;

}

ComThread.Release();

if (f.exists()) {

f.delete();

}

}

Dispatch.call((Dispatch) excel, "Close", new Variant(false));

byte[] content;

try {

BufferedInputStream in = new BufferedInputStream(

new FileInputStream(pdfTmplPath));

ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

byte[] temp = new byte[1024];

int size = 0;

while ((size = in.read(temp)) != -1) {

out.write(temp, 0, size);

}

in.close();

content = out.toByteArray();

out.close();

File pdfFile = new File(pdfTmplPath);

if (pdfFile.exists()) {

pdfFile.delete();

}

return content;

} catch (FileNotFoundException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

} catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

return null;

}

} else {

if (f.exists()) {

f.delete();

}

return null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值