We conducted a population-based study of 627 patients with biliary tract cancers (368 of gallbladder, 191 bile duct, and 68 ampulla of Vater), 1037 with biliary stones, and 959 healthy controls randomly selected from the Shanghai population, all personally interviewed. Gallstone status was based on information from self-reports, imaging procedures, surgical notes, and medical records. Among controls, a transabdominal ultrasound was performed to detect asymptomatic gallstones. Gallstones removed from cancer cases and gallstone patients were classified by size, weight, colour, pattern, and content of cholesterol, bilirubin, and bile acids. Of the cancer patients, 69% had gallstones compared with 23% of the population controls. Compared with subjects without gallstones, odds ratios associated with gallstones were 23.8 (95% confidence interval (CI), 17.0-33.4), 8.0 (95% CI 5.6-11.4), and 4.2 (95% CI 2.5-7.0) for cancers of the gallbladder, extrahepatic bile ducts, and ampulla of Vater, respectively, persisting when restricted to those with gallstones at least 10 years prior to cancer. Biliary cancer risks were higher among subjects with both gallstones and self-reported cholecystitis, particularly for gallbladder cancer (OR=34.3, 95% CI 19.9-59.2). Subjects with bile duct cancer were more likely to have pigment stones, and with gallbladder cancer to have cholesterol stones (P<0.001). Gallstone weight in gallbladder cancer was significantly higher than in gallstone patients (4.9 vs 2.8 grams; P=0.001). We estimate that in Shanghai 80% (95% CI 75-84%), 59% (56-61%), and 41% (29-59%) of gallbladder, bile duct, and ampulla of Vater cancers, respectively, could be attributed to gallstones.