最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格

 更新時(shí)間:2022年08月28日 12:01:40   作者:胡蘿卜★  
這篇文章主要為大家詳細(xì)介紹了Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格的具體代碼,供大家參考,具體內(nèi)容如下

一、Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格demo

1.引入依賴

<dependency>
? ? ? <groupId>org.apache.poi</groupId>
? ? ? <artifactId>poi-ooxml</artifactId>
? ? ? ?<version>4.1.2</version>
</dependency>

2.導(dǎo)入demo

2.1 controller層

/**
? ? ?* Excel導(dǎo)入?
? ? ?*/
? ? @PostMapping("/import")
? ? public Result userImport2(@RequestParam("file") MultipartFile file) throws Exception{
? ? ? ? Result result=userService.userImportExcel(file);
? ? ? ? return result;
? ? }

2.2 service實(shí)現(xiàn)類層

public Result userImportExcel(MultipartFile file){
? ? try {
? ? ? ? InputStream inputStream = file.getInputStream();
? ? ? ? XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
? ? ? ? //獲取表單sheet 第一個(gè)
? ? ? ? XSSFSheet sheetAt = sheets.getSheetAt(0);
? ? ? ? //獲取第一行
? ? ? ? int firstRowNum = sheetAt.getFirstRowNum();
? ? ? ? //最后一行
? ? ? ? int lastRowNum = sheetAt.getLastRowNum();
? ? ? ? //存入數(shù)據(jù)集合
? ? ? ? List<User> users=new ArrayList<>();
? ? ? ? //遍歷數(shù)據(jù)
? ? ? ? for(int i=firstRowNum+1;i<lastRowNum+1;i++){
? ? ? ? ? ? XSSFRow row = sheetAt.getRow(i);
? ? ? ? ? ? if(row!=null){
? ? ? ? ? ? ? ?/* //獲取第一行的第一列
? ? ? ? ? ? ? ? int firstCellNum = row.getFirstCellNum();
? ? ? ? ? ? ? ? //獲取第一行的最后列
? ? ? ? ? ? ? ? short lastCellNum = row.getLastCellNum();
? ? ? ? ? ? ? ? for (int j=firstCellNum;j<lastCellNum+1;j++){
? ? ? ? ? ? ? ? ? ? //放入集合中需要可以用這種方法
? ? ? ? ? ? ? ? ? ? String cellValue = getValue(row.getCell(firstCellNum));
? ? ? ? ? ? ? ? }*/
? ? ? ? ? ? ? ? //這里我就直接賦值
? ? ? ? ? ? ? ? User user = new User();
? ? ? ? ? ? ? ? user.setUname(row.getCell(0).getStringCellValue());
? ? ? ? ? ? ? ? user.setUpassword(row.getCell(1).getStringCellValue());
? ? ? ? ? ? ? ? user.setUsex(row.getCell(2).getStringCellValue());
? ? ? ? ? ? ? ? user.setRole(row.getCell(3).getStringCellValue());
? ? ? ? ? ? ? ? user.setUlove((int) row.getCell(4).getNumericCellValue());
? ? ? ? ? ? ? ? user.setUphoto(row.getCell(5).getStringCellValue());
? ? ? ? ? ? ? ? user.setUaddress(row.getCell(6).getStringCellValue());
? ? ? ? ? ? ? ? users.add(user);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //保存數(shù)據(jù)
? ? ? ? saveBatch(users);
? ? ? ? return Result.success();
? ? }catch (Exception e){
? ? ? ? e.printStackTrace();
? ? ? ? log.info("error:{}",e);
? ? }

? ? return Result.error("300","導(dǎo)入失敗");
}

/**
?* 判斷值的類型
?*/
public String getValue(HSSFCell cell) {

? ? if(cell==null){
? ? ? ? return "";
? ? }
? ? String cellValue= "";
? ? try {
? ? ? ? DecimalFormat df=new DecimalFormat("0.00");
? ? ? ? if(cell.getCellType()== CellType.NUMERIC){
? ? ? ? ? ? //日期時(shí)間轉(zhuǎn)換
? ? ? ? ? ? if(HSSFDateUtil.isCellDateFormatted(cell)){
? ? ? ? ? ? ? ? cellValue=DateFormatUtils.format(cell.getDateCellValue(),"yyyy-MM-dd");
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? NumberFormat instance = NumberFormat.getInstance();
? ? ? ? ? ? ? ? cellValue=instance.format(cell.getNumericCellValue()).replace(",","");
? ? ? ? ? ? }

? ? ? ? }else if(cell.getCellType() == CellType.STRING){
? ? ? ? ? ? //字符串
? ? ? ? ? ? cellValue=cell.getStringCellValue();
? ? ? ? }else if(cell.getCellType() == CellType.BOOLEAN){
? ? ? ? ? ? //Boolean
? ? ? ? ? ? cellValue= String.valueOf(cell.getBooleanCellValue());
? ? ? ? }else if(cell.getCellType() == CellType.ERROR){
? ? ? ? ? ? //錯(cuò)誤
? ? ? ? }else if(cell.getCellType() == CellType.FORMULA){
? ? ? ? ? ? //轉(zhuǎn)換公式 保留兩位
? ? ? ? ? ? cellValue=df.format(cell.getNumericCellValue());
? ? ? ? }else{
? ? ? ? ? ? cellValue=null;
? ? ? ? }

? ? } catch (Exception e) {
? ? ? ? e.printStackTrace();
? ? ? ? cellValue="-1";
? ? }

? ? return cellValue;
}

3.導(dǎo)出demo

3.1 controller層

/**
?* 導(dǎo)出?
?* @param response
?* @return
?* @throws Exception
?*/
@GetMapping("/export")
public Result userExport2(HttpServletResponse response) throws Exception{
? ? Result result=userService.userExportExcel(response);
? ? return result;
}

3.2 service實(shí)現(xiàn)類

public Result userExportExcel(HttpServletResponse response) {
? ? try {
? ? ? ? //創(chuàng)建excel
? ? ? ? XSSFWorkbook sheets = new XSSFWorkbook();
? ? ? ? //創(chuàng)建行
? ? ? ? XSSFSheet sheet = sheets.createSheet("用戶信息");
? ? ? ? //格式設(shè)置
? ? ? ? XSSFCellStyle cellStyle = sheets.createCellStyle();
? ? ? ? //橫向居中
? ? ? ? cellStyle.setAlignment(HorizontalAlignment.CENTER);
? ? ? ? //創(chuàng)建單元格第一列
? ? ? ? XSSFRow row = sheet.createRow(0);
? ? ? ? //表頭
? ? ? ? this.titleExcel(row,cellStyle);
? ? ? ? //查詢?nèi)康挠脩魯?shù)據(jù) ?mybatis-plus
? ? ? ? List<User> list = list();
? ? ? ? //遍歷設(shè)置值
? ? ? ? for(int i=0;i<list.size();i++){
? ? ? ? ? ? XSSFRow rows = sheet.createRow(i+1);
? ? ? ? ? ? User user=list.get(i);
? ? ? ? ? ? //表格里賦值
? ? ? ? ? ? this.titleExcelValue(user,rows,cellStyle);
? ? ? ? }
? ? ? ? //設(shè)置瀏覽器響應(yīng)格式
? ? ? ? response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
? ? ? ? String filName= URLEncoder.encode("用戶信息","UTF-8");
? ? ? ? response.setHeader("Content-Disposition","attachment;filename="+filName+".xls");

? ? ? ? ServletOutputStream outputStream=response.getOutputStream();
? ? ? ? sheets.write(outputStream);
? ? ? ? outputStream.close();
? ? ? ? sheets.close();
? ? ? ? return Result.success();

? ? }catch (Exception e){
? ? ? ? e.printStackTrace();
? ? ? ? log.info("error:{}",e);
? ? }

? ? return Result.error("300","導(dǎo)出失敗");
}

/**
*表格里賦值
**/
public void titleExcelValue(User user, XSSFRow row,XSSFCellStyle cellStyle) {
? ? XSSFCell cellId = row.createCell(0);
? ? cellId.setCellValue(user.getUid());
? ? cellId.setCellStyle(cellStyle);

? ? XSSFCell cellUserName = row.createCell(1);
? ? cellUserName.setCellValue(user.getUname());
? ? cellUserName.setCellStyle(cellStyle);

? ? XSSFCell cellPassword = row.createCell(2);
? ? cellPassword.setCellValue(user.getUpassword());
? ? cellPassword.setCellStyle(cellStyle);

? ? XSSFCell cellSex = row.createCell(3);
? ? cellSex.setCellValue(user.getUsex());
? ? cellSex.setCellStyle(cellStyle);

? ? XSSFCell cellRole = row.createCell(4);
? ? cellRole.setCellValue(user.getRole());
? ? cellRole.setCellStyle(cellStyle);

? ? XSSFCell cellLoveValue = row.createCell(5);
? ? cellLoveValue.setCellValue(user.getRole());
? ? cellLoveValue.setCellStyle(cellStyle);

? ? XSSFCell cellPhone = row.createCell(6);
? ? cellPhone.setCellValue(user.getUphoto());
? ? cellPhone.setCellStyle(cellStyle);

? ? XSSFCell cellAddress = row.createCell(7);
? ? cellAddress.setCellValue(user.getUaddress());
? ? cellAddress.setCellStyle(cellStyle);


}
/**
?? ?表頭
**/
public void titleExcel(XSSFRow row,XSSFCellStyle cellStyle){

? ? XSSFCell cellId = row.createCell(0);
? ? cellId.setCellValue("用戶ID");
? ? cellId.setCellStyle(cellStyle);

? ? XSSFCell cellUserName = row.createCell(1);
? ? cellUserName.setCellValue("用戶名");
? ? cellUserName.setCellStyle(cellStyle);

? ? XSSFCell cellPassword = row.createCell(2);
? ? cellPassword.setCellValue("密碼");
? ? cellPassword.setCellStyle(cellStyle);

? ? XSSFCell cellSex = row.createCell(3);
? ? cellSex.setCellValue("性別");
? ? cellSex.setCellStyle(cellStyle);

? ? XSSFCell cellRole = row.createCell(4);
? ? cellRole.setCellValue("角色");
? ? cellRole.setCellStyle(cellStyle);

? ? XSSFCell cellLoveValue = row.createCell(5);
? ? cellLoveValue.setCellValue("愛心值");
? ? cellLoveValue.setCellStyle(cellStyle);

? ? XSSFCell cellPhone = row.createCell(6);
? ? cellPhone.setCellValue("電話號(hào)碼");
? ? cellPhone.setCellStyle(cellStyle);

? ? XSSFCell cellAddress = row.createCell(7);
? ? cellAddress.setCellValue("地址");
? ? cellAddress.setCellStyle(cellStyle);

}

二、Hutool工具類封裝方法導(dǎo)出導(dǎo)入Excel

1.引入依賴

把poi封裝到工具類方法里面

<!-- hutool ?-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>cn.hutool</groupId>
? ? ? ? ? ? <artifactId>hutool-all</artifactId>
? ? ? ? ? ? <version>5.7.20</version>
? ? ? ? </dependency>

? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.poi</groupId>
? ? ? ? ? ? <artifactId>poi-ooxml</artifactId>
? ? ? ? ? ? <version>4.1.2</version>
</dependency>

2.導(dǎo)入demo

/**
? ? ?* Excel導(dǎo)入?
*/
@PostMapping("/import")
public Result userImport(@RequestParam("file") MultipartFile file) throws Exception{
? ? ? ? System.out.println(file.toString());
? ? ? ? //InputStream inputStream = multipartFile.getInputStream();
? ? ? ? InputStream inputStream = file.getInputStream();
? ? ? ? ExcelReader reader = ExcelUtil.getReader(inputStream);
? ? ? ? //讀取表的內(nèi)容
? ? ? ? List<List<Object>> list = reader.read(1);
? ? ? ? List<User> users = new ArrayList<>();
? ? ? ? for(List<Object> row : list){
? ? ? ? ? ? User user = new User();
? ? ? ? ? ? user.setUname(row.get(0).toString());
? ? ? ? ? ? user.setUpassword(row.get(1).toString());
? ? ? ? ? ? user.setUsex(row.get(2).toString());
? ? ? ? ? ? user.setRole(row.get(3).toString());
? ? ? ? ? ? user.setUlove(Integer.valueOf(row.get(4).toString()));
? ? ? ? ? ? user.setUphoto(row.get(5).toString());
? ? ? ? ? ? user.setUaddress(row.get(6).toString());
? ? ? ? ? ? users.add(user);
? ? ? ? }
? ? ? ? //批量插入用戶信息 mybatis-plus
? ? ? ? userService.saveBatch(users);
? ? ? ? return Result.success();
? ? }

3.導(dǎo)出demo
 

?/**
? ? ?* Excel導(dǎo)出 方法一
? ? ?*/
? ? @GetMapping("/export")
? ? public Result userExport(HttpServletResponse response) throws Exception{
? ? ? ? //查詢?nèi)康挠脩魯?shù)據(jù)
? ? ? ? List<User> list = userService.list();
? ? ? ? //在內(nèi)存里做操作,保存到瀏覽器
? ? ? ? ExcelWriter writer = ExcelUtil.getWriter(true);
? ? ? ? //自定義標(biāo)題別名
? ? ? ? writer.addHeaderAlias("uname","用戶名");
? ? ? ? writer.addHeaderAlias("upassword","密碼");
? ? ? ? writer.addHeaderAlias("usex","性別");
? ? ? ? writer.addHeaderAlias("role","角色");
? ? ? ? writer.addHeaderAlias("ulove","愛心值");
? ? ? ? writer.addHeaderAlias("uphoto","電話號(hào)碼");
? ? ? ? writer.addHeaderAlias("uaddress","地址");
? ? ? ? //一次性寫出list內(nèi)的對(duì)象的Excel,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題
? ? ? ? writer.write(list,true);
? ? ? ? //設(shè)置瀏覽器響應(yīng)格式
? ? ? ? response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
? ? ? ? String filName= URLEncoder.encode("用戶信息","UTF-8");
? ? ? ? response.setHeader("Content-Disposition","attachment;filename="+filName+".xls");

? ? ? ? ServletOutputStream outputStream=response.getOutputStream();
? ? ? ? writer.flush(outputStream,true);
? ? ? ? outputStream.close();
? ? ? ? writer.close();
? ? ? ? return Result.success();
? ? }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合Redis的示例

    SpringBoot整合Redis的示例

    這篇文章主要介紹了SpringBoot整合Redis的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-10-10
  • idea新建maven項(xiàng)目沒有src目錄的操作方法

    idea新建maven項(xiàng)目沒有src目錄的操作方法

    這篇文章主要介紹了idea新建maven項(xiàng)目沒有src目錄的兩種操作方法,需要的朋友可以參考下
    2018-03-03
  • SSM框架實(shí)現(xiàn)分頁和搜索分頁的示例代碼

    SSM框架實(shí)現(xiàn)分頁和搜索分頁的示例代碼

    本篇文章主要介紹了SSM框架實(shí)現(xiàn)分頁和搜索分頁的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Java中內(nèi)存溢出和內(nèi)存泄漏如何解決

    Java中內(nèi)存溢出和內(nèi)存泄漏如何解決

    ?內(nèi)存溢出?和?內(nèi)存泄漏?是兩種常見的內(nèi)存管理問題,它們都會(huì)對(duì)程序的性能產(chǎn)生負(fù)面影響,本文主要介紹了Java中的內(nèi)存溢出和內(nèi)存泄漏問題解決,感興趣的可以了解一下
    2024-12-12
  • Java?IO之流的分類詳解

    Java?IO之流的分類詳解

    這篇文章主要為大家介紹了Java?IO之流的分類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Java @Async注解導(dǎo)致spring啟動(dòng)失敗解決方案詳解

    Java @Async注解導(dǎo)致spring啟動(dòng)失敗解決方案詳解

    這篇文章主要介紹了Java @Async注解導(dǎo)致spring啟動(dòng)失敗解決方案詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java中comparator接口和Comparable接口的比較解析

    Java中comparator接口和Comparable接口的比較解析

    這篇文章主要介紹了Java中comparator接口和Comparable接口的比較解析,Java提供了一個(gè)用于比較的接口Comparator和Comparable接口,提供了一個(gè)比較的方法,所有實(shí)現(xiàn)該接口的類,都動(dòng)態(tài)的實(shí)現(xiàn)了該比較方法,需要的朋友可以參考下
    2023-08-08
  • java Scanner類的使用示例代碼

    java Scanner類的使用示例代碼

    這篇文章主要介紹了java Scanner類的使用,Scanner類還可以任意地對(duì)字符串和基本類型(如int和double)的數(shù)據(jù)進(jìn)行分析。借助于Scanner,可以針對(duì)任何要處理的文本內(nèi)容編寫自定義的語法分析器,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • SpringBoot配置項(xiàng)目訪問路徑URL的根路徑方式

    SpringBoot配置項(xiàng)目訪問路徑URL的根路徑方式

    這篇文章主要介紹了SpringBoot配置項(xiàng)目訪問路徑URL的根路徑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • springboot lua檢查redis庫存的實(shí)現(xiàn)示例

    springboot lua檢查redis庫存的實(shí)現(xiàn)示例

    本文主要介紹了springboot lua檢查redis庫存的實(shí)現(xiàn)示例,為了優(yōu)化性能,通過Lua腳本實(shí)現(xiàn)對(duì)多個(gè)馬戲場(chǎng)次下的座位等席的庫存余量檢查,感興趣的可以了解一下
    2024-09-09

最新評(píng)論

通河县| 平顺县| 台湾省| 广元市| 磴口县| 都昌县| 惠安县| 浦县| 石河子市| 旌德县| 儋州市| 东源县| 阳新县| 蓬莱市| 卫辉市| 惠安县| 屏东县| 萨迦县| 东兴市| 六枝特区| 东兴市| 辽阳市| 铜山县| 巴中市| 深州市| 邹城市| 福贡县| 塔城市| 休宁县| 阳山县| 广德县| 登封市| 安吉县| 天长市| 汉川市| 荔浦县| 津市市| 伊川县| 姚安县| 清远市| 通州区|