stream = MemoryStream()
try:
    table = Document.Data.Tables[jsonParam['tableName']]
    names = []
    columns = table.Columns
    if(jsonParam['columns'] != None):
        columns = jsonParam['columns']
        for colName in columns:
            names.append(colName)
    else:
        for col in columns:
            names.append(col.Name)
    writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.SbdfDataWriter)

    filtering = []
    if(jsonParam['filterRows']['type'] == 'All'):
        filtering = range(0, table.RowCount)
    elif(jsonParam['filterRows']['type'] == 'Marking'):
        marking = None
        if (jsonParam['filterRows']['name'] != None):
            marking = Document.Data.Markings[jsonParam['filterRows']['name']]
        else:
            marking = Document.ActiveMarkingSelectionReference
        markedRowsIndex = marking.GetSelection(table).AsIndexSet()
        for index in markedRowsIndex:
            filtering.append(index)
    elif(jsonParam['filterRows']['type'] == 'Filtering'):
        filteredRowsIndex = Document.ActiveFilteringSelectionReference.GetSelection(table).AsIndexSet()
        for index in filteredRowsIndex:
            filtering.append(index)

    if (len(filtering) != 0):
        writer.Write(stream, table, filtering, names)
    stream.Position = 0