Save 呼び出しでファイル形式を指定してみてください。
運が良ければ、Excel ヘルプで必要なファイル形式コードを見つけることができます。必要なものが見つからない場合は、OLEVIEW.EXEプログラムを使用して手を汚さなければなりません。ハード ドライブのどこかにそのコピーが保存されている可能性がありますが、そうでない場合でも、Google 検索で簡単にコピーを見つけることができます。
OLEVIEW.EXEを使用するには:
- それを実行します
- 「Type Libraries」エントリをクラックして開きます
- 使用している Excel のバージョンを確認する
- そのアイテムを開く
- 文字列「XlFileFormat」で表示される大量のテキストを検索します
- 有望と思われるコードの XLFileFormat 列挙を調べる
私のように Office2007 ("Excel12") を使用している場合は、次のいずれかの値を試してください。
- xlOpenXMLWorkbookMacroEnabled = 52
- xlOpenXMLTemplateMacroEnabled = 53
OLE を使用して Excel ファイルを保存するために使用する方法を次に示します。
/**
* Save the given workbook in the specified format.
*
* @param controlSiteAuto the OLE control site to use
* @param filepath the file to save to
* @param formatCode XlFileFormat code representing the file format to save as
* @param replaceExistingWithoutPrompt true to replace an existing file quietly, false to ask the user first
*/
public void saveWorkbook(OleAutomation controlSiteAuto, String filepath, Integer formatCode, boolean replaceExistingWithoutPrompt) {
Variant[] args = null;
Variant result = null;
try {
// suppress "replace existing?" prompt, if necessary
if (replaceExistingWithoutPrompt) {
setPropertyOnObject(controlSiteAuto, "Application", "DisplayAlerts", "False");
}
// if the given formatCode is null, for some reason, use a reasonable default
if (formatCode == null) {
formatCode = 51; // xlWorkbookDefault=51
}
// save the workbook
int[] id = controlSiteAuto.getIDsOfNames(new String[] {"SaveAs", "FileName", "FileFormat"});
args = new Variant[2];
args[0] = new Variant(filepath);
args[1] = new Variant(formatCode);
result = controlSiteAuto.invoke(id[0], args);
if (result == null || !result.getBoolean()) {
throw new RuntimeException("Unable to save active workbook");
}
// enable alerts again, if necessary
if (replaceExistingWithoutPrompt) {
setPropertyOnObject(controlSiteAuto, "Application", "DisplayAlerts", "True");
}
} finally {
cleanup(args);
cleanup(result);
}
}
protected void cleanup(Variant[] variants) {
if (variants != null) {
for (int i = 0; i < variants.length; i++) {
if (variants[i] != null) {
variants[i].dispose();
}
}
}
}