1

我有这段代码用于从 Excel 工作簿中读取工作表,然后填充列表。

我得到一个范围,将其放入二维数组中,然后使用 for 循环遍历它并填充列表。

我的问题是,有没有更好的方法来达到相同的结果?我的意思是,代码更少,速度更快。我必须为 4 张纸执行此操作,并且需要 260 毫秒。

public static void LoadDdrDataIntoObjects(ref List<Receivables> recList, ref List<Dilution> dilList, ref List<Accountable> accList, ref List<Outstanding> outList, string sheet)
{
    Workbook pantaReiWorkBook = PantaReiApplication.ActiveWorkbook;
    Sheets pantaReiWorkSheets = pantaReiWorkBook.Sheets;

    Worksheet pantaReiWorkSheet = pantaReiWorkSheets[sheet];
    Range pantaReiLastCell =
        pantaReiWorkSheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell);

    Range pantaReiRange =
        pantaReiWorkSheet.Range["A1:" + pantaReiLastCell.Address.Trim(new char['$']), Type.Missing];

    object[,] pantaReidataRange = (object[,]) pantaReiRange.Value[Type.Missing];

    for (var row = 2; row <= pantaReidataRange.GetLength(0); row++)
    {
        Receivables pantaReiReceivable = new Receivables
        {
            FileID = fileID.ID,
            SheetType =
                pantaReidataRange[row, 1].ToString(),
            SellerCompany =
                pantaReidataRange[row, 2].ToString(),
            Contract = pantaReidataRange[row, 3].ToString(),
            DebitID = pantaReidataRange[row, 4].ToString(),
            CompanyCode =
                pantaReidataRange[row, 5].ToString(),
            NoteNumber =
                pantaReidataRange[row, 6].ToString(),
            Installment =
                Convert.ToInt32(pantaReidataRange[row, 7]),
            InvoiceDate =
                (DateTime) pantaReidataRange[row, 8],
            DueDate1 = (DateTime) pantaReidataRange[row, 9],
            DueDate2 =
                (DateTime) pantaReidataRange[row, 10],
            DueDate3 =
                (DateTime) pantaReidataRange[row, 11],
            Currency =
                pantaReidataRange[row, 12].ToString(),
            Amount =
                (float)
                Convert.ToDouble(pantaReidataRange[row, 13]),
            Sign = pantaReidataRange[row, 14].ToString(),
            ProductType =
                pantaReidataRange[row, 15].ToString(),
            DilutionType =
                Convert.ToString(pantaReidataRange[row, 16]),
            MaturityStatus =
                Convert.ToInt32(pantaReidataRange[row, 17]),
            DebitStatus =
                pantaReidataRange[row, 18].ToString(),
            DealerFlag =
                pantaReidataRange[row, 19].ToString(),
            CustomerDescription =
                pantaReidataRange[row, 20].ToString(),
            AssetType =
                Convert.ToInt32(pantaReidataRange[row, 21])
        };

        recList.Add(pantaReiReceivable);
    }
}
4

2 回答 2

0

I managed on my own. I used reflection.

于 2012-09-02T09:10:42.417 回答
0

最好的办法是分析您的代码。JetBrains dotTrace是我经常使用的一个工具。

我的一个建议是,如果您要处理大量行,请考虑预先设置List<T>容量,甚至将单个记录对象的添加推迟List到您完成处理之前 - 将它们放入循环期间的数组。你可以在进入for循环之前声明数组;根据 for 循环的迭代次数,您已经知道数组的大小。完成处理后,您可以收集所有数组并 LINQ 将它们转换为您喜欢的列表。

于 2012-08-22T17:48:48.757 回答