1. 程式人生 > >Java匯入匯出資料到給定多sheet的excel檔案

Java匯入匯出資料到給定多sheet的excel檔案

JAVA程式實現匯入匯出資料到給定的EXCEL檔案模板中

首先建立一個maven專案,在pom.xml檔案中引入需要的包(這裡3.14版本的poi會有一些問題,在匯入07版本的excel時xmlbeans使用2.6.0及以上版本):

<dependency>
    <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>

準備好需要匯入的excel模板檔案:D://a.xlxs 。

關鍵程式碼:
//把查詢出的資料匯出到給定的excel模板中
public byte[] exportExcel(int startIndex, String[] keys, Map<String, Object> data){
    //獲取到模板檔案
    File excel = PropertiesUtil.getFile("D://a.xlxs");
    FileInputStream fis = new FileInputStream(excel);
    //建立excel
    XSSFWorkbook book = new XSSFWorkbook(fis);
    //keys是對應的匯出資料欄位,map中的key列表
    for(int i=0; i<keys.length; k++){
        String[] key = keys[i];
        XSSFSheet sheet = book.getSheetAt(i);
        List<Map<String, Object>> list = data.get("list"+i);
    for(int j=0; j<list.size(); k++){
        Row row = sheet.createRow(j+startIndex);
        Map<String, Object> map = list.get(j);
                for(int k=0; k<key.length; k++){
                    String s = map.get(key[k])+"";
                    row.createCell(k).setCellValue(s);
                    }
    }
    }
}
    ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wbs.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    return os.toByteArray();
}
檔案匯入
public Map<String, Object> importSeries(InputStream is, String[] keys, int startIndex) throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        Workbook book = null;
        try {
            book = new XSSFWorkbook(is);
        } catch (IOException e) {
            book = new HSSFWorkbook(is);
        }
        for(int i=0; i<keys.length; i++){
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            String[] key = keys[i];
            Sheet sheet = book.getSheetAt(i);
            int totalRows = sheet.getLastRowNum();
            for(int j = 0; j<totalRows; j++){
                Row row = sheet.getRow(startIndex+j);
                if(row == null){
                    continue;
                }
                int totalCells = row.getLastCellNum();
                Map<String, Object> map1 = new HashMap<String, Object>();
                for(int k=0; k<totalCells; k++){
                    Cell cell = row.getCell(k);
                    if(cell!=null){
                        map1.put(key[k], row.getCell(k).getStringCellValue());
                    }
                }
                list.add(map1);
            }
            map.put("list"+i, list);
        }
        return map;
    }
匯出到檔案
private static void exportExcel(List<ZSMM_SRM_MAT_SYNC> list) throws IOException{
        List<ZSMM_SRM_MAT_SYNC> list0 = list.subList(0, 20000);
        List<ZSMM_SRM_MAT_SYNC> list1 = list.subList(20000, 40000);
        List<ZSMM_SRM_MAT_SYNC> list2 = list.subList(40000, list.size());
        List<Object> listx = new ArrayList<Object>();
        listx.add(list0);
        listx.add(list1);
        listx.add(list2);
        for(int j=0; j<3; j++){
        System.out.println(j);
            @SuppressWarnings("unchecked")
            List<ZSMM_SRM_MAT_SYNC> lists= (List<ZSMM_SRM_MAT_SYNC>) listx.get(j);
        XSSFWorkbook wb = new XSSFWorkbook();
         XSSFSheet sheet = wb.createSheet("物料主資料"+j);
         sheet.setDefaultColumnWidth(20);
            //XSSFCellStyle style = wb.createCellStyle();
            XSSFRow row = sheet.createRow(0);
           // style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("物料號"); 
            cell = row.createCell(1);  
            cell.setCellValue("物料描述");  
            cell = row.createCell(2);  
            cell.setCellValue("建立日期");  
            cell = row.createCell(3);  
            cell.setCellValue("刪除識別符號");  
            cell = row.createCell(4);  
            cell.setCellValue("跨工廠物料狀態 ");  
            cell = row.createCell(5);  
            cell.setCellValue("物料型別");  
            cell = row.createCell(6);  
            cell.setCellValue("物料組");  
            cell = row.createCell(7);  
            cell.setCellValue("外部物料組");  
            cell = row.createCell(8);  
            cell.setCellValue("基本計量單位");  
            cell = row.createCell(9);  
            cell.setCellValue("採購訂單的計量單位 ");  
            cell = row.createCell(10);  
            cell.setCellValue("實驗室/設計室 ");  
            cell = row.createCell(11);  
            cell.setCellValue("毛重 ");  
            cell = row.createCell(12);  
            cell.setCellValue("淨重 ");  
            cell = row.createCell(13);  
            cell.setCellValue("重量單位 ");  
            cell = row.createCell(14);  
            cell.setCellValue("業務量 ");  
            cell = row.createCell(15);  
            cell.setCellValue("體積單位 ");  
            cell = row.createCell(16);  
            cell.setCellValue("長 ");  
            cell = row.createCell(17);  
            cell.setCellValue("寬 ");  
            cell = row.createCell(18);  
            cell.setCellValue("高");  
            cell = row.createCell(19);  
            cell.setCellValue("物料描述-英 ");  
        for (int i = 0; i < lists.size(); i++) {
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(lists.get(i).getMATNR());
            row.createCell(1).setCellValue(lists.get(i).getMAKTX());
            row.createCell(2).setCellValue(lists.get(i).getERSDA()+"");
            row.createCell(3).setCellValue(lists.get(i).getLVORM());
            row.createCell(4).setCellValue(lists.get(i).getMSTAE());
            row.createCell(5).setCellValue(lists.get(i).getMTART());
            row.createCell(6).setCellValue(lists.get(i).getMATKL());
            row.createCell(7).setCellValue(lists.get(i).getEXTWG());
            row.createCell(8).setCellValue(lists.get(i).getMEINS());
            row.createCell(9).setCellValue(lists.get(i).getBSTME());
            row.createCell(10).setCellValue(lists.get(i).getLABOR());
            row.createCell(11).setCellValue(lists.get(i).getBRGEW()+"");
            row.createCell(12).setCellValue(lists.get(i).getNTGEW()+"");
            row.createCell(13).setCellValue(lists.get(i).getGEWEI());
            row.createCell(14).setCellValue(lists.get(i).getVOLUM()+"");
            row.createCell(15).setCellValue(lists.get(i).getVOLEH());
            row.createCell(16).setCellValue(lists.get(i).getLENGTH());
            row.createCell(17).setCellValue(lists.get(i).getWIDTH());
            row.createCell(18).setCellValue(lists.get(i).getHEIGHT());
            row.createCell(19).setCellValue(lists.get(i).getMAKTX_E());
        }
                FileOutputStream out = new FileOutputStream("E://物料主資料"+j+".xlsx");
                wb.write(out);
                out.close();
                System.out.println("ok"+j);
    }