Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用mvc通过浏览器从数据库导出excel文件_Sql Server_Asp.net Mvc_Export To Excel - Fatal编程技术网

Sql server 使用mvc通过浏览器从数据库导出excel文件

Sql server 使用mvc通过浏览器从数据库导出excel文件,sql-server,asp.net-mvc,export-to-excel,Sql Server,Asp.net Mvc,Export To Excel,我想使用mvc“通过浏览器”将sql表格数据导出到客户端pc上的excel文件。我使用的是: public class ManagementController : Controller { public void ExportitemToExcel() { try { dt = SqlHelper.LoadTable("SM_GetAllItems", sql);

我想使用mvc“通过浏览器”将sql表格数据导出到客户端pc上的excel文件。我使用的是:

public class ManagementController : Controller
{
  public void ExportitemToExcel()
    {        
        try
        {              
            dt = SqlHelper.LoadTable("SM_GetAllItems", sql);
            if (dt != null && dt.Rows.Count > 0)
            {                   
                ExportToExcel(dt);
            }
        }
        catch
        { throw; }       
    }
以下是导出到excel的方法:

 private void ExportToExcel(DataTable dt)
    {
        Microsoft.Office.Interop.Excel.Application excel;
        Microsoft.Office.Interop.Excel.Workbook worKbooK;
        Microsoft.Office.Interop.Excel.Worksheet worKsheeT;
        excel = new Microsoft.Office.Interop.Excel.Application();
        excel.Visible = false;
        excel.DisplayAlerts = false;
        worKbooK = excel.Workbooks.Add(Type.Missing);

        worKsheeT = (Microsoft.Office.Interop.Excel.Worksheet)worKbooK.ActiveSheet;
        worKsheeT.Name = "Inventory";

        //Create an Excel workbook instance and open it from the predefined location
        // Excel.Workbook excelWorkBook = excel.Workbooks.Open(@"E:\Org.xlsx");
        // Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
        //excelWorkSheet.Name = dt.TableName;

        for (int i = 1; i < dt.Columns.Count + 1; i++)
        {
            worKsheeT.Cells[1, i] = dt.Columns[i - 1].ColumnName;
        }

        for (int j = 0; j < dt.Rows.Count; j++)
        {
            for (int k = 0; k < dt.Columns.Count; k++)
            {
                worKsheeT.Cells[j + 2, k + 1] = dt.Rows[j].ItemArray[k].ToString();
            }

        }
        string fastExportFilePath = "C:/" + "Inventory" + ".xlsx";

        if (System.IO.File.Exists(fastExportFilePath))
        {
            System.IO.File.Delete(fastExportFilePath);
        }

        worKbooK.SaveAs(fastExportFilePath);
        worKbooK.Close();
        excel.Quit();
private void ExportToExcel(数据表dt)
{
Microsoft.Office.Interop.Excel.Application Excel;
Microsoft.Office.Interop.Excel.Workbook工作簿;
Microsoft.Office.Interop.Excel.Worksheet工作表;
excel=新的Microsoft.Office.Interop.excel.Application();
excel.Visible=false;
excel.DisplayAlerts=false;
工作簿=excel.Workbooks.Add(Type.Missing);
工作表=(Microsoft.Office.Interop.Excel.worKsheeT)worKbooK.ActiveSheet;
工作表.Name=“库存”;
//创建Excel工作簿实例并从预定义位置打开它
//Excel.工作簿Excel工作簿=Excel.Workbooks.Open(@“E:\Org.xlsx”);
//Excel.Worksheet excelWorkSheet=excelWorkBook.Sheets.Add();
//excelWorkSheet.Name=dt.TableName;
对于(int i=1;i

这将以静默方式创建文件,即不通过浏览器。

您不能将excel直接保存到客户端的pc。您可以做的是将excel保存到服务器并与客户端共享以供下载

您可以修改ExportToExcel方法以返回生成的文件名,用于打开excel文件

private string ExportToExcel(DataTable dt)
{
  //your code goes here
  return fastExportFilePath
}
Javascript

 $(document).ready(function () {
//Check if the hidden field has a value. (Excel was generated)
if ($('#hdnFileName').val() != '')
{
 var url = "Full path to your file" + $('#hdnFileName').val();

                window.location.assign(url)
}
});

您不能将excel直接保存到客户端的pc。您可以做的是将excel保存到服务器并与客户端共享以下载

您可以修改ExportToExcel方法以返回生成的文件名,用于打开excel文件

private string ExportToExcel(DataTable dt)
{
  //your code goes here
  return fastExportFilePath
}
Javascript

 $(document).ready(function () {
//Check if the hidden field has a value. (Excel was generated)
if ($('#hdnFileName').val() != '')
{
 var url = "Full path to your file" + $('#hdnFileName').val();

                window.location.assign(url)
}
});