excel open protected password in jacob
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[]{});
}
}
}
}