# Export table to csv file selected row and column

**URL:** <https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603>\
**Category:** Ignition\
**Created:** [December 1, 2021, 4:03am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603 "2021-12-01T04:03:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiamweekhoo](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@thiamweekhoo](https://forum.inductiveautomation.com/u/thiamweekhoo)\
**Post date:** [December 1, 2021, 4:03am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/1 "2021-12-01T04:03:56Z")

</div>

Hello,

I have 2 Table inside my template, Table1 and Table2.  
Trying to export the data from both table to one single CSV file.  
I am using below scripting. I would like to have Table1 to start from CSV Row 1, Column 1 and Table2 to start from CSV Row 20, Column 5. in the same CSV file. Is that possible ? Thank you

> template = event.source.parent.getComponent('Summary\_Report')  
> loadedTemplate = template.loadedTemplate  
> myTable = loadedTemplate.getComponent("Table1")  
> data = myTable.data
> 
> filePath = system.dataset.exportCSV("data.csv", 1, data)  
> if filePath != None:  
> system.net.openURL("file:///"+filePath.replace('\','/'))

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [December 1, 2021, 8:18am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/2 "2021-12-01T08:18:51Z")

</div>

No even opened the designer for this haha, hope it works. ds1 is dataset1 and ds2 obviously is dataset2

```auto
headers1 = system.dataset.getColumnHeaders(ds1)
headers2 = system.dataset.getColumnHeaders(ds2)
headers = headers1+headers2
rows = []
for r in range(ds1.rowCount):
	row = []
	for c in range(ds1.columnCount):
		row.append(ds1.getValueAt(r,c))
	rows.append(row +[None for i in headers2])
for r in range(ds2.rowCount):
	row = [None for i in headers1]
	for c in range(ds2.columnCount):
		row.append(ds2.getValueAt(r,c))
	rows.append(row)
weirdDataset = system.dataset.toDataSet(headers,rows)

```

Then change this to

```auto
filePath = system.dataset.exportCSV(“data.csv”, 1, weirdDataset)

```

---

<div class="post-metadata">

**Author:** ![thiamweekhoo](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@thiamweekhoo](https://forum.inductiveautomation.com/u/thiamweekhoo)\
**Post date:** [December 1, 2021, 8:54am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/3 "2021-12-01T08:54:37Z")

</div>

hi jespinmartin, both my table is not having the same column…will that works?

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [December 1, 2021, 9:02am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/4 "2021-12-01T09:02:50Z")

</div>

no you will have to add in the misisng columns on both then (with value null or whatever is appropriate)

---

<div class="post-metadata">

**Author:** ![thiamweekhoo](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@thiamweekhoo](https://forum.inductiveautomation.com/u/thiamweekhoo)\
**Post date:** [December 1, 2021, 9:32am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/5 "2021-12-01T09:32:49Z")

</div>

hmm, then I still can’t select the row number on csv file isn’t it

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [December 1, 2021, 9:44am UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/6 "2021-12-01T09:44:16Z")

</div>

rownumber shoudlnt be affected?  
[https://docs.inductiveautomation.com/display/DOC81/system.dataset.addColumn](https://docs.inductiveautomation.com/display/DOC81/system.dataset.addColumn)

here is some pseudo code, doesnt work but you get the idea^^ you ofc should use a loop for the missing columns

#ds1headers[“1”,“2”,“3”]  
#ds2headers[“4”,“5”,“6”]

```auto
ds1 =system.dataset.addColumn(ds1, 3, [None]*ds1.getRowCount(), "4", type )
ds1 =system.dataset.addColumn(ds1, 4, [None]*ds1.getRowCount(), "5", type )
ds1 =system.dataset.addColumn(ds1, 5, [None]*ds1.getRowCount(), "6", type )
ds2 =system.dataset.addColumn(ds2, 0, [None]*ds2.getRowCount(), "1", type )
ds2 =system.dataset.addColumn(ds2, 1, [None]*ds2.getRowCount(), "2", type )
ds2 =system.dataset.addColumn(ds2, 2, [None]*ds2.getRowCount(), "3", type )

```

#ds1headerss=[1,2,3,4,5,6]  
#ds2headerss=[1,2,3,4,5,6]

the rest of the code should work just fine

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [December 1, 2021, 2:14pm UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/7 "2021-12-01T14:14:11Z")

</div>

It should work, I made an edit, test it yourself

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/f/5f3498dc0310f56672167532b7fbf75f3470844e.png)

---

<div class="post-metadata">

**Author:** ![thiamweekhoo](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@thiamweekhoo](https://forum.inductiveautomation.com/u/thiamweekhoo)\
**Post date:** [December 1, 2021, 4:36pm UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/8 "2021-12-01T16:36:04Z")

</div>

yes sir thanks, your script worked…but if I can have Table 2 to be at (A,10) coordinate or somewhere below the last line of Table 1 then that will be wonderful though…

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/e/8e6e1c7be7b78cfe782646845c95a7330b897593.png)

---

<div class="post-metadata">

**Author:** ![jespinmartin1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jespinmartin1/32/57175_2.png) [@jespinmartin1](https://forum.inductiveautomation.com/u/jespinmartin1)\
**Post date:** [December 1, 2021, 5:19pm UTC](https://forum.inductiveautomation.com/t/export-table-to-csv-file-selected-row-and-column/53603/9 "2021-12-01T17:19:52Z")

</div>

I’m afraid you were not clear at first, anyways. Here:

```auto
ds1 = event.source.parent.getComponent('Table').data
ds2 = event.source.parent.getComponent('Table 1').data
wider = max(len(system.dataset.getColumnHeaders(ds1)),len(system.dataset.getColumnHeaders(ds2)))
narrower = min(len(system.dataset.getColumnHeaders(ds1)),len(system.dataset.getColumnHeaders(ds2)))
largeDataset = ds1 if wider == ds1.columnCount else ds2
smallDataset = ds2 if narrower == ds2.columnCount else ds1
rowsGap = 5
rows = []
for r in range(ds1.rowCount):
	row = []
	for c in range(largeDataset.columnCount):
		try:
			row.append(str(largeDataset.getValueAt(r,c)))
		except:
			row.append(None)
	rows.append(row)
for i in range(rowsGap):
	rows.append([None for j in range(largeDataset.columnCount)])
midHeader = [h for h in system.dataset.getColumnHeaders(smallDataset)]
rows.append(midHeader+[None for k in range(largeDataset.columnCount-smallDataset.columnCount)])
for r in range(ds2.rowCount):
	row = []
	for c in range(largeDataset.columnCount):
		try:
			row.append(str(smallDataset.getValueAt(r,c)))
		except:
			row.append(None)
	rows.append(row)
weirdDataset = system.dataset.toDataSet(system.dataset.getColumnHeaders(largeDataset), rows)

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/d/9d6d0cd5b7f4b6ef059e66da102dc714c6ffabb2.png)
