package com.xinlong.test;
import java.io.File;
import java.io.FileFilter;
import java.util.Calendar;
import java.util.Date;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class ExcelToPdf {
private String path;
public static boolean runFlag=false;
public ExcelToPdf(String path)
{
this.path=path;
}
//原始
public void saveExcelAsPdf1(String filePath,String outFile){
ComThread.InitSTA();
ActiveXComponent actcom=new ActiveXComponent("Excel.Application");
try{
System.out.println((new Date()).toString()+" start convert from : "+filePath+" to "+outFile);
actcom.setProperty("Visible", new Variant(false));
Dispatch excels=actcom.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(excels,"Open",Dispatch.Method,
new Object[]{filePath,new Variant(false),new Variant(false)},
new int[9] ).toDispatch();
Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{outFile,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]);
Dispatch.call(excel, "Close",new Variant(false));
if(actcom!=null){
actcom.invoke("Quit",new Variant[]{});
actcom=null;
}
ComThread.Release();
File temp=new File(filePath);
//temp.renameTo(new File(filePath+"."+getDateStr()));
temp.renameTo(new File(filePath));
temp=new File(filePath);
// temp.deleteOnExit(); //自动删除
temp=null;
System.out.println((new Date()).toString()+" convert ok : "+filePath+" to "+outFile);
}catch(Exception es){
es.printStackTrace();
}
}
/***
* office 转换pdf
* @param docfile 要转换的路径
* @param toFile 转换后的路径
* @param type 57-ppt,17-doc,32-xls
*/
public void saveExcelAsPdf(String docfile, String toFile){
ComThread.InitSTA();
ActiveXComponent app=null;
Dispatch docs =null;
int type=17;
String typedoc=docfile.substring(docfile.lastIndexOf("."));
if(typedoc.equals(".xls")||typedoc.equals(".xlsx")){
type=32;
}else if(typedoc.equals(".ppt")||typedoc.equals(".pptx")){
type=57;
}
try {
if(type==32)
{
app = new ActiveXComponent("PowerPoint.Application"); // 启动PowerPoint
app.setProperty("Visible", new Variant(true)); //转ppt的时候把false改为了true
docs = app.getProperty("Presentations").toDispatch(); //ppt
}else if(type==57)
{
app = new ActiveXComponent("Excel.Application"); // 启动Excel
app.setProperty("Visible", new Variant(false));
docs = app.getProperty("Workbooks").toDispatch(); //excel
}else if(type==17)
{
app = new ActiveXComponent("Word.Application"); // 启动word
app.setProperty("Visible", new Variant(false));
docs = app.getProperty("Documents").toDispatch(); //word
}
Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
toFile, new Variant(type) }, new int[1]);
if(type!=32)
{
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f); //转ppt的时候把这两行代码去掉
}
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
public void listAllFile(){
runFlag=true;
String fileName="",appdex="";
File temp=null;
try{
File [] list=new File(path).listFiles(new FileFilter(){
public boolean accept(File pathname) {
boolean x = false;
if (pathname.getName().toLowerCase().endsWith(".doc")||pathname.getName().toLowerCase().endsWith(".xls")||pathname.getName().toLowerCase().endsWith(".ppt")) {
c+=1; System.out.println(pathname+"==="+c);
x = true;
}
return x;
}
});
//System.out.println((new Date()).toString()+" Total Convert File : "+list.length);
// for(int i=0;i<list.length;i++){
// fileName=list[i].getName().substring(0, list[i].getName().indexOf("."));
// appdex=list[i].getName().substring(list[i].getName().indexOf("."));
// temp=new File(path+fileName+".pdf");
// if(temp.exists()){
// temp.renameTo(new File(path+fileName+"-"+getDateStr()+".pdf"));
// }
// saveExcelAsPdf(path+fileName+appdex,path+fileName+".pdf");
// System.out.println(path+fileName+appdex);
runFlag=false;
// }
}catch(Exception ex){
ex.printStackTrace();
}
runFlag=false;
}
public String getDateStr(){
Calendar cl=Calendar.getInstance();
cl.setTime(new Date());
String str=cl.get(Calendar.YEAR)+""+(cl.get(Calendar.MONTH)+1)+""
+cl.get(Calendar.DATE)+""+cl.get(Calendar.HOUR)+""+cl.get(Calendar.MINUTE)+""
+cl.get(Calendar.SECOND);
str="9月加班.xls";
return str;
}
public void pp()
{
// ComThread.InitSTA();
//
// ActiveXComponent slideApp = new ActiveXComponent("PowerPoint.Application");
//
// Dispatch presentations = slideApp.getProperty("Presentations").toDispatch();
//
// Dispatch presentation = Dispatch.call(presentations, "Open", inputFile.getAbsolutePath(),
// MsoTriState.msoTrue.getInteger(), // ReadOnly
// MsoTriState.msoFalse.getInteger(), // Untitled The Untitled parameter is used to create a copy of the presentation.
// MsoTriState.msoFalse.getInteger() // WithWindow
// ).toDispatch();
}
private long[] count = new long[] { 0, 0 };
private File file;
int c=0;
private long[] listFile(String path) {
file = new File(path);
File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
count[0]++;
//System.out.println(f[i].getPath());
this.listFile(f[i].getPath());
this.path=f[i].getPath();
listAllFile();
} else {
count[1]++;
}
}
//System.out.println("shu="+c);
return count;
}
/**
* 得到指定路径下的文件和文件夹数量
*
* @param path
* 要查看的路径
* @return object[0]耗时(毫秒)<br>
* object[1]文件夹数量<br>
* object[2]文件数量
*/
public Object[] getFileCount(String path) {
long t = System.currentTimeMillis();
long[] count = this.listFile(path);
//System.out.println(path);
t = System.currentTimeMillis() - t;
Object[] o = new Object[] { Long.valueOf(t), Long.valueOf(count[0]),
Long.valueOf(count[1])};
return o;
}
public static void main(String[] args) {
ExcelToPdf l = new ExcelToPdf("");
Object[] count = l.getFileCount("E:\\肖秋峰");
System.out.println("用时:"+count[0]);
System.out.println("文件夹个数:"+count[1]);
System.out.println("共文件"+count[2]);
}
}
//public static void main(String[] args) {
//
// ExcelToPdf e = new ExcelToPdf("D:\\质量管理体系文件\\");
// //e.listAllFile();
e.saveExcelAsPdf("D:\\转换\\加班时间.xlsx","D:\\转换\\j.pdf");
System.out.println("222.111.docx".substring("222.111.docx".lastIndexOf(".")));
//
//}
//}
java 获取文件夹目录下的子目录或子目录下的文件
原创mb64216b23e45d0 博主文章分类:java ©著作权
文章标签 java System PowerPoint 文章分类 Python 后端开发
-
java 读取目录下的文件 包含子目录
p
System List java