ImportArchives.java 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.gz;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.poi.excel.ExcelReader;
  4. import cn.hutool.poi.excel.ExcelUtil;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.gz.core.exception.BusinessException;
  7. import com.gz.dto.archive.ArchiveDTO;
  8. import com.gz.mapper.archive.ArchiveMapper;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.poi.util.LittleEndianOutputStream;
  11. import org.junit.jupiter.api.Test;
  12. import org.omg.CORBA.IRObject;
  13. import org.springframework.boot.test.context.SpringBootTest;
  14. import org.springframework.transaction.annotation.Transactional;
  15. import sun.rmi.runtime.Log;
  16. import javax.annotation.Resource;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * @author LiuchangLan
  21. * @date 2021/2/25 13:04
  22. */
  23. @Slf4j
  24. @SpringBootTest
  25. class ImportArchives {
  26. private String excelPath = "C:\\Users\\84731\\Desktop\\a.xlsx";
  27. @Resource
  28. private ArchiveMapper mapper;
  29. @Test
  30. // @Transactional
  31. public void test() {
  32. ExcelReader reader = ExcelUtil.getReader(FileUtil.file(excelPath), 0);
  33. List<List<Object>> rows = reader.read();
  34. for (int i = 1; i < rows.size(); i++) {
  35. try {
  36. List<Object> row = rows.get(i);
  37. ArchiveDTO dto = new ArchiveDTO();
  38. dto.setDagdm(this.getExcelCellStringValue(row, 0));
  39. dto.setDh(this.getExcelCellStringValue(row, 1));
  40. dto.setQzh(this.getExcelCellStringValue(row, 2));
  41. dto.setGdnd(this.getExcelCellStringValue(row, 3));
  42. dto.setMj(this.getExcelCellStringValue(row, 4));
  43. dto.setBgqx(this.getExcelCellStringValue(row, 5));
  44. // TODO: 2021/2/25 机构代号 6 馆编号 7
  45. dto.setZtsl(this.getExcelCellIntegerValue(row, 8));
  46. dto.setWjbh(this.getExcelCellStringValue(row, 9));
  47. dto.setWjxcsj(this.getExcelCellStringValue(row, 10));
  48. dto.setZrz(this.getExcelCellStringValue(row, 11));
  49. dto.setTm(this.getExcelCellStringValue(row, 12));
  50. dto.setBz(this.getExcelCellStringValue(row, 13));
  51. dto.setZbbm(this.getExcelCellStringValue(row, 14));
  52. dto.setXbbm(this.getExcelCellStringValue(row, 15));
  53. dto.setKzf(this.getExcelCellStringValue(row, 16));
  54. dto.setDzwdh(this.getExcelCellStringValue(row, 17));
  55. dto.setSwh(this.getExcelCellStringValue(row, 18));
  56. dto.setGb(this.getExcelCellStringValue(row, 19));
  57. dto.setWz(this.getExcelCellStringValue(row, 20));
  58. dto.setZtgg(this.getExcelCellStringValue(row, 21));
  59. dto.setZtlx(this.getExcelCellStringValue(row, 22));
  60. dto.setZtdw(this.getExcelCellStringValue(row, 23));
  61. dto.setZzjgdm(this.getExcelCellStringValue(row, 24));
  62. dto.setFlh(this.getExcelCellStringValue(row, 25));
  63. dto.setZtc(this.getExcelCellStringValue(row, 26));
  64. dto.setQwbs(this.getExcelCellStringValue(row, 27));
  65. System.out.println(dto);
  66. mapper.insertSelective(dto);
  67. } catch (Exception e) {
  68. new BusinessException(500, String.format("导入出错,错误位置:{}行,错误原因{}", (i + 1), e.getMessage()));
  69. }
  70. }
  71. log.info("成功添加{}行数据", rows.size());
  72. }
  73. private String getExcelCellStringValue(List<Object> row, int index) {
  74. if (index >= row.size()) {
  75. return null;
  76. }
  77. return String.valueOf(row.get(index));
  78. }
  79. private Integer getExcelCellIntegerValue(List<Object> row, int index) {
  80. return Integer.valueOf(getExcelCellStringValue(row, index));
  81. }
  82. }