In this display the work done is plotted two ways. The blue line is the value based on the exact solution to the differential equation describing the spring. The trail of the red x marks the value based on adding up the areas of the little force by displacement rectangles. As you can see there is good agreement between the results. In fact by taking more displacement slices we can make the difference as small as we like.