index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { queryRecordSheet } from '@/services/boom';
  2. import LuckySheet from '../Detail/LuckySheet';
  3. import react, { useRef } from 'react';
  4. import LuckyExcel from 'luckyexcel';
  5. import { Button, message } from 'antd';
  6. const TEMPLATE_URL =
  7. 'https://water-service-test.oss-cn-hangzhou.aliyuncs.com/bom/635/%E5%90%88%E5%90%8C%E6%96%87%E4%BB%B6/%E6%8A%95%E6%A0%87%E6%A8%A1%E6%9D%BF.xlsx';
  8. const TEMPLATE_URL2 = 'https://water-service-test.oss-cn-hangzhou.aliyuncs.com/public/bom/psr.xlsx';
  9. function Index(props) {
  10. const { versionId = 2418 } = props;
  11. const sheetRef = useRef();
  12. const luckysheetRef = useRef();
  13. const uploadExcelByUrl = type => {
  14. LuckyExcel.transformExcelToLuckyByUrl(
  15. type == 1 ? TEMPLATE_URL : TEMPLATE_URL2,
  16. '模板.xlsx',
  17. async (exportJson, luckysheetfile) => {
  18. let record = await getExcel(versionId);
  19. console.log(record);
  20. luckysheetRef.current.destroy();
  21. luckysheetRef.current.create({
  22. lang: 'zh',
  23. showinfobar: false,
  24. showstatisticBar: false,
  25. data: [...exportJson.sheets, ...record],
  26. hook: {
  27. cellMousedown: (cell, position, sheet) => {
  28. console.log(cell, position, sheet);
  29. },
  30. cellUpdated: () => {
  31. luckysheetRef.current.refreshFormula();
  32. },
  33. },
  34. });
  35. }
  36. );
  37. };
  38. const handleLoad = () => {
  39. let contentWindow = sheetRef.current.contentWindow;
  40. luckysheetRef.current = contentWindow.luckysheet;
  41. };
  42. return (
  43. <div>
  44. <Button type="primary" onClick={() => uploadExcelByUrl(1)}>
  45. 导入投标投标
  46. </Button>
  47. <Button type="primary" onClick={() => uploadExcelByUrl(2)}>
  48. 导入合同模板
  49. </Button>
  50. <iframe
  51. style={{
  52. width: '100%',
  53. height: '80vh',
  54. }}
  55. ref={sheetRef}
  56. onLoad={handleLoad}
  57. src="/luckysheet.html"
  58. ></iframe>
  59. </div>
  60. );
  61. }
  62. async function getExcel(gridKey) {
  63. var formData = new FormData();
  64. formData.append('gridKey', gridKey);
  65. let res = await fetch(
  66. `/api/v1/purchase/record/sheet?gridKey=${gridKey}&JWT-TOKEN=${getToken()}`,
  67. {
  68. method: 'POST',
  69. body: formData,
  70. }
  71. ).then(response => response.text());
  72. return JSON.parse(JSON.parse(res));
  73. }
  74. export default Index;