前面有篇文章是從本地讀取Excel文件由桌,那樣做感覺太麻煩了捌斧,今天換了一種方法:上傳Excel文件并導(dǎo)出到數(shù)據(jù)庫丁存,多的不說觉增,上代碼
技術(shù)架構(gòu):SpringBoot+Mybatis
數(shù)據(jù)庫:Mysql
導(dǎo)入依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
domain
實體類的字段根據(jù)自己Excel的表頭來決定,數(shù)據(jù)庫的表字段最好和實體類一一對應(yīng)
import lombok.Data;
// Excel實體類
@Data
public class Detailed {
public Long id; // 主鍵
public String auditDate; // 審核日期
public Integer sceneId; // 場景id
public String sceneName; // 場景名稱
public String entityid; // entityid
public Long housingResourcesId; // 房源id/實拍視頻id
public Integer cityid; // cityid/城市id
public String firstInstanceOA; // 初審oa/一審審核人oa
public String firstInstance; // 初審方/一審核員部門名稱
public String preliminaryResults; // 初審結(jié)果/一審審核結(jié)果
public String reasonsForFirstInstance; // 初審原因/一審審核原因
public String timeOfInitialExaminationAndStorage; // 初審入庫時間
public String initialAuditTime; // 初審審核時間
public Double shenheshichang; // shenheshichang
public String timeoutStatus; // 超時狀態(tài)
public String qualityInspectionResults; // 質(zhì)檢結(jié)果/質(zhì)檢審核結(jié)果
public String qualityInspectionReasons; // 質(zhì)檢原因/質(zhì)檢審核原因
public String qualificationStatus; // 質(zhì)檢合格原因
}
utils
獲取Excel表格中的數(shù)據(jù),其中// 獲取目標(biāo)單元格的值并存進對象中
根據(jù)自己的實體類進行編寫
import com.iyunfish.tongcheng.domain.Detailed;
import org.apache.poi.ss.usermodel.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtils {
public static List<Detailed> excelToShopIdList(InputStream inputStream) throws IOException {
Workbook workbook = WorkbookFactory.create(inputStream);
inputStream.close();
// 在工作簿獲取目標(biāo)工作表
Sheet sheet = workbook.getSheetAt(0);
// 獲取到最后一行
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
// 該集合用來儲存行對象
ArrayList<Detailed> detaileds = new ArrayList<>();
// 遍歷整張表鲸阔,從第二行開始赞警,第一行的表頭不要,循環(huán)次數(shù)不大于最后一行的值
for (int i = 1; i < physicalNumberOfRows; i++) {
// 該對象用來儲存行數(shù)據(jù)
Detailed detailed = new Detailed();
// 獲取當(dāng)前行數(shù)據(jù)
Row row = sheet.getRow(i);
// 獲取目標(biāo)單元格的值并存進對象中
detailed.setAuditDate(row.getCell(0).getStringCellValue());
detailed.setSceneId(Integer.valueOf(row.getCell(1).getStringCellValue()));
detailed.setSceneName(row.getCell(2).getStringCellValue());
detailed.setEntityid(row.getCell(3).getStringCellValue());
detailed.setHousingResourcesId(Long.valueOf(row.getCell(4).getStringCellValue()));
detailed.setCityid(Integer.valueOf(row.getCell(5).getStringCellValue()));
detailed.setFirstInstanceOA(row.getCell(6).getStringCellValue());
detailed.setFirstInstance(row.getCell(7).getStringCellValue());
detailed.setPreliminaryResults(row.getCell(8).getStringCellValue());
detailed.setReasonsForFirstInstance(row.getCell(9).getStringCellValue());
detailed.setTimeOfInitialExaminationAndStorage(row.getCell(10).getStringCellValue());
detailed.setInitialAuditTime(row.getCell(11).getStringCellValue());
detailed.setShenheshichang(Double.valueOf(row.getCell(12).getStringCellValue()));
detailed.setTimeoutStatus(row.getCell(13).getStringCellValue());
detailed.setQualityInspectionResults(row.getCell(14).getStringCellValue());
detailed.setQualityInspectionReasons(row.getCell(15).getStringCellValue());
detailed.setQualificationStatus(row.getCell(16).getStringCellValue());
// 把對象放到集合里
detaileds.add(detailed);
System.out.println("獲取到的當(dāng)前行數(shù)據(jù):" + detailed);
}
return detaileds;
}
}
Controller
數(shù)據(jù)庫表的id點上自增秩彤,這樣添加的時候就不用再添加id了
import com.iyunfish.tongcheng.domain.Detailed;
import com.iyunfish.tongcheng.service.ReadService;
import com.iyunfish.tongcheng.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@Controller
public class UploadController {
@Autowired
private ReadService readService;
@RequestMapping("/file/upload")
public void pubggupload(@RequestParam("file") MultipartFile file) throws Exception {
String name = file.getOriginalFilename();
if(name.length() < 5 || !name.substring(name.length() - 5).equals(".xlsx")) {
throw new Exception("文件格式錯誤");
}
// 獲取Excel中的數(shù)據(jù)
List<Detailed> detaileds = ExcelUtils.excelToShopIdList(file.getInputStream());
// 向數(shù)據(jù)庫遍歷添加數(shù)據(jù)庫
for (int i = 0; i < detaileds.size(); i++) {
// 獲取行信息
Detailed detailed = detaileds.get(i);
// 先根據(jù)eneityid查詢數(shù)據(jù)庫里有沒有一樣的,沒有就進行添加
List<Long> longs = readService.queryDetailedByEneityid(detailed.getEntityid());
if (longs.size() <= 0) {
readService.addDetailed(detailed);
} else {
System.out.println("error:該條信息已存在 message:" + detailed);
}
}
}
}
邏輯層接口和實現(xiàn)類沒有邏輯淤堵,直接到控制層的xml文件
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.iyunfish.tongcheng.mapper.ReadMapper">
<select id="queryDetailedByEneityid" parameterType="String" resultType="com.itZhao.domain.Detailed">
SELECT
id
FROM
detailed
WHERE
entityid = #{entityid}
</select>
<insert id="addDetailed" parameterType="com.iyunfish.tongcheng.domain.Detailed">
INSERT INTO `tongcheng`.`detailed` (
`audit_date`,
`scene_id`,
`scene_name`,
`entityid`,
`housing_resources_id`,
`cityid`,
`firstInstanceOA`,
`firstInstance`,
`preliminary_results`,
`reasons_for_firstInstance`,
`time_ofInitial_examination_and_storage`,
`initial_audit_time`,
`shenheshichang`,
`timeout_status`,
`quality_inspection_results`,
`quality_inspection_reasons`,
`qualification_status`
)
VALUES
(#{read.auditDate}, #{read.sceneId}, #{read.sceneName}, #{read.entityid}, #{read.housingResourcesId}, #{read.cityid}, #{read.firstInstanceOA}, #{read.firstInstance}, #{read.preliminaryResults}, #{read.reasonsForFirstInstance}, #{read.timeOfInitialExaminationAndStorage}, #{read.initialAuditTime}, #{read.shenheshichang}, #{read.timeoutStatus}, #{read.qualityInspectionResults}, #{read.qualityInspectionReasons}, #{read.qualificationStatus})
</insert>
</mapper>
到此代碼就寫完了贱勃,有不懂的或報錯解決不了的評論留言,看完啦點個贊吧