ファイルの拡張子に応じて、これらの接続文字列を使用しています。
public static string ExcelConnectionString(string filePath)
{
OleDbConnectionStringBuilder sbConnection = new OleDbConnectionStringBuilder();
String strExtendedProperties = String.Empty;
sbConnection.DataSource = filePath;
if (Path.GetExtension(filePath).Equals(".xls"))//for 97-03 Excel file
{
sbConnection.Provider = "Microsoft.Jet.OLEDB.4.0";
strExtendedProperties = "Excel 8.0";//HDR=ColumnHeader,IMEX=InterMixed
}
else if (Path.GetExtension(filePath).Equals(".xlsx")) //for 2007 Excel file
{
sbConnection.Provider = "Microsoft.ACE.OLEDB.12.0";
strExtendedProperties = "Excel 12.0";
}
sbConnection.Add("Extended Properties", strExtendedProperties);
return sbConnection.ToString();
}
ただし、「名前を付けて保存: 2003 xls」または「名前を付けて保存: xlsx」オプションを使用せずに xls ファイルをインポートすると、. (そのため、ダウンロードしたファイルを保存しcontrol + sても機能しません。) 最終的に次のエラーが発生します。
OleDbException がユーザーコードによって処理されませんでした - 外部テーブルが予期された形式ではありません。
どうすればこれを修正できますか? control + s私のシステムは、2 つのうちの 1 つを手動で選択するのではなく、ファイルを保存するために連携する必要があります。
エクスポート後に手動でファイルを開くと、次のウィンドウが表示されるため、問題は私が作成したエクスポート*である可能性があります。
The file you are trying to open "yourfile.xls" in a different format than specified file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
*エクスポート ファイルは、生成された XML ファイルを .xls として保存したものです。これを警告なしで .xls として保存する方法はありますか? control + sファイルを保存するには本当に必要です。
前もって感謝します。
要するに、上記のエラーを表示せずに、作成した XML ファイルを XLS として保存するにはどうすればよいでしょうか。外部ライブラリなしでこれを行うことが本当に不可能な場合にのみ、外部ライブラリを使用したくありません。