POI recorded instance of Excel export

    XSSFWorkbook createUserListExcel static public (List <the Map <String, Object >> listresult) { 
        // Create 1. HSSFWorkbook, an Excel file corresponding to a HSSFWorkbook 
        XSSFWorkbook XSSFWorkbook new new WB = (); 
        // 2. Add a sheet in the workbook, the corresponding sheet Excel file 
        XSSFSheet sheet = wb.createSheet ( "Sheet1"); 
        // header 3. disposed, i.e., each column of the column name 
        String [] titel = { "rowName1 ", "rowName2", "rowName3", "rowName4"}; 
        // create the first line of 3.1 
        XSSFRow row = sheet.createRow (0); 
        // here to create a number column 
        row.createCell (0) .setCellValue ( "No."); 
        // write the column name the 

        Object style = getDefaultHeaderCellStyle (WB); 

        List <ExcelColumn>excelColumns =  ImportExportExcelUtil.xmlToBean(path);;

        Font redFont =  wb.createFont();
        redFont.setColor(Font.COLOR_RED);// 红色


        int j=0;
        for (ExcelColumn e:excelColumns){
            Cell cell = row.createCell(j + 1);
            if(e.getText().contains("*")){
                XSSFRichTextString richString = new XSSFRichTextString(e.getText());

              //  richString.applyFont( 0, 6,  blueFont );

                richString.applyFont( e.getText().length()-2, e.getText().length(), redFont );

                cell.setCellValue(richString);
            }else {
                cell.setCellValue(e.getText());
            }

          //  row.createCell(j+1).setCellValue(e.getText());
            ++ J; 
        } 
// 
// for (int I = 0; I <titel.length; I ++) { 
// // write data to the column, creating a cell, data is written 
// row.createCell (i + 1) .setCellValue (Titel [I]); 
//} 



        // formal writing data 
        for (int I = 0; I <listresult.size (); I ++) { 
            // create a row 
            row = sheet.createRow (i + 1) ; 
            // number 
            row.createCell (0) .setCellValue (I +. 1); 
            // name of hospital 
            row.createCell (1) .setCellValue (. listresult.get (i) .get ( "rowKey1") toString ()); 
            sheet.autoSizeColumn (. 1, to true); 
            // service type 
            row.createCell (2) .setCellValue (listresult.get (I) .get ( "rowKey2") toString ());. 
            // abnormality information
            row.createCell (. 3) .setCellValue (listresult.get (I) .get ( "rowKey3") toString ().); 
            // Number 
            XSSFCell Cell row.createCell = (. 4); 
            XSSFRichTextString RichString new new XSSFRichTextString = ( "the Hello * "); 

        // richString.applyFont (0,. 6, blueFont); 

            richString.applyFont (. 4,. 6, redFont); 

            cell.setCellValue (RichString); 
         // cell.setCellValue (listresult.get (I) .get (" rowKey4 ") toString ());. 
     // cell.setCellStyle ((the CellStyle) style); 



        } 
        / **  
         * the above operation is already generating a complete file, the file can only need to stream into generated;
         * set the following optional width, little impact on the overall 
         * / 
        // set the cell width 
        int curColWidth = 0; 
        for (int I = 0; I <= titel.length; I ++) { 
            // adaptive column width, half-width for Chinese unfriendly, if the column It contains Chinese need to re-set containing the Chinese. 
            sheet.autoSizeColumn (I, to true); 
            // set a minimum value for each column, to facilitate the display Chinese 
            curColWidth = sheet.getColumnWidth (I); 
            IF (curColWidth <2500) { 
                sheet.setColumnWidth (I, 2500); 
            } 
            // The first three words are more larger set point. 
            sheet.setColumnWidth (. 3, 8000); 
        } 
        return WB; 
    }

  Object Output

    static String downUserList Private (List <the Map <String, Object >> listresult) { 
        // getTime () returns a string that is the current time, used to make the file name 
        String name = "ExcelTest"; 
        // csvFile is one of my path, is provided on the line itself 
       // String csvFile + YS = "//" + name + ".xlsx"; 
        // 1. generating Excel 
        XSSFWorkbook userListExcel = createUserListExcel (listresult); 
        the try { 
            // file to output 
// file file = File new new (csvFile); 
// IF (! File.Exists () || file.isDirectory ()) { 
// file.mkdirs (); 
//} 
            // the TODO generated wb object transmission 
            FileOutputStream outputStream = new FileOutputStream ( new File (path2)); 
            userListExcel.write (outputStream) ; 
            outputStream.close ();
        }catch(Exception e){
            e.printStackTrace();
        }
        return name;
    }

  transfer

    public static void  main(String[] asge){
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("rowKey1","rowKey1");
        map.put("rowKey2","rowKey2");
        map.put("rowKey3","rowKey3");
        map.put("rowKey4","rowKey4");

        list.add(map);
        downUserList(list);

        List<ExcelColumn> excelColumns = getExcelColumns();



    }

  

Guess you like

Origin www.cnblogs.com/wangfl/p/10953941.html