Asp.net mvc 3 如何打开生成的excel

Asp.net mvc 3 如何打开生成的excel,asp.net-mvc-3,export-to-excel,Asp.net Mvc 3,Export To Excel,我正在将sql server数据库中的数据导出到wpf中的excel中,并成功地实现了该功能。现在我想在保存到某个位置后自动打开生成的excel。有什么想法吗?提前谢谢。我想要的功能如截图所示: private void button1_Click(object sender, RoutedEventArgs e) { string sql = null; string data = null; // string path = nu

我正在将sql server数据库中的数据导出到wpf中的excel中,并成功地实现了该功能。现在我想在保存到某个位置后自动打开生成的excel。有什么想法吗?提前谢谢。我想要的功能如截图所示:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        string sql = null;
        string data = null;
       // string path = null;
        //string myfilename = "Report";
        int i = 0;
        int j = 0;

        Microsoft.Office.Interop.Excel.Application xlApp;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        //xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

        xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkSheet.Name = "Customer List";

        //connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;";
        //SqlConnection cnn = new SqlConnection(GetConnectionString());

        SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=ClientLists;Integrated Security=SSPI;";
        cnn.Open();
        sql = "select FirstName, LastName, City, PostCode, TelephoneNo from Customers";
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
        DataSet ds = new DataSet();
        dscmd.Fill(ds);


        for (i = 0; i <= ds.Tables[0].Rows.Count -1; i++)
        {
            for (j = 0; j <= ds.Tables[0].Columns.Count -1; j++)
            {
                data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                xlWorkSheet.Cells[i + 2, j + 1] = data;
            }
        }


         xlWorkSheet.Cells.ColumnWidth = 16;
         xlWorkSheet.Cells.Font.Size = 12; 
         xlWorkSheet.Cells[1, 1].Font.Bold = true;
         xlWorkSheet.Cells[1, 2].Font.Bold = true;
         xlWorkSheet.Cells[1, 3].Font.Bold = true;
         xlWorkSheet.Cells[1, 4].Font.Bold = true;
         xlWorkSheet.Cells[1, 5].Font.Bold = true;
         xlWorkSheet.Cells[1, 1].Font.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbBlue;
         xlWorkSheet.Cells[1, 2].Font.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbBlue;
         xlWorkSheet.Cells[1, 3].Font.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbBlue;
         xlWorkSheet.Cells[1, 4].Font.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbBlue;
         xlWorkSheet.Cells[1, 5].Font.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbBlue;
         xlWorkSheet.Cells[1, 1] = "First Name";
         xlWorkSheet.Cells[1, 2] = "Last Name";
         xlWorkSheet.Cells[1, 3] = "City";
         xlWorkSheet.Cells[1, 4] = "Post Code";
         xlWorkSheet.Cells[1, 5] = "Telephone NO";          

        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();


        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

    }
}
private void按钮1\u单击(对象发送者,路由目标)
{
字符串sql=null;
字符串数据=null;
//字符串路径=null;
//字符串myfilename=“报告”;
int i=0;
int j=0;
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.工作簿;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
对象错误值=System.Reflection.Missing.Value;
//xlApp=新的Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp=新的Microsoft.Office.Interop.Excel.Application();
xlWorkBook=xlApp.Workbooks.Add(错误值);
xlWorkSheet=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlWorkSheet.Name=“客户列表”;
//connectionString=“数据源=服务器名;初始目录=数据库名;用户id=用户名;密码=密码;”;
//SqlConnection cnn=新的SqlConnection(GetConnectionString());
SqlConnection cnn=新的SqlConnection();
cnn.ConnectionString=@“数据源=。\sqlexpress;初始目录=客户端列表;集成安全性=SSPI;”;
cnn.Open();
sql=“从客户处选择名字、姓氏、城市、邮政编码、电话号码”;
SqlDataAdapter dscmd=新的SqlDataAdapter(sql,cnn);
数据集ds=新数据集();
dscmd.Fill(ds);

对于(i=0;i我通过添加以下代码实现了SaveAs函数:System.Windows.Forms.SaveFileDialog oDialog=new System.Windows.Forms.SaveFileDialog();oDialog.Filter=“Excel files |*.xls”;如果(oDialog.ShowDialog()==System.Windows.Forms.DialogResult.OK){sFileName=oDialog.FileName;}