Implement write_safe_csv(rows, fieldnames, stream). Write a header and every row using Python's CSV rules with \n line endings, returning the number of data rows.
Reject duplicate/empty field names and unexpected row keys with ValueError. Missing fields become empty cells. Before writing, prefix string cells with a single quote when their first character is =, +, -, @, tab, or carriage return so spreadsheet applications treat them as text. Do not mutate rows or fieldnames.