We investigate nucleosynthesis in collapsars, based on long-term, magnetohydrodynamic simulations of a rapidly rotating massive star of 40M ⊙ during the core collapse. We have calculated detailed composition of magnetically driven jets ejected from the collapsars, in which the magnetic fields before the collapse, are uniform and parallel to the rotational axis of the star and the magnitudes of the fields, B0, are 1010 G or 1012 G. We follow the evolution of chemical composition up to about 4000 nuclides inside the jets from the collapse phase to the ejection phase through the jet generation phase with use of a large nuclear reaction network. We find that the r-process successfully operates in the jets from the collapsar of B0 = 1012 G, so that U and Th are synthesized abundantly. Abundance pattern inside the jets is similar to that of r-elements in the solar system. Furthermore, we find that p-nuclei are produced without seed nuclei: not only light p-nuclei, such as 74Se, 78Kr, 84Sr, and 92Mo, but also heavy p-nuclei, 113In, 115Sn, and 138La, can be abundantly synthesized in the jets. The amounts of p-nuclei in the ejecta are much greater than those in core-collapse supernovae (SNe). In particular, 92Mo, 113In, 115Sn, and 138La deficient in the SNe, are significantly produced in the ejecta. On the other hand, in the jets from the collapsar of B0 = 1010 G, the r-process cannot operate and 56Ni, 28Si, 32S, and 4He are abundantly synthesized in the jets, as in ejecta from inner layers of Type II supernovae. An amount of 56Ni is much smaller than that from SN 1987A.