Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Office Document with .NET [ /b]

Status
Not open for further replies.

ysommer

Programmer
Feb 26, 2001
45
0
0
Hi, a trying to create a new Office document (word or excel)
through code, without opening the application itself.
I dont want to create it in stream writer beacuse it doesnt get the properties of a Word or Excel document.
Does anyone have an idea?
 
Here is some code to open an Excel Sheeet:

Code:
Dim oExcel As New Excel.Application()
Dim oSheet As Excel.Worksheet

oExcel.Visible = False
oSheet = oExcel.Workbooks.Add.Worksheets(1)

CODE FOR POPULATING

oExcel.Visible = True

You will need the Interop.Excel.dll reference loaded..

Hope this helps

 
Here's another example:

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet

' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = False

' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet

' Add column headers
oSheet.Cells(1, 1).Value = "EVENT_DATE"
oSheet.Cells(1, 2).Value = "USER_ID"
oSheet.Cells(1, 3).Value = "USER_NAME"
oSheet.Cells(1, 4).Value = "DOCUMENT_ID"
oSheet.Cells(1, 5).Value = "EVENT_ID"
oSheet.Cells(1, 6).Value = "DOC_TYPE"
oSheet.Cells(1, 7).Value = "JHH_BMC_ACNT"
oSheet.Cells(1, 8).Value = "JHH_BMC_MRN"
oSheet.Cells(1, 9).Value = "LAST_NAME"
oSheet.Cells(1, 10).Value = "FIRST_NAME"
oSheet.Cells(1, 11).Value = "MIDDLE"
oSheet.Cells(1, 12).Value = "AUDIT_DESCRIPTION"
oSheet.Cells(1, 13).Value = "EVENT_DESCRIPTION"

' Format as bold, vertical alignment = center.
With oSheet.Range("A1", "M1")
.Font.Bold = True
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
End With

'Load Excel spreadsheet with the Datagrid items
For intRow = 0 To intRowCnt - 1
For intCol = 0 To 12
oSheet.Cells(intRow + 2, intCol + 1).Value = DataGrid1.Item(intRow, intCol)
Next
Next

'Save spreadsheet
oSheet.SaveAs(strName)

'Clean up
oSheet = Nothing
oWB.Close()
oWB = Nothing
oXL.Quit()
oXL = Nothing
'Garbage collection - Required to end the Windows Task Manager process - Excel.exe
GC.Collect()

End Sub


You will also need to load Interop.Office.dll
 
Thank's very much
For your help...

Y.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top