【问题标题】:Closed XML Reading in a table Through Empty Rows通过空行在表中关闭 XML 读取
【发布时间】:2018-06-08 17:26:46
【问题描述】:

我有一个从电子表格中读取数据来为我的应用程序创建拍卖的方法。我在下面遇到了异常,我想知道为什么我的工作表的范围设置为那个范围,然后如何更改范围以便我可以读取我的 excel 文件的其余部分。

public ActionResult Upload(UploadFile UploadFile)
    {
        if (ModelState.IsValid)
        {

            if (UploadFile.ExcelFile.ContentLength > 0)
            {
                if (UploadFile.ExcelFile.FileName.EndsWith(".xlsx") || UploadFile.ExcelFile.FileName.EndsWith(".xls"))
                {
                    XLWorkbook wb;
                    //incase if the file is corrupt
                    try
                    {
                        wb = new XLWorkbook(UploadFile.ExcelFile.InputStream);
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(String.Empty, $"Check your file. {ex.Message}");
                        return View();
                    }
                    IXLWorksheet ws = null;
                    try//incase if the sheet you are looking for is not found
                    {
                        ws = wb.Worksheet("sheet1");

                    }
                    catch
                    {
                        ModelState.AddModelError(String.Empty, "Sheet not found");
                        return View();
                    }
                    var firstRowUsed = ws.FirstRowUsed();
                    var auctionRow = firstRowUsed.RowUsed().RowBelow();

                    //create auction
                    string auctionName = auctionRow.Cell(1).Value.ToString();
                    DateTimeOffset startDate = DateTimeOffset.Parse(auctionRow.Cell(2).Value.ToString());
                    DateTimeOffset endDate = DateTimeOffset.Parse(auctionRow.Cell(3).Value.ToString());
                    string folderName = auctionRow.Cell(4).Value.ToString();

                    Models.Auction auction = new Models.Auction(auctionName, startDate, endDate, folderName);
                    db.Auctions.Add(auction);


                    //find the next table
                    var nextRow = auctionRow.RowBelow();
                    while (nextRow.IsEmpty())
                    {
                        nextRow = nextRow.RowBelow();
                    }

                    const int catNameCol = 1;
                    var firstCatRow = nextRow.RowUsed();
                    var catRow = firstCatRow.RowBelow();

                    //get categories from ws table and add to the auction
                    while (!catRow.Cell(catNameCol).IsEmpty())
                    {
                        string catName = catRow.Cell(1).Value.ToString();
                        int seqNo = Convert.ToInt32(catRow.Cell(2).Value.ToString());
                        string fileName = catRow.Cell(3).Value.ToString();

                        Cat cat = new Cat(auction.AuctionId, catName, seqNo, fileName);
                        auction.Cats.Add(cat);

                        catRow = catRow.RowBelow();
                    }

                    var findNextRow = catRow.RowBelow();
                    while (findNextRow.IsEmpty())
                    {
                        findNextRow = findNextRow.RowBelow();
                    }
                    const int itemNameCol = 1;

                    var itemRow = findNextRow.RowUsed().RowBelow();
                    while (!itemRow.Cell(itemNameCol).IsEmpty())
                    {
                        string itemName = itemRow.Cell(1).Value.ToString();
                        string itemDesc = itemRow.Cell(2).Value.ToString();
                        string catName = itemRow.Cell(3).Value.ToString();
                        string modelNo = itemRow.Cell(4).Value.ToString();
                        decimal retailValue = Convert.ToDecimal(itemRow.Cell(5).Value.ToString());
                        string fileName = itemRow.Cell(6).Value.ToString();
                        decimal initialBid = Convert.ToDecimal(itemRow.Cell(7).Value.ToString());
                        decimal increment = Convert.ToDecimal(itemRow.Cell(8).Value.ToString());
                        Cat itemCat = null;

                        foreach (var cat in auction.Cats)
                        {
                            if (catName == cat.CatName.Trim())
                            {
                                itemCat = cat;
                            }
                        }

                        Item item = new Item(itemName, itemDesc, modelNo, retailValue, fileName, startDate, endDate, initialBid, increment, null, null, null, itemCat);
                        itemCat.Items.Add(item);

                        itemRow = itemRow.RowBelow();
                    }
                }
                else
                {
                    ModelState.AddModelError(String.Empty, "Only .xlsx and .xls files are allowed");
                    return View();
                }
            }
            else
            {
                ModelState.AddModelError(String.Empty, "Not a valid file");
                return View();
            }
        }
        db.SaveChanges();
        return View();
    }

例外: Excel 文档格式: 我对此真的很陌生,所以让我知道是否有一些语法可以完成我想要做的事情并修复这个异常。如果有什么突出的地方我可以改变/改进,也请告诉我。谢谢你

【问题讨论】:

  • 看起来像 ClosedXML 中的错误。请使用最少的代码示例在 Github 页面上报告。

标签: c# asp.net exception closedxml


【解决方案1】:

nextRow 是截断行“C15:F15”,您要查找的数据大于此范围。我认为这里还有一个 ClosedXML 错误。 在您的情况下,获取工作表的整行然后获取使用过的单元格会有所帮助。 尝试下一个:

var firstCatRow = nextRow.WorksheetRow().RowUsed();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-12
    • 2012-01-25
    • 2023-03-09
    • 2015-11-03
    • 2020-07-02
    • 2016-03-10
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多