开发者

Copy excel rows in visual basic .net (VS 2008, .NET FrameWork 3.5)

开发者 https://www.devze.com 2023-03-01 01:24 出处:网络
SOLUTION FOUND: CHECK BOTTOM OF POST Hello, I\'m using the excel object model (importing namespace Microsoft.Office.Interop.Excel) to open an Excel.Application object in my code, then open a workboo

SOLUTION FOUND: CHECK BOTTOM OF POST

Hello,

I'm using the excel object model (importing namespace Microsoft.Office.Interop.Excel) to open an Excel.Application object in my code, then open a workbook, worksheet, etc. What I'm doing is ridiculously simple: I'm opening two worksheet objects; one is already populated with data and another one is empty and is supposed to be populated with some rows from the first worksheet. If some conditions, regarding cells in the first worksheet, are not met, then I want to copy the entire row in the second worksheet, at the first available row. I have an integer counter, initialized at 1, which indexes the first empty row in the output worksheet this counter is obviously incremented every time I insert a row at outputSheet.Rows(counter).

Every solution I've Googled either uses VBA (I'm looking for a solution based on VB.NET) or is not compatible with .NET framework 3.5 (I'm receiving exceptions at runtime). I'm guessing I'm not using the Range object correctly or something; in any case, what's the simplest way to do this? I really don't want to open an OLEDB connection to my excel file: this is too complicated for the simple row copying mechanism I'm trying to implement.

Thanks,

Jason

Edit #1: Typical kind of excel records I Have to deal with:

1 | V | 1 | 01078215 | 02 | full name

2| V | 1 | 8048321 | 01| full name

If, for example, the fourth cell of a row is not 8 digits long, I want to copy the row AS IS to a second excel file.

Edit#2: Code:

'copy a row from an excel spreadsheet to another

Sub CopyRowTo(ByVal input As Excel.Worksheet, 

                  ByVal output As Excel.Worksheet, 

                  ByVal row_in As Integer, 

                  ByVal row_out As Integer)

  input.Activate()

  input.Rows(row_in).Select()

  input.Rows(row_in).Copy()

  output.Activate()

  output.Rows(row_out).Select()

  output.Rows(row_out).Paste()

End Sub

Called As : CopyRowTo(inputSheet, outputSheet, 1, 1)

#

SOLUTION FOUND:

I have found the solution to my problem. The following code did the trick:

input.Activate()

input.Rows(row_in).Copy()

output.Activate()

output.Rows(row_out).Select()

output.Paste()

It appears that, for some reason, I need to a开发者_如何学编程pply the paste method to the Excel.Worksheet object, after having Select() - ed the range contained within that object (I do not get VB.NET at all, honestly).

Thanks,

J

#


If you're within the limitations of the free version (150 rows per sheet, up to 5 worksheets), the Gembox library makes it easy: http://www.gemboxsoftware.com/GBSpreadsheetFree.htm

There are probably other similar things available.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号