欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

利用Java如何實(shí)現(xiàn)解析Excel文件并存入數(shù)據(jù)庫(kù)中

利用Java如何實(shí)現(xiàn)解析Excel文件并存入數(shù)據(jù)庫(kù)中?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括呼瑪網(wǎng)站建設(shè)、呼瑪網(wǎng)站制作、呼瑪網(wǎng)頁(yè)制作以及呼瑪網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,呼瑪網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到呼瑪省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

1.web.xml中的配置文件

web.xml中的配置文件就按照這種方式寫,只需要把"application.xml"換成你的配置文件名即可

<!--文件上傳對(duì)應(yīng)的配置文件-->
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener>
  <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:application.xml</param-value> 
  </context-param>

2.application.xml的配置文件(固定寫發(fā))

在這個(gè)配置文件中你還可以規(guī)定上傳文件的格式以及大小等多種屬性限制

<!-- 定義文件上傳解析器 --> 
  <bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   
   </bean>

3.文件上傳的前端HTML

注意:

1.enctype="multipart/form-data" 必須寫,封裝表單

2.method="post",提交方式必須為"post"提交

3.action="${text}/uploadfile", "uploadfile"切記不要寫成"upload",否則你找到世界末日也不會(huì)找到哪里有問題(本人因?yàn)檫@個(gè)折騰了一天多時(shí)間)。

<form name="fileupload" enctype="multipart/form-data" action="${text}/uploadfile" method="post">
<p >請(qǐng)選擇正確的excel文件上傳</p>
<input id="txt" class="input" type="text" disabled="disabled" value="文件域" name="txt">
 <input class="liulan" type="button" onclick="file.click()" size="30" value="上傳文件" onmousemove="file.style.pixelLeft=event.x-60;file.style.pixelTop=this.offsetTop;">
<input id="file1" class="files" type="file" hidefocus="" size="1"  name="file" onchange="txt.value=this.value">
<br/><input type="button" onclick="checkSuffix();" value="提交上傳" >
 <p >支持的excel格式為:xls、xlsx、xlsb、xlsm、xlst!</p>
</form>

4.驗(yàn)證上傳文件的格式

//用于驗(yàn)證文件擴(kuò)展名的正則表達(dá)式
function checkSuffix(){
 var name = document.getElementById("txt").value;
 var strRegex = "(.xls|.xlsx|.xlsb|.xlsm|.xlst)$"; 
 var re=new RegExp(strRegex);
 if (re.test(name.toLowerCase())){
  alert("上傳成功");
  document.fileupload.submit();
 } else{
  alert("文件名不合法"); 
 }
}

5.dao層的接口和實(shí)現(xiàn)類

package com.gxxy.team1.yyd.dao;
public interface IFileUploadDao {
   public void save(Object o);
 }
package com.gxxy.team1.yyd.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gxxy.team1.yyd.dao.IFileUploadDao;
@Repository
public class FileUploadDaoImpl implements IFileUploadDao {
  @Autowired
  private SessionFactory sessionFactory;
  private Session getSession() {
    Session session = sessionFactory.getCurrentSession();
    return session;
  }
  @Override
  public void save(Object o) {
    
    getSession().save(o);
  }

}

6.service層的接口和實(shí)現(xiàn)類

package com.gxxy.team1.yyd.service;

import java.util.List;

public interface IFileUploadService {
  public List<String[]> readExcel(String path);
  public void save(Object o);
}
package com.gxxy.team1.yyd.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxxy.team1.yyd.dao.IFileUploadDao;
import com.gxxy.team1.yyd.service.IFileUploadService;
@Service
public class FileUploadServiceImpl implements IFileUploadService {
  @Autowired
  private IFileUploadDao fileDao;
  @Override
  public List<String[]> readExcel(String path) { 
      SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 
      List<String[]> list = null;
      try { 
        //同時(shí)支持Excel 2003、2007 
        File excelFile = new File(path); //創(chuàng)建文件對(duì)象 
        FileInputStream is = new FileInputStream(excelFile); //文件流 
        Workbook workbook = WorkbookFactory.create(is); //這種方式 Excel 2003/2007/2010 都是可以處理的 
        int sheetCount = workbook.getNumberOfSheets(); //Sheet的數(shù)量
       //存儲(chǔ)數(shù)據(jù)容器 
        list = new ArrayList<String[]>();
        //遍歷每個(gè)Sheet 
        for (int s = 0; s < sheetCount; s++) { 
          Sheet sheet = workbook.getSheetAt(s); 
          int rowCount = sheet.getPhysicalNumberOfRows(); //獲取總行數(shù)      
          //遍歷每一行 
          for (int r = 0; r < rowCount; r++) { 
            Row row = sheet.getRow(r); 
            int cellCount = row.getPhysicalNumberOfCells(); //獲取總列數(shù) 
           //用來存儲(chǔ)每行數(shù)據(jù)的容器 
            String[] model = new String[cellCount-1];
            //遍歷每一列 
            for (int c = 0; c < cellCount; c++) { 
              Cell cell = row.getCell(c); 
              int cellType = cell.getCellType();
              
              if(c == 0) continue;//第一列ID為標(biāo)志列,不解析
              
              String cellValue = null; 
              switch(cellType) { 
                case Cell.CELL_TYPE_STRING: //文本 
                  cellValue = cell.getStringCellValue(); 
                  //model[c-1] = cellValue;
                  break; 
                case Cell.CELL_TYPE_NUMERIC: //數(shù)字、日期 
                  if(DateUtil.isCellDateFormatted(cell)) { 
                    cellValue = fmt.format(cell.getDateCellValue()); //日期型 
                    //model[c-1] = cellValue;
                  } 
                  else { 
                    
                    cellValue = String.valueOf(cell.getNumericCellValue()); //數(shù)字 
                    //model[c-1] = cellValue;                  
                  } 
                  break; 
                case Cell.CELL_TYPE_BOOLEAN: //布爾型 
                  cellValue = String.valueOf(cell.getBooleanCellValue()); 
                  break; 
                case Cell.CELL_TYPE_BLANK: //空白 
                  cellValue = cell.getStringCellValue(); 
                  break; 
                case Cell.CELL_TYPE_ERROR: //錯(cuò)誤 
                  cellValue = "錯(cuò)誤"; 
                  break; 
                case Cell.CELL_TYPE_FORMULA: //公式 
                  cellValue = "錯(cuò)誤"; 
                  break; 
                default: 
                  cellValue = "錯(cuò)誤"; 
                  
              } 
              System.out.print(cellValue + "  "); 
              
              model[c-1] = cellValue;
            } 
            //model放入list容器中 
            list.add(model);
            System.out.println(); 
          } 
        } 
        is.close();     
      } 
      catch (Exception e) { 
        e.printStackTrace(); 
      }
      
      return list;  
    }
  @Override
  public void save(Object o) {
    fileDao.save(o);
  } 
  }

7.controller層實(shí)現(xiàn)

//文件上傳方法
  @RequestMapping("/uploadfile")
  public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model,Model mod) throws Exception {
    String path = request.getSession().getServletContext().getRealPath("upload");
    System.out.println("文件路徑:"+path);
    String originalFilename = file.getOriginalFilename();
    String type = file.getContentType();
    //originalFilename = UUID.randomUUID().toString()+originalFilename;
    System.out.println("目標(biāo)文件名稱:"+originalFilename+",目標(biāo)文件類型:"+type);
    File targetFile = new File(path,originalFilename );
    if (!targetFile.getParentFile().exists()) {
      targetFile.getParentFile().mkdirs();
    }else if (!targetFile.exists()) {
      targetFile.mkdirs();
    }
    // 獲得上傳文件的文件擴(kuò)展名
    String subname = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
    System.out.println("文件的擴(kuò)展名:"+subname);
    
    try {
      file.transferTo(targetFile);  
    } catch (Exception e) {
      e.printStackTrace();
    }
    FileUploadServiceImpl fileUp = new FileUploadServiceImpl();
    String rootpath = path + File.separator + originalFilename;
    List<String[]> excellist = fileUp.readExcel(rootpath);
    int len = excellist.size();
    System.out.println("集合的長(zhǎng)度為:"+len);
    for (int i = 0; i < len; i++) {
      String[] fields = excellist.get(i);
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      String sampleNo = fields[0];
  
      Double valueOf = Double.valueOf(fields[1]);
      int sampleType = valueOf.intValue(); //double轉(zhuǎn)int
      
      String createTime = fields[2];
      Date createTime1 = format.parse(createTime);  
      String name = fields[3];
      String pId = fields[4];
      String hospitalName = fields[5];
      String cellPhone = fields[6];
      Sample sample = new Sample(sampleNo, sampleType, createTime1, name, pId);
      Patient patient = new Patient(hospitalName, cellPhone);
      fileService.save(sample);
      fileService.save(patient);    
    }  
    //model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+originalFilename);
    
    String username = (String) request.getSession().getAttribute("username");
    List<List<Menu>> power = powerService.power(username);
    mod.addAttribute("list", power);
    return "redirect:/ yyd";
  }

關(guān)于利用Java如何實(shí)現(xiàn)解析Excel文件并存入數(shù)據(jù)庫(kù)中問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前標(biāo)題:利用Java如何實(shí)現(xiàn)解析Excel文件并存入數(shù)據(jù)庫(kù)中
文章地址:http://aaarwkj.com/article14/jjjgge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化網(wǎng)站排名、ChatGPT、網(wǎng)站導(dǎo)航、App開發(fā)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
日韩中文字幕视频久久| 日韩中字伦理熟妇人妻| 久久精品国产亚洲av不卡| 少妇又色又爽又高潮欧美| 欧美日韩69av网| 亚洲天堂毛片在线观看| 久亚洲精品色婷婷国产熟女| 欧美午夜福利视频观看| 91精品大片免费在线观看| 麻豆国产自拍在线视频| 亚洲综合久久国产一区二区| 亚洲欧美一区二区三区三| av中文字幕国产精品| 国产精品毛片一区二区三| 日韩黄色精品中文视频| 高h视频在线播放观看| 人妻有码一区二区三区| 婷婷91麻豆精品国产人妻| 久久人妻制服乱码中文字幕| 亚洲人成免费在线观看| 18禁视频免费无遮挡| 欧美日韩视频一区二区| 午夜亚洲欧美日韩在线| 91免费版在线观看网址| 亚洲精品一区二区三区三州| 亚洲最新一区二区在线观看| 黄色大片免费在线观看| 情五月激情亚洲丁香佳色| 97水蜜桃视频在线观看| 日韩免费视频一区二区三区免费| 国产午夜激情在线播放| 熟妇人妻精品一区二区三区颏| 91精品国语对白人妻刺激| 国产日韩手机在线不卡视频| 国产好大好爽在线免费观看| 国产黄色大片一级久久| 亚洲不卡免费在线视频| 欧美+亚洲+精品+三区| 男人午夜福利视频在线观看| 国产精品日本一区二区三区在线| 亚洲日本av一区二区|