简体   繁体   English

将DataTable数据保存在Excel文件asp.Net中

[英]Save DataTable data in Excel file asp.Net

I have a Gridview which i fill with a Datatable. 我有一个Gridview,我填充数据表。 On the webpage the user has to be able to download an excel file with the contents of the Gridview. 在网页上,用户必须能够下载带有Gridview内容的excel文件。 So mi thought the easiest way to save the files is to save the datatable as an excel file. 所以mi认为保存文件最简单的方法是将数据表保存为excel文件。 I have troied this method, which i found here on stackoverflow. 我已经使用了这个方法,我在stackoverflow上找到了这个方法。

using Excel = Microsoft.Office.Interop.Excel;
 public static bool ExportDataTableToExcel(DataTable dt, string filepath)
    {

    Excel.Application oXL;
    Excel.Workbook oWB;
    Excel.Worksheet oSheet;
    Excel.Range oRange;

    try
    {
        // Start Excel and get Application object. 
        oXL = new Excel.Application();

        // Set some properties 
        oXL.Visible = true;
        oXL.DisplayAlerts = false;

        // Get a new workbook. 
        oWB = oXL.Workbooks.Add(Missing.Value);

        // Get the Active sheet 
        oSheet = (Excel.Worksheet)oWB.ActiveSheet;
        oSheet.Name = "Data";

        int rowCount = 1;
        foreach (DataRow dr in dt.Rows)
        {
            rowCount += 1;
            for (int i = 1; i < dt.Columns.Count + 1; i++)
            {
                // Add the header the first time through 
                if (rowCount == 2)
                {
                    oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                }
                oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
            }
        }

        // Resize the columns 
        oRange = oSheet.get_Range(oSheet.Cells[1, 1],
                      oSheet.Cells[rowCount, dt.Columns.Count]);
        oRange.EntireColumn.AutoFit();

        // Save the sheet and close 
        oSheet = null;
        oRange = null;
        oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Excel.XlSaveAsAccessMode.xlExclusive,
            Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);
        oWB.Close(Missing.Value, Missing.Value, Missing.Value);
        oWB = null;
        oXL.Quit();
    }
    catch
    {
        throw;
    }
    finally
    {
        // Clean up 
        // NOTE: When in release mode, this does the trick 
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }

    return true;
}

Bu i get a build error stating that "The name Missing does not exist in the current context" and i cant seem to figure out why? 我得到一个构建错误,指出“当前上下文中缺少名称缺失 ”,我似乎无法弄清楚为什么? can anyone help ? 有人可以帮忙吗?

the code is pasted from this stackoverflow page here 代码将从此stackoverflow页面粘贴到此处

According to msdn here , Missing.Value is defined in System.Reflection . 根据msdn 这里Missing.ValueSystem.Reflection定义。 You should add a reference to this namespace. 您应该添加对此命名空间的引用。

using System.Reflection;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM