The OneStream Community is temporarily frozen until June 29th due to the ongoing maintenance. Please read the blog post here to learn more.
Forum Discussion
MarcusH
3 years agoValued Contributor
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 ...
- 3 years ago
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
Omkareshwar
3 years agoContributor
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
MarcusH
3 years agoValued Contributor
Thank you so much for that. I will test it next week and post an update.
- Omkareshwar3 years agoContributor
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
- MarcusH3 years agoValued Contributor
Works really well. Thank you for sharing your code.
- Sridhar_M1 year agoContributor
We're upgrading to OneStream version 8.2.2 and encountering issues with code compilation due to changes in .NET 8. I've managed to fix many errors by updating the Open XML DLL path to:
C:\Program Files\OneStream Software\OneStreamAppRoot\OneStreamApp\DocumentFormat.OpenXml.dll
However, I'm still facing the following error:
"Reference required to assembly 'System.IO.Packaging, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' containing the type 'Package'. Add one to your project."
Has anyone found a solution to this problem?
- Sridhar_M1 year agoContributor
Never mind, I found the System.IO.Packaging.dll at this location, which resolved all the problems:
C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\8.0.7\System.IO.Packaging.dll
Related Content
- 7 months ago
- 2 months ago