Updating excel

  • Thread starter Thread starter Dwight Trumbower
  • Start date Start date
D

Dwight Trumbower

There has to be a better way than the following code. My main area of
question is getting data from a cell.
This following statement is the only way I can get it to work. Just seems
like to much clutter.
ConvType = CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
3],CurWorkSheet.Cells[Row, 3]).Value2.ToString() ;


private void ProcessToss( string[] filePaths)
{
int MaxRow;
string Col5, Col6, Col12, LookUp;
string ConvType;

XL._Worksheet CurWorkSheet;

using (XCel xlHand = new XCel()) // using a class that handles excel
connection
{
xlHand.Get_NewApp();
foreach( string TossFilePath in filePaths) // Can process multiple excel
files
{
xlHand.OpenFile (TossFilePath);
xlHand.ActivateWS(1);
MaxRow = xlHand.LastRow - 8 ;
CurWorkSheet = (XL._Worksheet )xlHand.CurrentSheet;
for ( int Row =9; Row <= MaxRow; Row++)
{
xlHand.StatusBar = string.Format("Current row is {0}", Row);
ConvType = CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
3],CurWorkSheet.Cells[Row, 3]).Value2.ToString() ;
if( ConvType == "T")
{

if(CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
5],CurWorkSheet.Cells[Row, 5]).Value2 == null)
Col5 = "";
else
Col5 = CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
5],CurWorkSheet.Cells[Row, 5]).Value2.ToString();

if(CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
6],CurWorkSheet.Cells[Row, 6]).Value2.ToString() == null)
Col6 = "";
else
Col6 = CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
6],CurWorkSheet.Cells[Row, 6]).Value2.ToString();

if(CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
12],CurWorkSheet.Cells[Row, 12]).Value2.ToString() == null)
Col12 = "";
else
Col12 = CurWorkSheet.get_Range (CurWorkSheet.Cells[Row,
12],CurWorkSheet.Cells[Row, 12]).Value2.ToString();

LookUp = ConvType + Col5 + Col6 +Col12;
if( this.textTable.Contains(LookUp))
{
CurWorkSheet.Cells[ Row, 4 ] = this.textTable[LookUp].ToString ();
}
}
}
xlHand.SaveWB (TossFilePath.Substring (0,18) + "UniqueText.xls");
xlHand.StatusBar = "";
}
}
}

Thanks
 
Dwight,

It seems you are calling the accessors directly (get_Range), instead of
just the properties (Range). Why are you doing this?

Also, you are creating a number of references to the excel workbooks and
pages that are not being cleaned up correctly, and therefore, will leave a
number of excel instances running.

Hope this helps.
 
message news: said:
Dwight,

It seems you are calling the accessors directly (get_Range), instead of
just the properties (Range). Why are you doing this?

Because this is the only way I could get the data. Any other method I tried
failed.

Dwight
 
Dwight,

How did it fail? You couldn't compile? An exception was thrown?
 
I tried this ConvType = CurWorkSheet.Cells[Row,3].ToString() ; Which I think
should work, but all it does is return a string, "System.__ComOBject".

I either recieved casting errors or not the correct data.

Nicholas Paldino said:
Dwight,

How did it fail? You couldn't compile? An exception was thrown?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dwight Trumbower said:
in



Because this is the only way I could get the data. Any other method I
tried
failed.

Dwight
 
Back
Top