早上好,
我对此进行了无休止的研究,并尝试了多种解决方案,但均未成功。这种异常可能发生在测试中保存的 4 次尝试中的 1 次。单击保存按钮后,SaveDialog 将开始出现,冻结一秒钟并关闭应用程序。我试过 [StaThread],但没有帮助。任何建议,将不胜感激。提前致谢。我的代码在下面..
private void button4_Click(object sender, EventArgs e)
{
try
{
using (SaveFileDialog SFD = new SaveFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
{
SFD.FileName = textBox11.Text.ToString();
if (SFD.ShowDialog() == DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = app.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)app.ActiveSheet;
ws.Name = textBox11.Text.ToString();
app.Visible = false;
int i = 2;
foreach (ListViewItem item in lstLocal.Items)
wb.SaveAs(SFD.FileName, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
wb.Close();
app.Quit();
wb = null;
app = null;
MessageBox.Show("File has been saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch
{
MessageBox.Show("Something went wrong!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
慕的地8271018
相关分类