c# - 从 IIS 上载的 Excel 文件中读取有限的行

标签 c# asp.net-mvc excel iis oledb

我有一个托管在 IIS 上的 asp.net mvc 应用程序。我有一个表单,用户可以从中上传包含 50k+ 行的 excel 文件。我使用以下 C# 代码读取了 excel 文件。

    public DataTable GetExcelDataTable(string fileName)
    {
        string connectionString = Path.GetExtension(fileName) == "xls" ?
            string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}; Extended Properties=Excel 8.0;", fileName) :
            string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", fileName);

        var conn = new OleDbConnection(connectionString);
        using (var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", conn))
        {
            var ds = new DataSet();

            adapter.Fill(ds);

            DataTable data = ds.Tables[0];

            conn.Close();
            conn.Dispose();
            adapter.Dispose();
            return data;
        }
    }

问题是它最多只能读取 30k 行,而不会读取整个 excel 文件。

有趣的是,如果我使用 visual studio 运行 mvc 应用程序,我可以读取(使用相同的代码)所有行,但同样,永远不会从 IIS(IIS 也在我的机器上)托管的网站上读取。

有什么想法,为什么会这样?

最佳答案

这种方法不需要在目标机器上安装excel

    NPOI.SS.UserModel.IWorkbook hssfworkbook;

    bool InitializeWorkbook(string path)
    {
        try
        {
            if (path.ToLower().EndsWith(".xlsx"))
            {
                FileStream file1 = File.OpenRead(path);
                hssfworkbook = new XSSFWorkbook(file1);
            }
            else
            {
                //read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
                //book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added. 
                using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new HSSFWorkbook(file);
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }

在以下内容中:

public DataTable GetExcelDataTable(NPOI.SS.UserModel.IWorkbook hssfworkbook, int rowCount)
    {
        NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

        DataTable dt = new DataTable();

        bool skipReadingHeaderRow = rows.MoveNext();
        if (skipReadingHeaderRow)
        {
            dynamic row;
            if (rows.Current is NPOI.HSSF.UserModel.HSSFRow)
                row = (NPOI.HSSF.UserModel.HSSFRow)rows.Current;
            else
                row = (NPOI.XSSF.UserModel.XSSFRow)rows.Current;
            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);
                if (cell != null)
                {
                    dt.Columns.Add(cell.ToString());
                }
                else
                {
                    dt.Columns.Add(string.Empty);
                }
            }
        }
        int cnt = 0;
        while (rows.MoveNext() && cnt < rowCount)
        {
            cnt++;
            dynamic row;
            if (rows.Current is NPOI.HSSF.UserModel.HSSFRow)
                row = (NPOI.HSSF.UserModel.HSSFRow)rows.Current;
            else
                row = (XSSFRow)rows.Current;

            DataRow dr = dt.NewRow();

            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);

                if (cell == null)
                {
                    dr[i - 1] = null;
                }
                else if (i > 0)
                {

                    dr[i - 1] = cell.ToString();
                }
            }
            dt.Rows.Add(dr);
        }

        return dt;
    }

或:

     public DataTable GetExcelDataTable(NPOI.SS.UserModel.IWorkbook hssfworkbook, int rowCount)
    {

        NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
        DataTable dt = new DataTable();
        bool skipReadingHeaderRow = rows.MoveNext();
        if (skipReadingHeaderRow)
        {
            dynamic row;
            if (rows.Current is NPOI.HSSF.UserModel.HSSFRow)
                row = (NPOI.HSSF.UserModel.HSSFRow)rows.Current;
            else
                row = (NPOI.XSSF.UserModel.XSSFRow)rows.Current;
            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);
                if (cell != null)
                {
                    dt.Columns.Add(cell.ToString());
                }
                else
                {
                    dt.Columns.Add(string.Empty);
                }
            }
        }
        int cnt = 0;
        while (rows.MoveNext() && cnt < rowCount)
        {
            cnt++;
            dynamic row;
            if (rows.Current is NPOI.HSSF.UserModel.HSSFRow)
                row = (HSSFRow)rows.Current;
            else
                row = (XSSFRow)rows.Current;
            DataRow dr = dt.NewRow();

            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);

                if (cell == null && i > 0)
                {
                    dr[i - 1] = null;
                }
                else if (i > 0)
                {
                    switch (cell.CellType)
                    {
                        case CellType.Blank:
                            dr[i - 1] = "[null]";
                            break;
                        case CellType.Boolean:
                            dr[i - 1] = cell.BooleanCellValue;
                            break;
                        case CellType.Numeric:
                            dr[i - 1] = cell.ToString();
                            break;
                        case CellType.String:
                            dr[i - 1] = cell.StringCellValue;
                            break;
                        case CellType.Error:
                            dr[i - 1] = cell.ErrorCellValue;
                            break;
                        case CellType.Formula:
                        default:
                            dr[i - 1] = "=" + cell.CellFormula;
                            break;
                    }

                }
            }
            dt.Rows.Add(dr);
        }

        return dt;
    }

或:

public DataTable GetExcelDataTable(NPOI.SS.UserModel.IWorkbook hssfworkbook, int segment, int rowCount)
    {
        NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

        DataTable dt = new DataTable();

        bool skipReadingHeaderRow = rows.MoveNext();
        if (skipReadingHeaderRow)
        {
            dynamic row;
            if (rows.Current is NPOI.HSSF.UserModel.HSSFRow)
                row = (NPOI.HSSF.UserModel.HSSFRow)rows.Current;
            else
                row = (NPOI.XSSF.UserModel.XSSFRow)rows.Current;
            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);
                if (cell != null)
                {
                    dt.Columns.Add(cell.ToString());
                }
                else
                {
                    dt.Columns.Add(string.Empty);
                }
            }
        }
        for (int i = 0; i < (segment - 1)*rowCount; i++)
        {
            if (!rows.MoveNext()) break;
        }

        int cnt = 0;
        while (rows.MoveNext() && cnt < rowCount)
        {
            cnt++;
            dynamic row;
            if (rows.Current is NPOI.HSSF.UserModel.HSSFRow)
                row = (NPOI.HSSF.UserModel.HSSFRow) rows.Current;
            else
                row = (NPOI.XSSF.UserModel.XSSFRow) rows.Current;

            DataRow dr = dt.NewRow();

            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);

                if (cell == null)
                {
                    dr[i - 1] = null;
                }
                else if (i > 0)
                {
                    switch (cell.CellType)
                    {
                        case CellType.Blank:
                            dr[i - 1] = "[null]";
                            break;
                        case CellType.Boolean:
                            dr[i - 1] = cell.BooleanCellValue;
                            break;
                        case CellType.Numeric:
                            dr[i - 1] = cell.ToString();
                            break;
                        case CellType.String:
                            dr[i - 1] = cell.StringCellValue;
                            break;
                        case CellType.Error:
                            dr[i - 1] = cell.ErrorCellValue;
                            break;
                        case CellType.Formula:
                        default:
                            dr[i - 1] = "=" + cell.CellFormula;
                            break;
                    }
                }
            }
            dt.Rows.Add(dr);
        }
        return dt;
    }

关于c# - 从 IIS 上载的 Excel 文件中读取有限的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651365/

相关文章:

c# - 在多个 lightswitch 屏幕中重复使用通用命令

asp.net-mvc - ASP.NET MVC 表单处理未知数量的输入

javascript - 保留 html.dropdownlist 中选定的值

php - 从Excel导入数据时如何使用phpexcel更改日期格式?

excel - 根据 excel 中 sheet1 中的行数在 sheet2 中创建列

regex - Visual Basic Excel 正则表达式 {}

c# - 如何在 C# 中创建一个 onedrive 文件下载器

c# - 什么是 System.Reactive.Linq.Observαble? (注意阿尔法)

c# - 通知字典上的属性更改

C# Web服务,如何接收JSON