javaweb-Excel導入導出后台代碼


前言:

導入導出后台java代碼寫好很久了,但是。。。但是。。。前台不會寫啊。

先把后台代碼帖上吧

1、excelToDb

package util;
/**
 * 代碼解釋:此方法將傳入一個URL,即為當前用戶所上傳的Excel的目標路徑,然后返回一個泛型為StudentInfoEntity的ArrayList
 */

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import dao.DistrictDao;
import dao.impl.DistrictDaoImp;

import entity.District;


public class ExcelToList {
    public ArrayList<District> excelTo(String URL) throws IOException{
        //創建list集合存放對象
        ArrayList<District> list = new ArrayList<District>();
    
        File file = new File(URL);

        Workbook workbook=null;
        try {
            workbook = WorkbookFactory.create(file);
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //代碼解釋:讀取默認第一個工作表sheet
        Sheet sheet =  workbook.getSheetAt(0);
        //代碼解釋:獲取sheet中最后一行行號
        int lastRowNum = sheet.getLastRowNum();
       // System.out.println("lastRowNum========="+lastRowNum);
        //代碼解釋:循環所有行
        ArrayList<String> list2 = new ArrayList<String>();
        for (int i = 1; i <= lastRowNum; i++) {
            //代碼解釋:獲取當前行中的內容
            Row row = sheet.getRow(i);
            short cell = row.getLastCellNum();
          //  System.out.println("cellnumber======="+cell);
            if(row !=null && cell!=0){
                for(int j=0;j<cell;j++){
                    District district=new District();
                    Cell name=row.getCell(j);
                    district.setName(getValue(name));
                    list.add(district);
                }
            }
        }
        return list;
    }
    //取單元格中的值
    public String getValue(Cell cell){
        String result="";
        if(cell.getCellType() == cell.CELL_TYPE_BOOLEAN){
            //返回布爾類型的值
            result = cell.getBooleanCellValue() +"";
        }else if(cell.getCellType() == cell.CELL_TYPE_NUMERIC){
            //返回數值類型的值
            if(HSSFDateUtil.isCellDateFormatted(cell)){
                result = DateUtil.getJavaDate(cell.getNumericCellValue()).toString();
            }else{
                result = cell.getNumericCellValue()+"";
            }
            return String.valueOf(cell.getNumericCellValue());
        }else if(cell.getCellType() == cell.CELL_TYPE_FORMULA){
            result = cell.getCellFormula();
        }else if(cell.getCellType() == cell.CELL_TYPE_STRING){
            result = cell.getStringCellValue();
        }else{
            //返回字符口串類型的值
            result = cell.getStringCellValue();
        }
        return result;
    }
    
    public static void main(String[] args) throws IOException {
        ExcelToList excelToList=new ExcelToList();
        ArrayList<District> list =excelToList.excelTo("D:\\work\\File\\district.xls");
        DistrictDao dao=new DistrictDaoImp();
        for (District district : list) {
            dao.addDistrict(district);
        }
    }
    
    
}

這里我設想的很美,JSP頁面點擊導入,選擇文件,給我個xls的路徑,我就可以返回給你個結果list集供你頁面顯示。但是在點擊打開選擇文件窗口選擇文件確認后傳路徑給后台的JS代碼我不會寫。。。。。

2、listToExcel:導出

package util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import dao.DistrictDao;
import dao.impl.DistrictDaoImp;

import entity.District;

/**
 * 導出,從數據庫中導出數據,並生成excel文件
 * @author 0
 *
 */
public class OutExcel {
    //動態從JSp頁面獲取標題
    public String downloadGrade(String contextPath,String filename){
        DistrictDao  districtDao=new DistrictDaoImp();
        List<District> listd = districtDao.getDistricts();
        String[] title={"ID","disname"};
        //創建excel工作薄
        XSSFWorkbook workbook=new XSSFWorkbook();
        //創建一個工作表sheet
        XSSFSheet sheet=workbook.createSheet();
        //創建第一行
        XSSFRow row=sheet.createRow(0);
        XSSFCell cell=null;
        //插入第一行數據 id 地區名稱
        for(int i=0;i<title.length;i++){
            //創建一行的一格
            cell = row.createCell(i);
            //賦值
            cell.setCellValue(title[i]);
        }
        //追加數據行數
        int j=1;
        for(int i=0;i<listd.size();i++){
            //從集合中得到一個對象
            District district2=listd.get(i);
            //創建第2行
            XSSFRow nextrow=sheet.createRow(j);
            //創建第1列並賦值
            XSSFCell cess2 = nextrow.createCell(0);
            cess2.setCellValue(district2.getId());
            //創建第2列並賦值
            XSSFCell cess3 = nextrow.createCell(1);
            cess3.setCellValue(district2.getName());
            j++;
        }
        
        //創建一個文件
        String path = contextPath +"/"+filename+".xlsx";
        File file =new File(path);
        try {
            file.createNewFile();
            //將excel內容存盤
            FileOutputStream fos =FileUtils.openOutputStream(file);
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return path;
    }
    
    public static void main(String[] args) {
        OutExcel oe=new OutExcel();
        
        String path = oe.downloadGrade("D:", "district");
        System.out.println(path);
    }
}

這個導出,我是想JSP給我傳勾選的每一行的信息給后端處理,然后按需導出。但頁面JS那塊不會處理

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM