develog

excel open protected password in jacob 본문

Dev/Java

excel open protected password in jacob

냐옴 2013. 6. 8. 23:01

JACOB - Java COM Bridge

http://sourceforge.net/projects/jacob-project/


-------------------------------------------------------------------------------


import java.io.File;


import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;



public class TestMain {


public static void main(String[] args) throws Exception {

System.out.println("START");

System.out.println("------------------------------");

TestMain main = new TestMain();

main.setPassword(

"excel/source.xlsx", 

"excel/source.xls", 

"123"

);

System.out.println("------------------------------");

System.out.println("END");

}

public void setPassword(String sourceFileNm, String outputFileNm, String password) throws Exception {

String sourceFile = new File(sourceFileNm).getCanonicalPath();

String outputFile = new File(outputFileNm).getCanonicalPath();

ActiveXComponent comp = null;

try {

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

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

Dispatch workbooks = comp.getProperty("Workbooks").toDispatch();

Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[] {sourceFile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();

Dispatch.call(workbook, "SaveAs", new Variant(outputFile), new Variant("1"), new Variant(password));

} finally {

if (comp != null) {

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

}

}

}

}



'Dev > Java' 카테고리의 다른 글

java mail  (0) 2013.06.12
UserException  (0) 2013.06.11
getAbsolutePath, getCanonicalPath  (0) 2013.06.08
Jacob  (0) 2013.06.08
@Autowired 사용시 셋팅  (0) 2013.05.30
Comments