[integral from 0 to x']dW= [integral from 0 to x'] F(x') dx'
=[integral from 0 to x']kx' dx'
=1/2kx^2
It is a 1-D problem so line integral do not need to be used. At a given instant, let the amount by which the spring is already compressed be x'. The force in the spring is then F = kx', where k is the spring constant. This means if we compress the spring further by an infinitesimal dx, the work done is dW given by dW = kx' dx.
So it is possible to integrate to find the work done from x = 0 to x = x'.