Page 1 of 1 1
Topic Options
#183058 - 2007-11-29 09:28 PM Add new rows in excel file
Adolfo Offline
Fresh Scripter
*****

Registered: 2007-01-25
Posts: 49
Loc: Cali, CO
Hi all,

How can I open an exist excel file, select a sheet and then write a new row.

Thanks for your time and help.

Top
#183060 - 2007-11-29 10:32 PM Re: Add new rows in excel file [Re: Adolfo]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Hi Adolfo,

This should get you started:

create a 'C:\test.xls' then run this code:
 Code:
;path and file name to open
$FileName = "c:\test.xls"

;create Excel Object
$ObjExcel = CreateObject("Excel.Application")
$ObjExcel.Visible = "True"

;don't display alerts
$ObjExcel.DisplayAlerts = "False"

;to have user interaction: show excel openfile dialog 
;$ObjWorkbook = $ObjExcel.GetOpenFilename

;to open a predetermined file if it exists
If Exist($FileName)
	;set $ObjWorkbook = the FileName to be opened and open
	$ObjWorkbook = $ObjExcel.Workbooks.Open($FileName)
Else
	$nul = MessageBox($FileName + ' does not exist and will not be opened', 'Error Opening File,..', 16)
	;Exit excel If needed
	$nul = $ObjExcel.Quit
EndIf

;identify worksheet where Worksheets(1) identifes the first sheet, 
;could be changed to Worksheets(2) to identify the second sheet
$ObjWorksheet = $ObjWorkbook.Worksheets(1)

;** line styles **
;Continuous					1
;Dash								-4115
;DashDot						4
;DashDotDot					5
;Dot								-4118
;Double							-4119
;LineStyleNone			-4142
;SlantDashDot				13
$LineStyle = 1

;** line thickness **
;Hairline						1
;Thin								2
;Medium							-4138
;Thick							4
$LineThickness = 2

;** border style **
;DiagonalDown				5
;DiagonalUp					6
;EdgeBottom					9
;EdgeLeft						7
;EdgeRight					10
;EdgeTop						8
;InsideHorizontal		12
;InsideVertical			11
;All								13
$BorderStyle = 13

;** horizontal alignment **
;left -4131
;right -4152
;center -4108
$HorizontalAlignment = - 4108
		
;** vertical alignment **
;top -4160
;bottom -4107
;center -4108
$VerticalAlignment = - 4108

;add a Value to row 1, column 1, wrap text, set to bold, set font size and font name
$ObjWorksheet.Cells(1, 1).Value = "This is the cell we want to wrap text"
$ObjWorksheet.Cells(1, 1).WrapText = "True"
$ObjWorksheet.Cells(1, 1).Font.Bold = "True"
$ObjWorksheet.Cells(1, 1).Font.Size = 8
$ObjWorksheet.Cells(1, 1).Font.Name = "Times New Roman"

;add cell in column 8, set to bold, set font size and turn on autofit for column 8
$ObjWorksheet.Cells(1, 8).Value = "This is the row we want to autofit"
$ObjWorksheet.Cells(1, 8).Font.Bold = "True"
$ObjWorksheet.Cells(1, 8).Font.Size = 18
$nul = $ObjWorksheet.Columns(8).AutoFit = "True"

;add Values to individual cells, HorizontalAlignment and VerticalAlignment
;$ObjWorksheet.Cells(Row,Column).Value = "A"
$ObjWorksheet.Cells(2, 1).Value = "A"
;left
$ObjWorksheet.Cells(2, 1).HorizontalAlignment = - 4131
;top
$ObjWorksheet.Cells(2, 1).VerticalAlignment = - 4160

$ObjWorksheet.Cells(2, 2).Value = "B"
;center
$ObjWorksheet.Cells(2, 2).HorizontalAlignment = - 4108
;center
$ObjWorksheet.Cells(2, 2).VerticalAlignment = - 4108

$ObjWorksheet.Cells(2, 3).Value = "C"
;right
$ObjWorksheet.Cells(2, 3).HorizontalAlignment = - 4152
;bottom
$ObjWorksheet.Cells(2, 3).VerticalAlignment = - 4107

;set row 2 to bold
;  $ObjExcel.Rows(2).Font.Bold = "True"

;set column 2 to bold
;  $ObjExcel.Columns(2).Font.Bold = "True"

;set a range of cells A2, B2, C2 to bold
$ObjWorksheet.Range("A2", "C2").Font.Bold = "True"

;set all used cells to bold
;  $ObjWorksheet.UsedRange.Font.Bold = "True"

;set single cell horizontal alignment
$ObjWorksheet.Cells(1, 1).HorizontalAlignment = $HorizontalAlignment

;set single cell font color
$ObjWorksheet.Cells(1, 1).Font.ColorIndex = 3

;add multiple cells with multiple properties
$index = 1
For $col = 1 to 7
	For $row = 3 to 10
		;set HorizontalAlignment to center
		$ObjWorksheet.Cells($row, $col).HorizontalAlignment = $HorizontalAlignment
				
		;set VerticalAlignment to center
		$ObjWorksheet.Cells($row, $col).VerticalAlignment = $VerticalAlignment
				
		;add pattern color
		$ObjWorksheet.Cells($row, $col).Interior.ColorIndex = $index
				
		;set cell Value to index color
		$ObjWorksheet.Cells($row, $col).Value = $index
				
		;add border
		If $BorderStyle = 13
			;set all borders
			$ObjWorksheet.Cells($row, $col).Borders.LineStyle = $LineStyle
			$ObjWorksheet.Cells($row, $col).Borders.ColorIndex = 1
			$ObjWorksheet.Cells($row, $col).Borders.Weight = $LineThickness
		Else
			;set select border
			$ObjWorksheet.Cells($row, $col).Borders($BorderStyle).LineStyle = $LineStyle
			$ObjWorksheet.Cells($row, $col).Borders($BorderStyle).ColorIndex = 1
			$ObjWorksheet.Cells($row, $col).Borders($BorderStyle).Weight = $LineThickness
		EndIf
		$index = $index + 1
	Next
Next

;get used Column count
$UsedColumns = $ObjWorksheet.UsedRange.Columns.Count

;get used Row count
$UsedRows = $ObjWorksheet.UsedRange.Rows.Count

;add a new row of cells
;this will add a new row of cells every time this code is run
$ObjWorksheet.Cells($UsedRows + 1, 1).Value = "New Row Cell 1"
$ObjWorksheet.Cells($UsedRows + 1, 2).Value = "New Row Cell 2"
$ObjWorksheet.Cells($UsedRows + 1, 3).Value = "New Row Cell 3"

;add a new column to existing cells
;this will add a new column of cells every time this code is run
$ObjWorksheet.Cells(3, $UsedColumns + 1).Value = "New Column Cell 1"
$ObjWorksheet.Cells(4, $UsedColumns + 1).Value = "New Column Cell 2"
$ObjWorksheet.Cells(5, $UsedColumns + 1).Value = "New Column Cell 3"

;save workBook as FileName
$nul = $ObjWorkbook.SaveAs($FileName)

;exit excel
$nul = $ObjExcel.Quit



Edited by Benny69 (2007-11-29 11:52 PM)
Edit Reason: Ooooppps I mixed up a couple things and left out a couple but have modified the code I provided to reflect the needed changes
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#183062 - 2007-11-30 12:22 AM Re: Add new rows in excel file [Re: Benny69]
Adolfo Offline
Fresh Scripter
*****

Registered: 2007-01-25
Posts: 49
Loc: Cali, CO
Thanks Benny test was very useful
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.051 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org