The elastic potential energy of a spring is given by
[tex]U= \frac{1}{2}kx^2 [/tex]
where k is the spring's constant and x is the displacement with respect to the relaxed position of the spring.
The work done by the spring is the negative of the potential energy difference between the final and initial condition of the spring:
[tex]W=-\Delta U = \frac{1}{2}kx_i^2 - \frac{1}{2}kx_f^2 [/tex]
In our problem, initially the spring is uncompressed, so [tex]x_i=0[/tex]. Therefore, the work done by the spring when it is compressed until [tex]x_f[/tex] is
[tex]W=- \frac{1}{2}kx_f^2 [/tex]
And this value is actually negative, because the box is responsible for the spring's compression, so the work is done by the box.