我正在开发一个创建 excel 文件的操作,然后打开一个 SaveFileDialog 允许客户将其保存在他们的计算机中。
问题是在本地,SaveFileDialog 工作但出现在我的浏览器窗口后面,这是一个问题......
然后,当我在我的服务器上发布它时,SaveFileDialog 完全不起作用。大约 2 天后,我在 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;
}