Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Dear All
It is showing error at this line Microsoft.Office.Interop.Excel.Workbook aWorkbookObj = appObj.Workbooks. Open(aPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel. XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); May I know what is wrong here?Please help urgent public string ReadExcelContents(string aPath, out ArrayList ErrorList, out string GlobalErrorMessage) { string aResTin = string.Empty; //Initialize local variables GlobalErrorMessage = "Errors In data in following columns in excel file "; ErrorList = new ArrayList(); string aAddress1 = string.Empty; string aTin = string.Empty; string aVendor = string.Empty; string aDBA = string.Empty; string aAddress2 = string.Empty; string aCity = string.Empty; string aState = string.Empty; string aZip = string.Empty; string aCountry = string.Empty; string aTransactionId = string.Empty; string aTransactionDate = string.Empty; string aAmount = string.Empty; string aDescription = string. Empty; string aPgcb = string.Empty; StringBuilder aStringBuilder = new StringBuilder(); //Create and Initialize the object of excel application Microsoft.Office.Interop.Excel.ApplicationClass appObj = new ApplicationClass(); System.Threading.Thread.Sleep(2000); //Create the workbook by calling open method of excel Microsoft.Office.Interop.Excel.Workbook aWorkbookObj = appObj. Workbooks.Open(aPath, 0, true, 5, "", "", true, Microsoft.Office.Interop. Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); Microsoft.Office.Interop.Excel.Workbook aWorkbookObj = appObj. Workbooks.Open(aPath, 0, true, 5, "", "", true, Microsoft.Office.Interop. Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); //obtain the active worksheet of excel application Microsoft.Office.Interop.Excel.Worksheet aWorksheetObj = (Microsoft.Office.Interop.Excel.Worksheet)aWorkboo kObj.ActiveSheet; //Get the column count in the excel sheet //aWorksheetObj.Columns.Count; int aIndex = 0; object rowIndex = 2; object colIndex1 = 1; object colIndex2 = 2; object colIndex3 = 3; object colIndex4 = 4; object colIndex5 = 5; object colIndex6 = 6; object colIndex7 = 7; object colIndex8 = 8; object colIndex9 = 9; object colIndex10 = 10; object colIndex11 = 11; object colIndex12 = 12; object colIndex13 = 13; object colIndex14 = 14; object aColIndex = 0; int aRowCount = aWorksheetObj.UsedRange.Rows.Count; try { while (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex1]).Value2 != null) { rowIndex = 2 + aIndex; if (Convert.ToInt32(rowIndex) aRowCount) { break; } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex1]).Value2 == null) { } else { aTin = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex1]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex2]).Value2 == null) { } else { aVendor = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex2]).Value2.ToString(); } if(((Microsoft.Office.Interop.Excel.Range)aWorkshe etObj. Cells[rowIndex, colIndex3]).Value2 == null) { } else { aDBA = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex3]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex4]).Value2 == null) { } else { aAddress1 = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex4]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex5]).Value2 == null) { } else { aAddress2 = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex5]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex6]).Value2 == null) { } else { aCity = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex6]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex7]).Value2 == null) { } else { aState = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex7]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex8]).Value2 == null) { } else { aZip = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex8]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex9]).Value2 == null) { } else { aCountry = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex9]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex10]).Value2 == null) { } else { aTransactionId = ((Microsoft.Office.Interop.Excel. Range)aWorksheetObj.Cells[rowIndex, colIndex10]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex11]).Value2 == null) { } else { aTransactionDate = ((Microsoft.Office.Interop.Excel. Range)aWorksheetObj.Cells[rowIndex, colIndex11]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex12]).Value2 == null) { } else { aAmount = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex12]).Value2.ToString(); } double aNewAmt = Convert.ToDouble(aAmount); //string aNewAmount = string.Format("{0:C}", aNewAmt); if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex13]).Value2 == null) { } else { aDescription = ((Microsoft.Office.Interop.Excel. Range)aWorksheetObj.Cells[rowIndex, colIndex13]).Value2.ToString(); } if (((Microsoft.Office.Interop.Excel.Range)aWorksheet Obj. Cells[rowIndex, colIndex14]).Value2 == null) { } else { aPgcb = ((Microsoft.Office.Interop.Excel.Range) aWorksheetObj.Cells[rowIndex, colIndex14]).Value2.ToString(); } Regex aRegTin = new Regex(@"^\d{2}-\d{7}$"); bool aTinFlag = aRegTin.IsMatch(aTin); Regex aRegexObj = new Regex(@"^\d{3}-?\d{2}-?\d{4}$"); bool aSSNFlag = aRegexObj.IsMatch(aTin); if((aTinFlag == true) || (aSSNFlag == true)) { aResTin = RemoveSymbol(aTin); aStringBuilder.Append(aResTin); } else { GlobalErrorMessage = GlobalErrorMessage + "Tin" + ", "; ErrorList.Add(aTin); } Regex aRegstate = new Regex(@"^[a-zA-Z]{2}$"); bool aStateFlag = aRegstate.IsMatch(aState); if (aStateFlag == false) { GlobalErrorMessage = GlobalErrorMessage + "State" + ","; ErrorList.Add(aState); } else { aStringBuilder.Append(aState); } Regex aRegCountry = new Regex(@"^[a-zA-Z]{2}$"); bool aCountryFlag = aRegCountry.IsMatch(aCountry); if (aCountryFlag == false) { GlobalErrorMessage = GlobalErrorMessage + "Country" + ","; ErrorList.Add(aState); } else { aStringBuilder.Append(aCountry); } //Regex aRegTransactionDate = new Regex(@"^\d{1}[1-9]\/\d{1}[1-9]\/\d{4}$"); Regex aRegTransactionDate = new Regex(@"(([1-9]|[1-2][0-9]|3[0-1]|0[1-9])[/ /.]([1-9]|1[0-2]|0[1-9])[/ /. ]([1-9][0-9])\d\d)"); bool aTransdateFlag = aRegTransactionDate. IsMatch(aTransactionDate); if (aTransdateFlag == false) { GlobalErrorMessage = GlobalErrorMessage + " " + "TransactionDate" + ","; ErrorList.Add(aTransactionDate); } else { aStringBuilder.Append(aTransactionDate); } bool aAmountFlag = CheckDollarandCommainAmount(aAmount) ; if (aAmountFlag == true) { GlobalErrorMessage = GlobalErrorMessage + "Amount" + ","; ErrorList.Add(aAmount); } else { //char[] ResultantArr = RemoveDollarandcomma(aAmount) ; //string aNewAmt = new string(ResultantArr); //double aNewAmount = Convert.ToDouble(aAmount); string aNewValue = aNewAmt.ToString("#.##"); aStringBuilder.Append(aNewValue); } aStringBuilder.Append(aVendor); aStringBuilder.Append(aDBA); aStringBuilder.Append(aAddress1); aStringBuilder.Append(aAddress2); aStringBuilder.Append(aCity); aStringBuilder.Append(aZip); aStringBuilder.Append(aTransactionId); aStringBuilder.Append(aDescription); aStringBuilder.Append(aPgcb); aIndex++; } aWorksheetObj = null; aWorkbookObj.Close(true, null, null); appObj.Quit(); } catch (Exception ex) { appObj.Quit(); Console.WriteLine(ex.Message); } finally { System.Runtime.InteropServices.Marshal. ReleaseComObject(appObj); } return aStringBuilder.ToString(); } url:http://www.ureader.com/gp/1037-1.aspx |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Exception from HRESULT: 0x800A03EC - COM Exception Unhandled | Excel Programming | |||
Exception from HRESULT: 0x800A03EC at Excel.Workbooks.Open | Excel Programming | |||
Error: Exception from HRESULT: 0x800A03EC | Excel Programming | |||
Exception from HRESULT: 0x800A03EC. at Microsoft.Office.Interop.Excel.WorkbookClass.get_VBProject() | Excel Programming | |||
Chart.Export throws COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC. | Excel Programming |