C# Excel Interop taking too long process when the row count is more than 3000, when i break the excel down by 1500 rows and process the excel its not taking more than 2 mins.
Note : I am wondering why excel of two 1500 rows is processed quicker but one excel with more than 3000 rows is taking too long ?
sometimes this below exception is thrown
Return argument has an invalid type.
at System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg(Object arg, Type paramType)
at System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParameters(IMessage msg, Object[] outArgs, Object returnValue)
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Range.get_Columns()
at PayableManagementHandler.Common.Utilities.ReadFileDetails(Request requestObj)
Method calling performance issue.
public static DataTable ReadFileDetails(Request requestObj)
{
DataTable fileDetailsTable = null;
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
DataRow row = null;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(requestObj.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
if ((null != range) && (null != range.Cells) &&
(requestObj.Source != PayableHndlrResource.STR_SOURCE ? range.Columns.Count.ToString() == PayableHndlrResource.ColumnsCount :
(requestObj.FileName.Contains(ConfigurationManager.AppSettings["FileName"]) || requestObj.FileName.Contains(ConfigurationManager.AppSettings["FileName"]) ? range.Columns.Count.ToString() == PayableHndlrResource.ColumnsCount : range.Columns.Count.ToString() == PayableHndlrResource.ColumnsCount)))
{
fileDetailsTable = new DataTable("ExcelData");
for (int rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
{
if (range.Cells[rowCount, 1].Value2 != null)
{
if (rowCount > 1)
row = fileDetailsTable.Rows.Add();
for (int col = 1; col <= range.Columns.Count; col++)
{
if (rowCount == 1)
{
fileDetailsTable.Columns.Add((string)(range.Cells[rowCount, col] as Excel.Range).Value2).ToString().Trim();
}
else
{
if (requestObj.Source == PayableHndlrResource.STR_SOURCE)
{
row[col - 1] = ((range.Cells[rowCount, col] as Excel.Range).Value);
}
else
{
string data = Convert.ToString((range.Cells[rowCount, col] as Excel.Range).Value);
if (data != null)
{
switch (fileDetailsTable.Columns[col - 1].Caption.ToString().ToUpper())
{
case "SECONDARY ID":
row[col - 1] = data.Trim().ToString().Truncate(15);
break;
case "ACCOUNT NAME":
row[col - 1] = data.Trim().ToString().Truncate(255);
break;
case "BUSINESS UNIT1":
row[col - 1] = data.Trim().ToString().Truncate(100);
break;
case "BUSINESS UNIT2":
row[col - 1] = data.Trim().ToString().Truncate(100);
break;
case "DESCRIPTION":
row[col - 1] = data.Trim().ToString().Truncate(100);
break;
case "VENDOR":
row[col - 1] = data.Trim().ToString().Truncate(51);
break;
case "CITY":
row[col - 1] = data.Trim().ToString().Truncate(35);
break;
default:
row[col - 1] = data.Trim().ToString();
break;
}
}
else
{
if (fileDetailsTable.Columns[col - 1].Caption.ToString().ToUpper() == "REPORT TOTAL"
|| fileDetailsTable.Columns[col - 1].Caption.ToString().ToUpper() == "TRANSACTION AMOUNT"
|| fileDetailsTable.Columns[col - 1].Caption.ToString().ToUpper() == "ACCOUNT NO"
)
{
row[col - 1] = 0;
}
else if (fileDetailsTable.Columns[col - 1].Caption.ToString().ToUpper() == "TOTAL"
|| fileDetailsTable.Columns[col - 1].Caption.ToString().ToUpper() == " AMOUNT")
{
row[col - 1] = DateTime.MinValue;
}
else
{
row[col - 1] = string.Empty;
}
}
}
}
}
}
}
xlWorkBook.Close(true, requestObj.FileName, Type.Missing);
xlApp.Quit();
ReleaseObject(xlWorkSheet);
ReleaseObject(xlWorkBook);
ReleaseObject(xlApp);
if (requestObj.Source == PayableHndlrResource.STR_SOURCE)
{
SetDefaultValueForCTSI(fileDetailsTable,requestObj.CompanyId);
}
}
}
catch (Exception)
{
throw;
}
return fileDetailsTable;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…