Skip to main content
Expert
Apr 27, 2023 12:37 PM UTC
Solved

Create Excel spreadsheet from Business Rules

Does anyone know if you can create an Excel spreadsheet from Business Rules? I do not mean a csv file - I need to create multiple sheets. The OpenXMLBuilder object doesn't seem to have anything with writing files. I don't want to use a third-party object as that would require an installation or download.

Thanks for any suggestions.

Contributor
Apr 27, 2023 8:47 PM UTC

You can use this function it takes a data table as parameter and generates an excel file I don't know what your exact requirements are but this can be a good starting point and you can modify it further.

Imports System
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Public Sub CreateExcelFileFromDataTable(dataTable As DataTable)
	'Create a new Excel file
	Dim filePath As String = "C:\example.xlsx"
	'Create the SpreadsheetDocument object and set its type to Workbook
	Using document As SpreadsheetDocument = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook)
		'Create the workbook
		Dim workbookPart As WorkbookPart = document.AddWorkbookPart()
		workbookPart.Workbook = New Workbook()
		'Create the worksheet
		Dim worksheetPart As WorksheetPart = workbookPart.AddNewPart(Of WorksheetPart)()
		worksheetPart.Worksheet = New Worksheet()
		'Create the sheet data
		Dim sheetData As SheetData = worksheetPart.Worksheet.AppendChild(New SheetData())
		'Create the header row
		Dim headerRow As Row = New Row()
		'Loop through each column in the DataTable and add it to the header row
		For Each column As DataColumn In dataTable.Columns
			headerRow.AppendChild(New Cell() With {.DataType = CellValues.String, .CellValue = New CellValue(column.ColumnName)})
		Next

		'Add the header row to the sheet data
		sheetData.AppendChild(headerRow)
		'Populate the data rows
		For Each dataRow As DataRow In dataTable.Rows
			Dim row As Row = New Row()
			'Loop through each column in the DataTable and add the corresponding cell value to the current row
			For Each column As DataColumn In dataTable.Columns
				row.AppendChild(New Cell() With {.DataType = CellValues.String, .CellValue = New CellValue(dataRow(column.ColumnName).ToString())})
			Next

			'Add the row to the sheet data
			sheetData.AppendChild(row)
		Next

		'Create the sheets
		Dim sheets As Sheets = workbookPart.Workbook.AppendChild(New Sheets())
		Dim sheet As Sheet = New Sheet() With {.Id = workbookPart.GetIdOfPart(worksheetPart), .SheetId = 1, .Name = "Sheet1"}
		sheets.Append(sheet)
		'Save changes
		workbookPart.Workbook.Save()
	End Using
End Sub

 

31 replies

Apr 27, 2023 1:29 PM UTC

Are you looking to extract data to the excel sheet or exporting some report to an excel file ?

MarcusHAuthor
Expert
Apr 27, 2023 1:43 PM UTC

It's not a report, it's security information that needs to be formatted in a particular way (Security Audit Reports doesn't have a report that will present the required information).

Apr 27, 2023 2:15 PM UTC

You can use Streamwrite to write to files.

Ex-

Using sWriter As StreamWriter = File.CreateText(filePath)

Dim line As New Text.StringBuilder

sWriter.WriteLine(line.ToString())
Member
Apr 27, 2023 2:20 PM UTC

You have two options, use openxml or use a 3rd party.  Did you try openxml?

MarcusHAuthor
Expert
Apr 27, 2023 2:27 PM UTC

I couldn't see any functions in OpenXMLBuilder that allows me to create Excel files. Or are you suggesting using the generic Open XML package? 

Contributor
Apr 27, 2023 8:47 PM UTC

You can use this function it takes a data table as parameter and generates an excel file I don't know what your exact requirements are but this can be a good starting point and you can modify it further.

Imports System
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Public Sub CreateExcelFileFromDataTable(dataTable As DataTable)
	'Create a new Excel file
	Dim filePath As String = "C:\example.xlsx"
	'Create the SpreadsheetDocument object and set its type to Workbook
	Using document As SpreadsheetDocument = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook)
		'Create the workbook
		Dim workbookPart As WorkbookPart = document.AddWorkbookPart()
		workbookPart.Workbook = New Workbook()
		'Create the worksheet
		Dim worksheetPart As WorksheetPart = workbookPart.AddNewPart(Of WorksheetPart)()
		worksheetPart.Worksheet = New Worksheet()
		'Create the sheet data
		Dim sheetData As SheetData = worksheetPart.Worksheet.AppendChild(New SheetData())
		'Create the header row
		Dim headerRow As Row = New Row()
		'Loop through each column in the DataTable and add it to the header row
		For Each column As DataColumn In dataTable.Columns
			headerRow.AppendChild(New Cell() With {.DataType = CellValues.String, .CellValue = New CellValue(column.ColumnName)})
		Next

		'Add the header row to the sheet data
		sheetData.AppendChild(headerRow)
		'Populate the data rows
		For Each dataRow As DataRow In dataTable.Rows
			Dim row As Row = New Row()
			'Loop through each column in the DataTable and add the corresponding cell value to the current row
			For Each column As DataColumn In dataTable.Columns
				row.AppendChild(New Cell() With {.DataType = CellValues.String, .CellValue = New CellValue(dataRow(column.ColumnName).ToString())})
			Next

			'Add the row to the sheet data
			sheetData.AppendChild(row)
		Next

		'Create the sheets
		Dim sheets As Sheets = workbookPart.Workbook.AppendChild(New Sheets())
		Dim sheet As Sheet = New Sheet() With {.Id = workbookPart.GetIdOfPart(worksheetPart), .SheetId = 1, .Name = "Sheet1"}
		sheets.Append(sheet)
		'Save changes
		workbookPart.Workbook.Save()
	End Using
End Sub

 

MarcusHAuthor
Expert
Apr 28, 2023 7:34 AM UTC

Thank you so much for that. I will test it next week and post an update.

Contributor
Apr 28, 2023 2:28 PM UTC

Also add these references in your BR

C:\Program Files\OneStream Software\OneStreamAppRoot\OneStreamApp\bin\DocumentFormat.OpenXml.dll; C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll

 

pjbwilson
Member
Feb 25, 2025 2:21 PM UTC

I am testing an upgrade to 8.5 and my rule creating a spreadsheet does not compile anymore, has anyone else seen this issue? 

MarcusHAuthor
Expert
Feb 25, 2025 2:28 PM UTC

I had to make changes to get it to run in 8.4. I haven't tried 8.5. What error are you getting? In 8.4 you need to add these to the referenced assemblies:

DocumentFormat.OpenXml.dll; XF\System.IO.Packaging.dll

And these imports:

Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

 

 

Contributor
Feb 25, 2025 5:01 PM UTC

Those referenced assemblies are compatible with version 8.5 as well.

The only modification from earlier versions to version 8 is due to the latest .NET changes introduced in version 8.

pjbwilson
Member
Feb 25, 2025 3:24 PM UTC

Marcus,

Thanks for getting back to me, I get:

Error compiling Business Rule 'xxxxxx'. 

  1. Error at line 2669:  Type 'SpreadsheetDocument' is not defined.

And then related issues. We are going from 7.3 to 8.5, so I think it is just this issue, and also I am testing on a different environment, so I think I just need to play around with the referencing.

Cheers,

Peter