我有这段代码用于从 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);
}
}