Hi guys,
I have tried to export the dataset with the operator name in Excel. export dataset successfully using the below script but how to export the operator name in cell A1 any idea?
def datasetToExcel(dsIn,fileName = '',sheetName = 'Sheet1',firstRow = 0, firstCol = 0,dateFormat = 'yyyy-mm-dd hh:mm:ss'):
import org.apache.poi.ss.usermodel.Cell as Cell
import org.apache.poi.ss.usermodel.Row as Row
import org.apache.poi.ss.usermodel.Sheet as Sheet
import org.apache.poi.ss.usermodel.Workbook as Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook as XSSFWorkbook
import org.apache.poi.xssf.usermodel.XSSFDataFormat as XSSFDataFormat
from java.io import FileOutputStream, ByteArrayOutputStream
dsType = str(type(dsIn))
if 'com.inductiveautomation.ignition.common.BasicDataset' in dsType:
pyDS = system.dataset.toPyDataSet(dsIn)
elif 'com.inductiveautomation.ignition.common.JsonDataset' in dsType:
pyDS = system.dataset.toPyDataSet(dsIn)
elif 'com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet' in dsType:
pyDS = dsIn
else:
raise Exception('Not a valid DataSet')
if fileName == '':
output = ByteArrayOutputStream()
else:
output = FileOutputStream(fileName)
# Create workbook
wb = XSSFWorkbook()
# Create Sheet
sheet = wb.createSheet(sheetName)
# Create formatter
fmt = wb.createDataFormat()
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello world')
# Create style for headers
headerStyle = wb.createCellStyle()
headerFont = wb.createFont()
headerFont.setBold(True)
headerFont.setFontHeightInPoints(10)
headerFont.setFontName('Arial')
headerStyle.setFont(headerFont)
# Create style for data
rowStyle = wb.createCellStyle()
rowFont = wb.createFont()
rowFont.setBold(False)
rowFont.setFontHeightInPoints(10)
rowFont.setFontName('Arial')
rowStyle.setFont(rowFont)
# Create style for dates.
dateStyle = wb.createCellStyle()
dateFont = wb.createFont()
dateFont.setBold(False)
dateFont.setFontHeightInPoints(10)
dateFont.setFontName('Arial')
dateStyle.setFont(dateFont)
dateStyle.setDataFormat(fmt.getFormat(dateFormat.lower()))
# Create header row in the sheet
headerRow = sheet.createRow(firstRow)
for j, col in enumerate(pyDS.getColumnNames()):
cell = headerRow.createCell(j+firstCol)
cell.setCellStyle(headerStyle)
cell.setCellValue(col)
# Create data rows
for i, row in enumerate(pyDS):
dataRow = sheet.createRow(i+1+firstRow)
for j, col in enumerate(list(row)):
cell = dataRow.createCell(j+firstCol)
cell.setCellValue(col)
cell.setCellStyle(rowStyle)
# Check if it's a date, and set cell format accordingly
if 'java.util.Date' in str(type(col)):
cell.setCellStyle(dateStyle)
# Resize the columns
for i in range(pyDS.getColumnCount()):
sheet.autoSizeColumn(i)
wb.write(output)
output.close()
if fileName == '':
return output.toByteArray()
else:
return
today = system.date.now()
date = system.date.format(today, "dd.MM.yyyy")
time = system.date.format(today, "HHmm")
#if filePathAllFormulas is not None:
selectedLabel = event.source.parent.getComponent('Dropdown 1').selectedLabel
if selectedLabel == 'Exporting all formulas':
query = "Select * from Test"
pyDataset = system.db.runPrepQuery(query)
ds = system.dataset.toDataSet(pyDataset)
# Create file name
fileName = "AllFormulas_" +date+ "_" +time+ ".xlsx"
filePathAllFormulas = system.file.saveFile(fileName)
# Create Excel file
datasetToExcel(ds, filePathAllFormulas, firstRow=3)
# Open the Excel file
system.net.openURL("file://"+filePathAllFormulas)
elif selectedLabel != 'Exporting all formulas':
query = "Select * from Test where Name = ?"
Name = event.source.parent.getComponent('Dropdown 1').selectedLabel
args = [Name]
pyDataset = system.db.runPrepQuery(query, args)
dsIn = system.dataset.toDataSet(pyDataset)
# Create file name
fileNames = "SelectedFormulas_" +date+ "_" +time+ ".xlsx"
filePathFormula = system.file.saveFile(fileNames)
# Create Excel file
datasetToExcel(dsIn,filePathFormula, firstRow=3)
# Open the Excel file
system.net.openURL("file://"+filePathFormula)
Thank you,
Priyanka