TableStudy.vue 16.3 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<!--
 * 表格学习
  -->
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useMessage } from 'src/common/hooks';
import { ComTitlePage, ComUpLoader } from 'src/components';
import ICON from 'src/config/icons';

import { read, utils, writeFile } from 'xlsx';
// var XLSX = require('xlsx');

hucy's avatar
hucy committed
14 15
import lang from '../../lang';

hucy's avatar
hucy committed
16
const { t } = useI18n({ messages: lang });
hucy's avatar
hucy committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
const { warn } = useMessage();

const excelTypeList = ['xlsx', 'xls'];

const columns: any[] = [
  {
    name: 'name',
    field: 'name',
    label: '姓名',
    align: 'center',
  },
  {
    name: 'age',
    field: 'age',
    label: '年龄',
    align: 'center',
  },
  {
    name: 'sex',
    field: 'sex',
    label: '性别',
    align: 'center',
  },
  {
    name: 'salary',
    field: 'salary',
    label: '工资',
    align: 'center',
  },
  {
    name: 'hobby',
    field: 'hobby',
    label: '爱好',
    align: 'center',
  },
];

hucy's avatar
hucy committed
54
console.log('翻译', t('No file selected'));
hucy's avatar
hucy committed
55 56 57

const hasUploadXlsxMsg = ref(false);
const uploadXlsxMsg = ref(t('No file selected'));
hucy's avatar
hucy committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

const state = reactive({
  rows: [
    {
      name: '张三',
      sex: '男',
      age: 18,
      hobby: '摸鱼',
    },
    {
      sex: '男',
      age: 18,
      name: '孙火旺 名字很长很长名字很长很长',
    },
    {
      salary: '5000.00',
      name: '小美',
      age: 20,
      sex: '女',
    },
    {
      name: '小李',
      age: 22,
      salary: '6000.00',
      sex: '男',
    },
  ],
  viewTableHtml: null as any,
});

function onXport() {
  const table = document.getElementById('TableToExport');
  const wb = utils.table_to_book(table);
  writeFile(wb, '库存表.xlsx');
}

function uploadXlsx() {
  let _input: HTMLInputElement = document.createElement('input'); // 创建一个隐藏的input
  _input.setAttribute('type', 'file');
  _input.setAttribute(
    'accept',
    'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  );
hucy's avatar
hucy committed
101
  // _input.setAttribute('accept', '');
hucy's avatar
hucy committed
102 103 104 105 106 107 108

  _input.onchange = async function (e: any) {
    const file = e.target.files[0];
    const fileTypeArr = file.name.split('.');
    const fileType = fileTypeArr[fileTypeArr.length - 1];
    // console.log('上传文件 >>>>', fileType, file);

hucy's avatar
hucy committed
109 110 111 112 113 114
    if (excelTypeList.includes(fileType)) {
      let unit: string; // 单位
      let size: number | string = 0;
      if (file.size < 1000) {
        unit = 'B';
        size = file.size.toFixed(3);
hucy's avatar
hucy committed
115
      } else {
hucy's avatar
hucy committed
116 117 118 119
        const KB_size = file.size * 0.0009765625;
        if (KB_size < 1000) {
          unit = 'KB';
          size = KB_size.toFixed(3);
hucy's avatar
hucy committed
120
        } else {
hucy's avatar
hucy committed
121 122 123 124
          const MB_size = KB_size * 0.0009765625;
          if (MB_size < 1000) {
            unit = 'MB';
            size = MB_size.toFixed(2);
hucy's avatar
hucy committed
125
          } else {
hucy's avatar
hucy committed
126 127 128 129 130 131 132 133 134 135
            const GB_size = MB_size * 0.0009765625;
            if (GB_size <= 2) {
              unit = 'GB';
              size = GB_size.toFixed(2);
            } else {
              const msg = t('Select files smaller than 2GB');
              warn(msg);
              console.warn(msg);
              return;
            }
hucy's avatar
hucy committed
136 137 138 139 140
          }
        }
      }

      uploadXlsxMsg.value = `${file.name}&nbsp;&nbsp;${size}${unit}`;
hucy's avatar
hucy committed
141
      hasUploadXlsxMsg.value = true;
hucy's avatar
hucy committed
142 143 144 145 146 147
      const data = await file.arrayBuffer();
      const wb = read(data);
      const ws = wb.Sheets[wb.SheetNames[0]];
      const asd = utils.sheet_to_html(ws, { id: 'tabeller' });
      state.viewTableHtml = asd;
    } else {
hucy's avatar
hucy committed
148
      const msg = t('File format error', { type: fileType });
hucy's avatar
hucy committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      warn(msg);
      console.warn(msg);
    }
  };
  _input.click();
}

function uploadOk(val: any) {
  console.log('uploadOk >>>>', val);
}

function exportTableData() {
  // https://docs.sheetjs.com/docs/getting-started/example/

  // 生成一个工作表
  const worksheet = utils.json_to_sheet(state.rows);

  // 创建一个新工作簿
  const workbook = utils.book_new();
  // 把工作表添加到工作簿,这个新的工作表被命名为 "StudentInfo"
  utils.book_append_sheet(workbook, worksheet, 'StudentInfo');

  // 更改标题行
  // 默认标题行是row里面对象数据第一个出现的键名
  // { origin: "A1" } 从单元格A1开始写入文本
  utils.sheet_add_aoa(worksheet, [['姓名', '性别', '年龄', '爱好', '工资']], {
    origin: 'A1',
  });

  // 有些名称比默认列宽长,可以通过设置 "!cols" 来设置列宽
  worksheet['!cols'] = [{ wch: 40 }]; // set column A width to 40 characters
  // 计算最大宽度
  // const max_width = state.rows.reduce((w, r) => Math.max(w, r.name.length), 10);
  // worksheet['!cols'] = [{ wch: max_width }];

  // 插入新的数据行
  // const ws = workbook.Sheets['StudentInfo'];
  // XLSX.utils.sheet_add_aoa(
  //   worksheet,
  //   [
  //     ['郝美丽', '女', '40', '购物', '1000000'],
  //     ['甄漂亮', '女', '30', '看电视', '2000000'],
  //   ],
  //   {
  //     origin: -1,
  //   }
  // );

  // 创建一个 XLSX 文件并尝试保存为 学生信息.xlsx
  writeFile(workbook, '学生信息.xlsx', { compression: true });
}
</script>

<template>
  <div>
    <com-title-page title="table学习" />

    <div class="my-table q-mt-md q-gutter-sm">
      <div>
        <q-btn
hucy's avatar
hucy committed
209
          :label="t('Export table data')"
hucy's avatar
hucy committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
          square
          push
          :icon="ICON.export"
          style="background: #4aacc5; color: white"
          @click="exportTableData"
        />
      </div>

      <div>
        <q-table :rows="state.rows" :columns="columns" row-key="name" />
      </div>
    </div>
    <div class="upload-box">
      <div style="width: 400px; height: 232px" class="q-my-md">
hucy's avatar
hucy committed
224
        <com-up-loader bg-color="#FAD4D4" dark multiple @on-ok="uploadOk" />
hucy's avatar
hucy committed
225 226
      </div>
      <div style="width: 400px; height: 232px" class="q-my-md">
hucy's avatar
hucy committed
227 228 229 230 231 232
        <com-up-loader
          bg-color="#A4BE7B"
          multiple
          accept="image/*"
          @on-ok="uploadOk"
        />
hucy's avatar
hucy committed
233 234 235 236 237
      </div>
      <div>
        <div class="upload-xlsx-box">
          <div>
            <q-btn
hucy's avatar
hucy committed
238
              :label="t('Upload files')"
hucy's avatar
hucy committed
239 240 241 242 243 244 245 246
              square
              push
              :icon="ICON.upload"
              style="background: #4aacc5; color: white"
              @click="uploadXlsx"
            />
          </div>
          <div>
hucy's avatar
hucy committed
247 248 249
            <q-chip :ripple="false" square>
              <span v-if="!hasUploadXlsxMsg">{{ t('No file selected') }}</span>
              <span v-else v-html="uploadXlsxMsg"></span>
hucy's avatar
hucy committed
250 251 252 253 254 255 256 257 258 259 260 261 262
            </q-chip>
          </div>
        </div>
      </div>
      <div class="view-table-box">
        <div class="view-table-inner">
          <div v-html="state.viewTableHtml" class="table-view"></div>
        </div>
      </div>
    </div>
    <div class="table-box q-mt-md q-pb-md">
      <div class="q-mb-sm">
        <q-btn
hucy's avatar
hucy committed
263
          :label="t('Export inventory table')"
hucy's avatar
hucy committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
          square
          push
          :icon="ICON.export"
          style="background: #4aacc5; color: white"
          @click="onXport"
        />
      </div>
      <div>
        <table id="TableToExport">
          <caption>
            库存表
          </caption>
          <thead>
            <tr>
              <th rowspan="3">货号</th>
              <th rowspan="3">颜色</th>
              <th rowspan="3">码号</th>
              <th rowspan="3">上月库存数量</th>
              <th rowspan="3">单价</th>
              <th rowspan="3">上月库存金额</th>
              <th>4月采购</th>
              <th colspan="2">4月销售</th>
              <th rowspan="3">本月库存数量</th>
              <th rowspan="3">单价</th>
              <th rowspan="3">本月库存金额</th>
            </tr>
            <tr>
              <th>采购合计</th>
              <th>4月16日</th>
              <th>5月20日</th>
            </tr>
            <tr>
              <th>数量合计</th>
              <th>数量</th>
              <th>数量</th>
            </tr>
          </thead>
          <tfoot>
            <tr>
              <td colspan="12">商品增加,直接“复制” “粘贴”即可</td>
            </tr>
            <tr>
              <td colspan="12">
                每月月初复制粘贴“上月月末库存” 数据作为 “本月月初库存” 数据
              </td>
            </tr>
          </tfoot>
          <tbody>
            <tr>
              <td rowspan="6">&nbsp;</td>
              <td rowspan="3">&nbsp;</td>
              <td>&nbsp;</td>
              <td>2</td>
              <td>2.00</td>
              <td>4.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>2</td>
              <td>2.00</td>
              <td>4.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>2</td>
              <td>2.00</td>
              <td>4.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>2</td>
              <td>2.00</td>
              <td>4.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>2</td>
              <td>2.00</td>
              <td>4.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>2</td>
              <td>2.00</td>
              <td>4.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>6</td>
              <td>&nbsp;</td>
              <td>12</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>6</td>
              <td>0.00</td>
              <td>12.00</td>
            </tr>
            <tr>
              <td rowspan="5">&nbsp;</td>
              <td rowspan="3">&nbsp;</td>
              <td>&nbsp;</td>
              <td>3</td>
              <td>3.00</td>
              <td>9.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>3</td>
              <td>3.00</td>
              <td>9.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>3</td>
              <td>3.00</td>
              <td>9.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>3</td>
              <td>3.00</td>
              <td>9.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>3</td>
              <td>3.00</td>
              <td>9.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>3</td>
              <td>3.00</td>
              <td>9.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>9</td>
              <td>&nbsp;</td>
              <td>27</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>9</td>
              <td>0.00</td>
              <td>27.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>合计</td>
              <td>&nbsp;</td>
              <td>15</td>
              <td>&nbsp;</td>
              <td>39.00</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>15</td>
              <td>&nbsp;</td>
              <td>39.00</td>
            </tr>
            <tr>
              <td rowspan="6">&nbsp;</td>
              <td rowspan="3">&nbsp;</td>
              <td>&nbsp;</td>
              <td>4</td>
              <td>4.00</td>
              <td>16.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>4</td>
              <td>4.00</td>
              <td>16.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>4</td>
              <td>4.00</td>
              <td>16.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>4</td>
              <td>4.00</td>
              <td>16.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>4</td>
              <td>4.00</td>
              <td>16.00</td>
              <td>0</td>
              <td>&nbsp;</td>
              <td>0</td>
              <td>4</td>
              <td>4.00</td>
              <td>16.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>12</td>
              <td>&nbsp;</td>
              <td>48.00</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>12</td>
              <td>&nbsp;</td>
              <td>48.00</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>合计</td>
              <td>&nbsp;</td>
              <td>12</td>
              <td>&nbsp;</td>
              <td>48.00</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>12</td>
              <td>&nbsp;</td>
              <td>48.00</td>
            </tr>
            <tr style="background-color: #dbeef4">
              <td>总计</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>27</td>
              <td>&nbsp;</td>
              <td>87.00</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>27</td>
              <td>&nbsp;</td>
              <td>87.00</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.table-box {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#TableToExport {
  text-align: center; /*文本居中*/
  border-collapse: collapse; /*表格的边框合并,如果相邻,则共用一个边框*/
  border-spacing: 0; /*设置行与单元格边框的间距。当表格边框独立(即border-collapse:separate;)此属性才起作用*/
  border: 1px solid #0d3f67;
  color: #000;
  caption {
    font-size: 28px;
    letter-spacing: 15px;
    padding: 10px 0;
    color: white;
    background-color: #4aacc5;
    border-bottom: none;
  }
  thead {
    tr {
      background-color: #dbeef4;
    }
  }
  th {
    padding: 6px 4px;
    font-size: 16px;
    min-width: 120px;
    border: 2px solid #4aacc5;
  }
  td {
    font-size: 16px;
    padding: 6px 4px;
    border: 2px solid #4aacc5;
  }
  tfoot > tr > td {
    padding: 0;
    font-size: 18px;
  }
}

.view-table-box {
  margin-top: $padding-sm;
  box-sizing: border-box;
  background-color: #dbeef4;
  max-width: 1440px;
  padding: 16px;
  overflow: hidden;
  .view-table-inner {
    overflow: auto;
  }
}

// 上传文件
.upload-xlsx-box {
  display: flex;
  flex-flow: row nowrap;
}
</style>