The answer to this question is quite complicated, there is a whole website devoted to this subject here.
Shrinking things down as short as possible, the idea of seven day periods within a calendar based primarily on months came from the West into China, possibly more than once. During the Tang dynasty, it was taken up in China for use in astrology. For the percentage of the population who were not astrologers, it was not an important or widely known idea, and by the Ming dynasty, it was simply an odd factoid in astrological treatises.
According to the same website, the Japanese got the idea of the seven day period through Buddhist sources in the early 800s. Both the Chinese and Japanese thus got this idea, either directly or indirectly, through Western Buddhist or Manichaean sources, which identified the days with the sun, moon, and five planets, the 'seven luminaries' 七曜. Unlike China, the astrological use remained popular in Japan, but was limited to astrology until Japan decided to adopt the Western system of weeks as part of its calendar reform in 1876.
The seven-luminaries names of the seven days thus continued to be used in Japan because they had a continuous history. In China, the whole thing was forgotten until they were reminded of the idea when Westerners starting setting up extra-territorial concession across China in the second half of the 1800s. The concessions naturally used weeks to regulate both work schedules and religious worship, particularly the Sabbath.
It seems clear that libai 禮拜 'worship' as the name for a week originated some time in this period; there is a very interesting article by 張書華 called 西制東漸：試探“星期制”於清末上海之傳布 which describes the spread of the week throughout China, a spread in which Shanghai played a leading role.
Yuan Chia-ku 袁嘉谷 was the Qing official who was supposedly responsible for coining the name xingqi 星期 'a stellar period' for the week in the last years of the Qing dynasty, but it was not until the Republic of China was declared that the Western calendar was adopted and the 'week' became a part of Chinese time-keeping. Even then, the use of libai remained much more common than xingqi, until the dawn of the People's Republic in 1949.
I have to say that Yuan's objection to the use of the Tang era names for the days of the week doesn't seem that unreasonable to me. If we had to go through a major calendrical reform (heaven forfend), would you like to have a new system of days set up named after the Buddhas of the seven heavens?