I think the answer is positive and the following is a proof. All we will assume is that $X$ is locally factorial, which is implied by smoothness. I would really appreciate it if someone can check it though!
Let $Z$ be the complement of $U$ in $X$, as a topological space. Let $\xi_1,\dots,\xi_k$ be the codimension 1 primes coming from $Z$. Each $\xi_i$ defines a pure codimension-1 subvariety of $X$, call it $Y_i$, and all of them are contained in $Z$. Since $Y:=Y_1\cup\cdots\cup Y_k$ is pure codimension 1, and $X$ is locally factorial, $Y$ is an effective Cartier divisor. Thus the open set $U':=X\setminus Y$ is affine.
Now the complement of $U$ in $U'$ is codimension 2, so by Hartog's lemma we have an isomorphism
$$
\mathbb{C}[U']\to\mathbb{C}[U]
$$
coming from restriction of functions. It follows that $U'=\operatorname{Spec}\mathbb{C}[U]$, and we are done.