0

我正在使用 c# 中的 DataTable 将 Excel 导入到网格视图,但我的问题是它在我的本地系统上工作正常,但它重定向到我的在线服务器上的登录页面,我已经尝试过 Microsoft.Office.Interop.Excel 和 OLEdb 连接. 但问题是一样的。请告诉我这些有什么问题,或者任何人有任何其他功能可以在 GridView 中导入 Excel 数据。

private void processExcel(string filename)
{
    filename = Server.MapPath("~/Files/WM-0b23-productsBook.xlsx");
    Microsoft.Office.Interop.Excel.Application xlApp;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

    var missing = System.Reflection.Missing.Value;

    xlApp = new Microsoft.Office.Interop.Excel.Application();
    xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange;
    Array myValues = (Array)xlRange.Cells.Value2;

    int vertical = myValues.GetLength(0);
    int horizontal = myValues.GetLength(1);

    System.Data.DataTable dt = new System.Data.DataTable();

    // must start with index = 1
    // get header information
    for (int i = 1; i <= horizontal; i++)
    {
        dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
    }

    // Get the row information
    for (int a = 2; a <= vertical; a++)
    {
        object[] poop = new object[horizontal];
        for (int b = 1; b <= horizontal; b++)
        {
            poop[b - 1] = myValues.GetValue(a, b);
        }
        DataRow row = dt.NewRow();
        row.ItemArray = poop;
        dt.Rows.Add(row);
    }

    // assign table to default data grid view
    GridView1.DataSource = dt;
    GridView1.DataBind();

    xlWorkBook.Close(true, missing, missing);
    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("Unable to release the Object " + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}
4

2 回答 2

1

看一看:

private void processExcel(string filename)
    {
        filename = Server.MapPath("~/Files/WM-0b23-productsBook.xlsx");
        Microsoft.Office.Interop.Excel.Application xlApp;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    var missing = System.Reflection.Missing.Value;

    xlApp = new Microsoft.Office.Interop.Excel.Application();
    xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange;
    Array myValues = (Array)xlRange.Cells.Value2;

    int vertical = myValues.GetLength(0);
    int horizontal = myValues.GetLength(1);

    System.Data.DataTable dt = new System.Data.DataTable();

    // must start with index = 1
    // get header information
    for (int i = 1; i <= horizontal; i++)
    {
        dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
    }

    // Get the row information
    for (int a = 2; a <= vertical; a++)
    {
        object[] poop = new object[horizontal];
        for (int b = 1; b <= horizontal; b++)
        {
            poop[b - 1] = myValues.GetValue(a, b);
        }
        DataRow row = dt.NewRow();
        row.ItemArray = poop;
        dt.Rows.Add(row);
    }

    // assign table to default data grid view
    GridView1.DataSource = dt;
    GridView1.DataBind();

    xlWorkBook.Close(true, missing, missing);
    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("Unable to release the Object " + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}
于 2013-10-25T03:29:54.130 回答
0

最好不要在服务器案例中使用 MS Office 互操作,这将在未来带来很多性能和内存问题。推荐使用一些 3rd 方库,例如:https ://www.nuget.org/packages/Spread.Services/ 来获得你想要的。

于 2017-09-08T17:01:53.407 回答