使用 C# 导入 Excel

本文关键字:Excel 导入 使用 | 更新日期: 2023-09-27 18:31:29

我正在使用 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();
    }
}

使用 C# 导入 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();
    }
}

最好不要在服务器情况下使用MS Office互操作,这将在未来带来很多性能和内存问题。建议使用一些第三方库,例如:https://www.nuget.org/packages/Spread.Services/以获得您想要的内容。