Excelファイルを作成し、SaveFileDialogを開いてクライアントがコンピューターに保存できるようにするアクションを開発しています。
問題は、ローカルでは、SaveFileDialog は機能しますが、ブラウザー ウィンドウの背後に表示されることです。これは問題です...
そして、それをサーバーに公開すると、SaveFileDialog はまったく機能しません。ここStackoverflowでそれに関する他のトピックを読んでいますが、まだ正しい答えが見つかりません...
(私の英語の間違いについて申し訳ありません、私はフランス人です)。これが私のコードです:
String path = string.Empty;
object misValue = System.Reflection.Missing.Value;
bool canExport = false;
Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Add(1);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
Excel.Range rng = (Excel.Range)ws.get_Range("A1", "K1");
/** Excel File completion **/
/** ... **/
/** ... **/
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = Environment.SpecialFolder.Personal.ToString();
sfd.Filter = "Classeur Excel 2010 (*.xls)|*.xls";
if (STAShowDialog(sfd) == DialogResult.OK)
{
path = sfd.FileName;
if (System.IO.File.Exists(path))
{
try
{
System.IO.File.Delete(path);
canExport = true;
}
catch
{
MessageBox.Show("Impossible d'écrire par-dessus ce fichier.");
canExport = false;
}
}
else
{
canExport = true;
}
}
SaveExcelFile(canExport, wb, path, app, ws, misValue);
return View();
そして今私の機能:
public void SaveExcelFile(bool canExport, Excel.Workbook wb, String path, Excel.Application app, Excel.Worksheet ws, object misValue)
{
if (canExport)
{
try
{
wb.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
wb.Close(true, misValue, misValue);
app.Quit();
}
catch
{
MessageBox.Show("Problème durant l'exportation\r\n Code erreur #EX01");
}
}
releaseObject(ws);
releaseObject(wb);
releaseObject(app);
}
private DialogResult STAShowDialog(FileDialog dialog)
{
DialogState state = new DialogState();
state.dialog = dialog;
System.Threading.Thread t = new System.Threading.Thread(state.ThreadProcShowDialog) { IsBackground = true, Name = "threadExport", Priority = System.Threading.ThreadPriority.AboveNormal };
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
t.Join();
return state.result;
}