Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Iterating through workbook capturing text

Hello all.

I'm trying to write a simple C# interop class that, when given a valid
path to an Excel file, returns the textual contents of that file.

(Assume that the info parameter refers to a valid XLS file.)

public string parse( FileInfo info )
{
StringBuilder sb = new StringBuilder();
Excel.Application app = new Excel.Application();
app.Visible = false;

Excel.Workbook wb = ,
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 );

wb.Activate();

//
// Attempt # 1:
//

try
{
Excel.Range range = app.get_Range( "Sheets", Type.Missing );
sb.Append( range.Text );
}
catch( Exception e )
{
Console.WriteLine( "Error parsing excel document." );
Console.WriteLine( e.Message );
Console.WriteLine( e.StackTrace );
}

//
// Prints the exception:
//
// Error parsing excel document.
// Exception from HRESULT: 0x800A03EC.
// at Excel.ApplicationClass.get_Range(Object Cell1, Object Cell2)
//

//
// Attempt # 2:
//

try
{
foreach( Excel._Worksheet sheet in wb.Worksheets )
{
foreach( Excel.Range cell in sheet.Cells )
{
sb.Append( cell.Text );
}
}
}
catch( Exception e )
{
Console.WriteLine( "Error parsing excel document." );
Console.WriteLine( e.Message );
Console.WriteLine( e.StackTrace );
}

//
// Prints the exception:
//
// Error parsing excel document.
// Member not found.
// at System.RuntimeType.ForwardCallToInvokeMember
// (String memberName, BindingFlags flags,
// Object target, Int32[] aWrapperTypes,
// MessageData& msgData)
// at Excel.Range.GetEnumerator()
//

wb.Close(false, Type.Missing, Type.Missing);
app.Quit();
return sb.ToString();
}

Any help would be greatly appreciated.
TIA,
-Matt
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Iterating through workbook capturing text

Why don't you just use interop to save the file as an XML Spreadsheet, then use an XSLT transform

Otherwise, you probably want to replace sheet.Cells with sheet.UsedRange, and I think cell.Text should be cell.Value in your second attempt

-- Jeff
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default Iterating through workbook capturing text

The Excel.Range object's Text property is read-only. try using the
Value (actually, with c# Value2 is more usable) or Formula properties
instead.

--

(Matt Giedt) wrote in message om...
Hello all.

I'm trying to write a simple C# interop class that, when given a valid
path to an Excel file, returns the textual contents of that file.

(Assume that the info parameter refers to a valid XLS file.)

public string parse( FileInfo info )
{
StringBuilder sb = new StringBuilder();
Excel.Application app = new Excel.Application();
app.Visible = false;

Excel.Workbook wb = ,
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 );

wb.Activate();

//
// Attempt # 1:
//

try
{
Excel.Range range = app.get_Range( "Sheets", Type.Missing );
sb.Append( range.Text );
}
catch( Exception e )
{
Console.WriteLine( "Error parsing excel document." );
Console.WriteLine( e.Message );
Console.WriteLine( e.StackTrace );
}

//
// Prints the exception:
//
// Error parsing excel document.
// Exception from HRESULT: 0x800A03EC.
// at Excel.ApplicationClass.get_Range(Object Cell1, Object Cell2)
//

//
// Attempt # 2:
//

try
{
foreach( Excel._Worksheet sheet in wb.Worksheets )
{
foreach( Excel.Range cell in sheet.Cells )
{
sb.Append( cell.Text );
}
}
}
catch( Exception e )
{
Console.WriteLine( "Error parsing excel document." );
Console.WriteLine( e.Message );
Console.WriteLine( e.StackTrace );
}

//
// Prints the exception:
//
// Error parsing excel document.
// Member not found.
// at System.RuntimeType.ForwardCallToInvokeMember
// (String memberName, BindingFlags flags,
// Object target, Int32[] aWrapperTypes,
// MessageData& msgData)
// at Excel.Range.GetEnumerator()
//

wb.Close(false, Type.Missing, Type.Missing);
app.Quit();
return sb.ToString();
}

Any help would be greatly appreciated.
TIA,
-Matt

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing a word sequence as part of a text in a spreadsheet cell T.Mad Excel Worksheet Functions 4 July 3rd 07 12:08 PM
Iterating files in folder Minerva Excel Worksheet Functions 1 January 11th 06 10:00 AM
iterating checkboxes [email protected] Excel Discussion (Misc queries) 1 May 11th 05 08:42 PM
Capturing Shape Name/Text from OnAction property William Bartusek Excel Discussion (Misc queries) 3 April 12th 05 06:38 PM
Capturing the directory when a workbook is opened from Explorer Brian Walters[_2_] Excel Programming 1 December 1st 03 05:17 PM


All times are GMT +1. The time now is 06:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"