本文介绍了使用SaveWorkSpace保存Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的应用程序中,我将值从Grid导出到Excel并使用SaveWorkSpace()方法保存excel文件。 我成功保存文件时设置了一个消息框。 但如果我按下取消,即使这样它也会显示警告信息。 我不想在取消时显示消息。 以下是我的代码。 < blockquote class = FQ>< div class = FQA> Quote:< / div > protected void btnExort_Click( object sender,EventAr gs e) { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = false ; Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add( 1 ); Microsoft.Office.Interop.Excel.Worksheet worksheet =(Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets [ 1 ]; Microsoft.Office.Interop.Excel.Workbook objSheet = null ; // Microsoft.Office.Interop.Excel.Workbook book = Microsoft.Office.Interop.Excel.Workbooks .Add()as Excel.Workbook; Microsoft.Office.Interop.Excel._Workbook objBook = null ; for ( int gridRow = 0 ; gridRow < gridView.Rows.Count; gridRow ++) { GridViewRow row = gridView.Rows [gridRow]; string name =((Label)row.FindControl( Label1的))的ToString(); worksheet.Cells [gridRow + 3 , 3 ] =((标签)row.FindControl ( Label1))。文字; worksheet.Cells [gridRow + 3 , 2 ] =((标签)row.FindControl ( Label2))。文字; worksheet.Cells [gridRow + 3 , 4 ] =((标签)row.FindControl ( Label3))。文字; worksheet.Cells [gridRow + 3 , 5 ] =((标签)row.FindControl ( Label4))。文字; worksheet.Cells [gridRow + 3 , 6 ] =((标签)row.FindControl ( Label7))。文字; worksheet.Cells [gridRow + 3 , 7 ] =((标签)row.FindControl ( Label8))。文字; } worksheet.Cells [ 2 , 2 ] = 客户名称; worksheet.Cells [ 2 , 3 ] = 客户ID; worksheet.Cells [ 2 , 4 ] = 客户显示名称; worksheet.Cells [ 2 , 5 ] = 客户电子邮件; worksheet.Cells [ 2 , 6 ] = 服务ID; worksheet.Cells [ 2 , 7 ] = 服务名称; DateTime currentTime; currentTime = DateTime.Now; try { app.SaveWorkspace(); app.Workbooks.Close(); } catch (Exception ex){} ClientScript.RegisterStartupScript( this .GetType(), fnCall, < script language ='javascript'> alert('Saved Successfully');< / script>); } 请帮忙。解决方案 在你的代码中添加这个, string filePath = string.Empty; ... 。 .... .... 试试 { app.SaveWorkspace(); filePath = workbook.Path; app.Workbooks.Close(); } catch(exception ex){} check(); if(filePath ==) { ClientScript.RegisterStartupScript(this.GetType(),fnCall,< script language = 'javascript'>提醒('保存已取消');< / script>); } 其他 { ClientScript.RegisterStartupScript(this.GetType(),fnCall,< s cript language ='javascript'> alert('Saved Successfully');< / script>); } Hi,In my application I am exporting values from Grid to Excel and saving the excel file using SaveWorkSpace() method.I have set a message box when the file is saved successfully.But if I press cancel, even then it's showing the alert message.I don't want to show the message when it's cancelled.Below is my code.<blockquote class="FQ"><div class="FQA">Quote:</div>protected void btnExort_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = false; Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add(1); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; Microsoft.Office.Interop.Excel.Workbook objSheet = null; // Microsoft.Office.Interop.Excel.Workbook book = Microsoft.Office.Interop.Excel.Workbooks.Add("") as Excel.Workbook; Microsoft.Office.Interop.Excel._Workbook objBook = null; for (int gridRow = 0; gridRow < gridView.Rows.Count; gridRow++) { GridViewRow row = gridView.Rows[gridRow]; string name = ((Label)row.FindControl("Label1")).ToString(); worksheet.Cells[gridRow + 3, 3] = ((Label)row.FindControl("Label1")).Text; worksheet.Cells[gridRow + 3, 2] = ((Label)row.FindControl("Label2")).Text; worksheet.Cells[gridRow + 3, 4] = ((Label)row.FindControl("Label3")).Text; worksheet.Cells[gridRow + 3, 5] = ((Label)row.FindControl("Label4")).Text; worksheet.Cells[gridRow + 3, 6] = ((Label)row.FindControl("Label7")).Text; worksheet.Cells[gridRow + 3, 7] = ((Label)row.FindControl("Label8")).Text; } worksheet.Cells[2, 2] = "Customer Name"; worksheet.Cells[2, 3] = "Customer ID"; worksheet.Cells[2, 4] = "Customer Display Name"; worksheet.Cells[2, 5] = "Customer Email"; worksheet.Cells[2, 6] = "Service ID"; worksheet.Cells[2, 7] = "Service Name"; DateTime currentTime; currentTime = DateTime.Now; try { app.SaveWorkspace(); app.Workbooks.Close(); } catch (Exception ex) { } ClientScript.RegisterStartupScript(this.GetType(), "fnCall", "<script language='javascript'>alert('Saved Successfully');</script>"); }Please help. 解决方案 Add this in your code,string filePath=string.Empty;............try { app.SaveWorkspace(); filePath = workbook.Path; app.Workbooks.Close(); } catch (Exception ex) { } check(); if (filePath == "") { ClientScript.RegisterStartupScript(this.GetType(), "fnCall", "<script language='javascript'>alert('Saving Cancelled');</script>"); } else { ClientScript.RegisterStartupScript(this.GetType(), "fnCall", "<script language='javascript'>alert('Saved Successfully');</script>"); } 这篇关于使用SaveWorkSpace保存Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 10:05