자바 엑셀 파일 조작
package com.deppon.tps.module.aboutexcel.server.poidlexcel.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import javax.servlet.http.HttpServletResponse;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisFileEntity;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisResponseEntity;
public interface IPOIdwExcelUtilsService {
    /**
     *          ,  ,       
     * @param entity
     * @return
     */
    public AnalysisResponseEntity importFileData(AnalysisFileEntity entity);
    /**
     *      ,   ,     excel  
     * @param title
     * @param entity
     * @return
     */
    public ByteArrayOutputStream exportExcel(String title,AnalysisResponseEntity entity);
    /**
     *     ,   ,     excel  
     * @param title
     * @param entity
     * @return
     */
    public ByteArrayOutputStream exportExcel(File file,AnalysisResponseEntity entity);
    /**
     *     ,     excel  ,    excel  
     * @param file
     * @param entity
     * @return
     */
    public String createExcel(File file,AnalysisResponseEntity entity);
    /**
     *   excel
     * @param path
     * @param response
     */
    public void download(String path, HttpServletResponse response);
}
서비스 구현 클래스
package com.deppon.tps.module.aboutexcel.server.poidlexcel.service.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.annotation.XmlElement;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.sonatype.aether.util.StringUtils;
import com.deppon.foss.framework.exception.BusinessException;
import com.deppon.tps.module.aboutexcel.server.poidlexcel.service.IPOIdwExcelUtilsService;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisFileEntity;
import com.deppon.tps.module.aboutexcel.server.shared.domain.AnalysisResponseEntity;
import com.esafenet.dll.FileDlpUtil;
public class POIdwExcelUtlsServiceImpl implements IPOIdwExcelUtilsService {
    Logger log = Logger.getLogger(POIdwExcelUtlsServiceImpl.class);
    //sheet0         
    public static int SHEET_START_ROW_NUMBER = 0;
    //sheet0         
    public static int SHEET_START_COLUMN_NUMBER = 0;
    public static final int SHEET_LIMIT_ROW_NUMBER = 5000;
    /**
     *       
     * @param entity
     * @return
     */
    public AnalysisResponseEntity importFileData(AnalysisFileEntity entity){
        final File historyfile = entity.getFile();
        final File file = decryptFile(historyfile);
        entity.setFile(file);
        log.info("      ,"+file.getName());
        //          
        judgeFile(file);
        //       
        AnalysisResponseEntity responseentity = analysisFile(entity);
                if(historyfile.exists()){
                    historyfile.delete();   
                }
                if(file.exists()){
                    file.delete();  
                }
        return responseentity;
    }
    private File decryptFile(File file){
        String filepath = file.getAbsolutePath().substring(0,(int)file.getAbsoluteFile().toString().length()-file.getName().length());
        String filename = file.getName().substring(0, file.getName().indexOf("."))+"_UN"+file.getName().substring(file.getName().indexOf("."));
        File copyfile = new File(filepath+filename);
        boolean copysuccess = FileDlpUtil.decryptFile(file.getAbsolutePath(),filepath+filename);
        if(copysuccess){
            return copyfile;    
        }
        return null;
    }
    /**
     *           
     * @param file
     */
    public void judgeFile(File file){
        judegFileNull(file);
        //        
        judegFileSuffix(file);
        //      
        judgeFilelength(file);
        //      
        judegFreeMemory(file);
    }
    public void judegFileNull(File file){
        if(null == file || file.length()<=0){
            log.error("    !");
            throw new BusinessException("    !");
        }
    } 
    /**
     *         
     * @param file
     */
    public void judegFileSuffix(File file){
        String filename = file.getName();
        String sufffilename = "";
        if ((filename == null) ||((filename != null)&& (filename.length() <= 0))) {
            log.error("    :"+file.getName()+"     ");
            throw new BusinessException("       ,     ");
        }
        int dot = filename.lastIndexOf('.'); 
        if ((dot >-1) && (dot < (filename.length()))) { 
            sufffilename =   filename.substring(dot+1); 
            if(!"xls".equalsIgnoreCase(sufffilename) &&
               !"xlsx".equalsIgnoreCase(sufffilename)&&
               !"xlsm".equalsIgnoreCase(sufffilename)){
                log.error("  :"+file.getName()+"     ");
                throw new BusinessException("       ,     ");
            }
        } 
    }
    /**
     *       
     * @param file
     */
    public void judgeFilelength(File file){
        long length = file.length();
        //         3M
        if(length>3*1024*1024){
            log.error("  :"+file.getName()+"  ,"+file.length());
            throw new BusinessException("    ,     ");
        }
    }
    /**
     *       
     * @param file
     */
    public void judegFreeMemory(File file){
        long freememory = Runtime.getRuntime().freeMemory();
        //        3M    
        if(freememory < 3*1024*1024){
            log.error("  :"+file.getName()+"   ,    ");
            throw new BusinessException("    ,     !");
        }
    }
    /**
     *        
     * @param entity
     */
    public AnalysisResponseEntity analysisFile(AnalysisFileEntity entity){
        AnalysisResponseEntity responseentity = new AnalysisResponseEntity();
        //        
        XSSFWorkbook workbook = null;
        //       class  --  
        Field[] columnClassNamez = null;
        //    --  
        String[] columnNamez = null; 
        // excel          
        List두 개의 Javabean 중 하 나 는 Analysis FileEntity 입 니 다. 이 정 의 는 엑셀 파일 의 내 제목 이 전 달 된 클래스 와 대응 하 는 클래스 입 니 다.
package com.deppon.tps.module.aboutexcel.server.shared.domain;
import java.io.File;
public class AnalysisFileEntity {
    //     
    public File file;
    //   class,   name
    public String className;
//  //   class,   path
//  public String classPath;
    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }
    /**
     * @param file the file to set
     */
    public void setFile(File file) {
        this.file = file;
    }
    /**
     * @return the className
     */
    public String getClassName() {
        return className;
    }
    /**
     * @param className the className to set
     */
    public void setClassName(String className) {
        this.className = className;
    }
//  /**
//   * @return the classPath
//   */
//  public String getClassPath() {
//      return classPath;
//  }
//  /**
//   * @param classPath the classPath to set
//   */
//  public void setClassPath(String classPath) {
//      this.classPath = classPath;
//  }
}
이 JavaBean - AnalysisResponseEntity 는 해 석 된 데 이 터 를 저장 하 는 데 사 용 됩 니 다.
package com.deppon.tps.module.aboutexcel.server.shared.domain;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
public class AnalysisResponseEntity extends AnalysisFileEntity{
    //       class  --  
    Field[] columnClassNamez = null;
    //    --  
    String[] columnNamez = null; 
    // excel          
    List list = new LinkedList();
    // excel          
    List errorlist = new LinkedList();
    // excel          
    List errorMessagelist = new LinkedList();
    /**
     * @return the columnClassNamez
     */
    public Field[] getColumnClassNamez() {
        return columnClassNamez;
    }
    /**
     * @param columnClassNamez the columnClassNamez to set
     */
    public void setColumnClassNamez(Field[] columnClassNamez) {
        this.columnClassNamez = columnClassNamez;
    }
    /**
     * @return the columnNamez
     */
    public String[] getColumnNamez() {
        return columnNamez;
    }
    /**
     * @param columnNamez the columnNamez to set
     */
    public void setColumnNamez(String[] columnNamez) {
        this.columnNamez = columnNamez;
    }
    /**
     * @return the list
     */
    public List getList() {
        return list;
    }
    /**
     * @param list the list to set
     */
    public void setList(List list) {
        this.list = list;
    }
    /**
     * @return the errorlist
     */
    public List getErrorlist() {
        return errorlist;
    }
    /**
     * @param errorlist the errorlist to set
     */
    public void setErrorlist(List errorlist) {
        this.errorlist = errorlist;
    }
    /**
     * @return the errorMessagelist
     */
    public List getErrorMessagelist() {
        return errorMessagelist;
    }
    /**
     * @param errorMessagelist the errorMessagelist to set
     */
    public void setErrorMessagelist(List errorMessagelist) {
        this.errorMessagelist = errorMessagelist;
    }
}
            * 테스트 클래스 입 니 다 *
    @Test
    public void importFileData(){
        AnalysisFileEntity entity = new AnalysisFileEntity();
        File sourcefile = new File("d:\\314746\\Desktop\\test\\testexcel-UN.xlsx");
        File file = new File("d:\\314746\\Desktop\\test\\testexcel1.xlsx");
        entity.setFile(file);
        String className = "com.deppon.tps.module.aboutexcel.server.poidwexcel.shared.domain.testEntity";
        entity.setClassName(className);
        AnalysisResponseEntity  entity1 = poidwExcelUtlsServiceImpl.importFileData(entity);
        List list = entity1.getList();
        for(int i=0;iget(i);
            testEntity entity2 = (testEntity)obj;
            System.out.println(entity2.getIndex());
        }
        //        
        if(null !=entity1.getErrorlist() && entity1.getErrorlist().size()>0){
            String path = poidwExcelUtlsServiceImpl.createExcel(sourcefile,entity1);
            System.out.println(path);
        }
    }  이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POI Excel 사용자 정의 날짜 형식 읽기 (인스턴스 코드)POI로 Excel 데이터 읽기: (버전 번호: POI3.7) 1. Excel 읽기 2, Excel 데이터 처리: Excel 저장 날짜, 시간은 모두 수치 형식으로 저장되며, 읽을 때 POI가 먼저 수치 유형인지 아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.