国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

java中使用POI生成Excel并導(dǎo)出

這篇具有很好參考價值的文章主要介紹了java中使用POI生成Excel并導(dǎo)出。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

注:本文章中代碼均為本地Demo版本,若后續(xù)代碼更新將不會更新文章

需求說明及實現(xiàn)方式

  1. 根據(jù)從數(shù)據(jù)庫查詢出的數(shù)據(jù),將其寫入excel表并導(dǎo)出

    我的想法是通過在實體屬性上寫自定義注解的方式去完成。因為我們在代碼中可以通過反射的方式去獲取實體類中全部的注解及屬性名稱等等。我們可以在自定義注解中聲明一個參數(shù)value,這里面就存儲其標(biāo)題,這樣我們

  2. 數(shù)據(jù)查詢type不同,則顯示的標(biāo)題數(shù)量不同

    在注解類中增加type參數(shù),只有滿足對應(yīng)type的屬性會被導(dǎo)出至excel中

  3. 數(shù)據(jù)查詢type不同,則顯示的標(biāo)題不同(同一個字段)

    優(yōu)化參數(shù)value,判斷傳入的value是否為json字符串,如果是json字符串則找到其與type對應(yīng)的value

  4. 數(shù)據(jù)的格式化(時間類型格式化、數(shù)據(jù)格式化顯示)

    數(shù)據(jù)格式化顯示通過在注解類中增加dict參數(shù),該參數(shù)傳入json字符串。

本來我是想著通過easyExcel來完成這些功能,但是由于項目中已經(jīng)引入了POI的3.9版本依賴,然后easyExcel中POI的依賴版本又高于該版本,而且不管是版本升級還是版本排除降級,總會有一個出現(xiàn)問題,最終也只能通過最基礎(chǔ)的POI編寫代碼實現(xiàn)。

需求完成

依賴引入:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.33</version>
        </dependency>

java中使用POI生成Excel并導(dǎo)出,java,python,開發(fā)語言

通用代碼

  1. ExcelExport

    • value:標(biāo)題,也可為json字符串
    • dict:json字符串格式的字典,格式如User中所示
    • type:數(shù)組類型,查詢數(shù)據(jù)type類型是什么值時這個字段會寫入excel中。如我type = {"a"},則我在查詢數(shù)據(jù)時傳入的type為b則不會將這個字段寫入excel中,如果傳入的是a則會正常寫入。
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface ExcelExport {
        String value();
        String dict() default "";
        String[] type() default {};
    }
    
  2. User

    • @ExcelExport:即自定義的注解,其中值的含義在上面已經(jīng)說清楚了
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors(chain = true)
    public class User {
        @ExcelExport(value = "用戶名",type = {"a"})
        private String userName;
        @ExcelExport(value = "{a: '年齡',b: '年紀(jì)'}",type = {"a","b"})
        private Integer age;
        @ExcelExport(value = "性別",
                dict = "[{ value: \"0\", label: \"女\" }," +
                        "{ value: \"1\", label: \"男\(zhòng)" }]",
                type = {"a","b"})
        private Integer sex;
        @ExcelExport(value = "生日",type = {"b"})
        private Date birthday;
    }
    
    

版本1

版本1中未實現(xiàn)數(shù)據(jù)查詢type不同,則顯示的標(biāo)題不同(同一個字段)這一功能,如需要加請看PoiExcelUtil中writeTitleCellData方法。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzj.anno.ExcelExport;
import com.lzj.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.StringUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author:雷子杰
 * @date:2023/7/4
 */
public class One {
    public static void main(String[] args) throws IOException {
        List<User> userList = new ArrayList<>();
        userList.add(new User("lzj1",1,1,new Date()));
        userList.add(new User("lzj2",2,0,new Date()));
        userList.add(new User("lzj3",3,1,new Date()));
        userList.add(new User("lzj4",4,0,new Date()));

        //聲明XSSF對象
        XSSFWorkbook xssfSheets = new XSSFWorkbook();
        //創(chuàng)建sheet
        XSSFSheet userSheet = xssfSheets.createSheet("user");

        //創(chuàng)建標(biāo)題字體
        XSSFFont titleFont = xssfSheets.createFont();
        titleFont.setBold(true);//加粗
        titleFont.setFontName("微軟雅黑");
        titleFont.setFontHeightInPoints((short) 12);//字體大小
        //創(chuàng)建通用字體
        XSSFFont commonFont = xssfSheets.createFont();
        commonFont.setBold(false);//加粗
        commonFont.setFontName("微軟雅黑");
        commonFont.setFontHeightInPoints((short) 12);//字體大小

        // 創(chuàng)建標(biāo)題行單元格樣式
        CellStyle titleCellStyle = xssfSheets.createCellStyle();
        titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框線
        titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平對齊方式
        titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直對齊方式
        titleCellStyle.setFont(titleFont);//字體樣式
        titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//單元格前景色
        titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充單元格

        //創(chuàng)建通用行單元格樣式
        CellStyle commonCellStyle = xssfSheets.createCellStyle();
        commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
        commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        commonCellStyle.setFont(commonFont);
        commonCellStyle.setWrapText(true);//自動換行

        //獲取實體類中全部屬性
        Field[] fields = User.class.getDeclaredFields();
        //當(dāng)前行
        int currentRow = 0;
        //當(dāng)前列
        int currentColumn = 0;
        //行高
        float rowHeight = 40.1f;
        //列寬
        int columnWidth = 33 * 256;
        //創(chuàng)建行
        XSSFRow row = userSheet.createRow(currentRow);
        當(dāng)前行+1
        //currentRow++;

        //創(chuàng)建標(biāo)題行
        // 遍歷每個字段
        for (Field field : fields) {
            // 檢查字段是否帶有Explanation注解
            if (field.isAnnotationPresent(ExcelExport.class)) {
                // 獲取Explanation注解實例
                ExcelExport explanation = field.getAnnotation(ExcelExport.class);
                // 獲取注解中的解釋
                String value = explanation.value();

                //創(chuàng)建單元格,傳入值,設(shè)置單元格樣式
                XSSFCell cell = row.createCell(currentColumn);
                cell.setCellValue(value);
                cell.setCellStyle(titleCellStyle);

                //設(shè)置行高度
                row.setHeightInPoints(rowHeight);
                //設(shè)置列的寬度
                userSheet.setColumnWidth(currentColumn,columnWidth);
                //當(dāng)前列+1
                currentColumn++;
            }
        }
        //重置當(dāng)前列
        currentColumn = 0;

        //創(chuàng)建數(shù)據(jù)行
        for (User user : userList) {
            //每次循環(huán)時重置列
            currentColumn = 0;
            //當(dāng)前行+1
            currentRow++;
            //創(chuàng)建行
            row = userSheet.createRow(currentRow);
            for (Field field : fields) {
                if (field.isAnnotationPresent(ExcelExport.class)) {
                    try {
                        //解除private限制
                        field.setAccessible(true);

                        // 獲取Explanation注解實例
                        ExcelExport explanation = field.getAnnotation(ExcelExport.class);
                        // 獲取屬性的值
                        Object value = field.get(user);

                        //日期類型格式化
                        if (value != null && field.getType() == Date.class){
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            value = sdf.format(value);
                        }
                        //獲取對應(yīng)字典
                        String dict = explanation.dict();
                        if (!StringUtils.isEmpty(dict) && value != null){
                            //JSONObject jsonObject = JSON.parseObject(dict);
                            List<String> list = JSON.parseArray(dict, String.class);
                            for (String item : list) {
                                JSONObject jsonObject = JSON.parseObject(item);
                                if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
                                    value = jsonObject.getString("label");
                                    break;
                                }
                            }
                            //value = jsonObject.get(value.toString());
                        }
                        //創(chuàng)建單元格,傳入值,設(shè)置單元格樣式
                        XSSFCell cell = row.createCell(currentColumn);
                        cell.setCellValue(value == null?"":value.toString());
                        cell.setCellStyle(commonCellStyle);

                        //設(shè)置行高度
                        row.setHeightInPoints(rowHeight);
                        //當(dāng)前列+1
                        currentColumn++;

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        // 將生成的excel文件輸出流轉(zhuǎn)為字節(jié)數(shù)組
        byte[] bytes = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        xssfSheets.write(outputStream);
        outputStream.close();
        bytes = outputStream.toByteArray();

        //讀取字節(jié)數(shù)組為文件輸入流
        InputStream inputStream = new ByteArrayInputStream(bytes);
        inputStream.close();


        //在聲明一個輸出流將文件下載到本地
        File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        //將bytes中的內(nèi)容寫入
        bufferedOutputStream.write(bytes);
        //刷新輸出流,否則不會寫出數(shù)據(jù)
        bufferedOutputStream.flush();
        bufferedOutputStream.close();


    }
}

版本2

版本二相比與版本1,其主要優(yōu)勢是將POI相關(guān)操作都封裝進了PoiExcelUtil中。

  1. PoiExcelUtil

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.lzj.anno.ExcelExport;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.util.HSSFColor;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.xssf.usermodel.*;
    import org.springframework.util.StringUtils;
    
    import java.lang.reflect.Field;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    /**
     * <p>
     *
     * </p>
     *
     * @author:雷子杰
     * @date:2023/7/6
     */
    public class PoiExcelUtil {
    
        /**
         * 獲取標(biāo)題字體
         * @param xssfWorkbook
         * @return
         */
        public static XSSFFont getTitleFont(XSSFWorkbook xssfWorkbook){
            //創(chuàng)建標(biāo)題字體
            XSSFFont titleFont = xssfWorkbook.createFont();
            titleFont.setBold(true);//加粗
            titleFont.setFontName("微軟雅黑");
            titleFont.setFontHeightInPoints((short) 12);//字體大小
    
            return titleFont;
        }
    
        /**
         * 獲取通用字體
         * @param xssfWorkbook
         * @return
         */
        public static XSSFFont getCommonFont(XSSFWorkbook xssfWorkbook){
            //創(chuàng)建通用字體
            XSSFFont commonFont = xssfWorkbook.createFont();
            commonFont.setBold(false);//加粗
            commonFont.setFontName("微軟雅黑");
            commonFont.setFontHeightInPoints((short) 12);//字體大小
    
            return commonFont;
        }
    
        /**
         * 獲取標(biāo)題單元格樣式
         * @param xssfWorkbook
         * @param xssfFont
         * @return
         */
        public static CellStyle getTitleCellStyle(XSSFWorkbook xssfWorkbook , XSSFFont xssfFont){
            // 創(chuàng)建標(biāo)題行單元格樣式
            CellStyle titleCellStyle = xssfWorkbook.createCellStyle();
            titleCellStyle.setBorderTop(CellStyle.BORDER_THIN);//框線
            titleCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
            titleCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
            titleCellStyle.setBorderRight(CellStyle.BORDER_THIN);
            titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平對齊方式
            titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直對齊方式
            titleCellStyle.setFont(xssfFont);//字體樣式
            titleCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//單元格前景色
            titleCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充單元格
    
            return titleCellStyle;
        }
    
        /**
         * 獲取通用單元格樣式
         * @param xssfWorkbook
         * @param xssfFont
         * @return
         */
        public static CellStyle getCommonCellStyle(XSSFWorkbook xssfWorkbook, XSSFFont xssfFont){
            //創(chuàng)建通用行單元格樣式
            CellStyle commonCellStyle = xssfWorkbook.createCellStyle();
            commonCellStyle.setBorderTop(CellStyle.BORDER_THIN);
            commonCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
            commonCellStyle.setBorderLeft(CellStyle.BORDER_THIN);
            commonCellStyle.setBorderRight(CellStyle.BORDER_THIN);
            commonCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
            commonCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
            commonCellStyle.setFont(xssfFont);
            commonCellStyle.setWrapText(true);//自動換行
    
            return commonCellStyle;
        }
    
        /**
         * 寫入單個單元格數(shù)據(jù)
         * @param row 行對象
         * @param xssfSheet sheet對象
         * @param value 單元格的值
         * @param cellStyle 單元格樣式
         * @param rowHeight 行高
         * @param columnWidth 列寬
         */
        public static void writeCellData(XSSFRow row, XSSFSheet xssfSheet , Object value ,CellStyle cellStyle,Integer currentColumn,Float rowHeight,Integer columnWidth){
    
            //創(chuàng)建單元格,傳入值,設(shè)置單元格樣式
            XSSFCell cell = row.createCell(currentColumn);
            cell.setCellValue(value == null ? "" : value.toString());
            cell.setCellStyle(cellStyle);
            //設(shè)置行高度
            row.setHeightInPoints(rowHeight);
            //設(shè)置列的寬度
            xssfSheet.setColumnWidth(currentColumn,columnWidth);
        }
    
        /**
         *
         * @param row 行對象
         * @param xssfSheet sheet對象
         * @param cellStyle 單元格樣式
         * @param fields 反射獲取得到的實體對象的全部屬性
         * @param currentColumn 當(dāng)前列
         * @param rowHeight 行高
         * @param columnWidth 列寬
         * @param type 類型
         */
        public static void writeTitleCellData(XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Integer currentColumn,Float rowHeight,Integer columnWidth,String type){
            //創(chuàng)建標(biāo)題行
            // 遍歷每個字段
            for (Field field : fields) {
                // 檢查字段是否帶有ExcelExport注解
                if (field.isAnnotationPresent(ExcelExport.class)) {
                    // 獲取Explanation注解實例
                    ExcelExport explanation = field.getAnnotation(ExcelExport.class);
    
                    //判斷是否是需要寫入的數(shù)據(jù)類型
                    String[] typeArray = explanation.type();
                    Set<String> set = new HashSet<>(Arrays.asList(typeArray));
                    if (!set.contains(type)){
                     continue;
                    }
                    // 獲取注解中的解釋
                    String value = explanation.value();
                    //判斷value是否是json格式數(shù)據(jù)
                    boolean isJson = true;
                    try{
                        Object parse = JSON.parse(value);
                    }catch (Exception e){
                        isJson = false;
                    }
                    if (isJson == true){//如果是json格式數(shù)據(jù),則給他對應(yīng)對應(yīng)類型的值
                        JSONObject jsonObject = JSON.parseObject(value);
                        value = jsonObject.getString(type);
                    }
    
                    //寫入單元格數(shù)據(jù)
                    PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
                    //當(dāng)前列+1
                    currentColumn++;
                }
            }
        }
    
        /**
         * 將集合數(shù)據(jù)全部寫入單元格
         * @param list 需要寫入excel的集合數(shù)據(jù)
         * @param currentRow 當(dāng)前行
         * @param currentColumn 當(dāng)前列
         * @param row 行對象
         * @param xssfSheet sheet對象
         * @param cellStyle 單元格樣式
         * @param fields 反射獲取得到的實體對象的全部屬性
         * @param rowHeight 行高
         * @param columnWidth 列寬
         * @param type 類型
         * @param <T>
         */
        public static <T> void writeCommonRowCellData(List<T> list,Integer currentRow ,Integer currentColumn, XSSFRow row,XSSFSheet xssfSheet,CellStyle cellStyle,Field[] fields,Float rowHeight,Integer columnWidth,String type){
            //創(chuàng)建數(shù)據(jù)行
            for (T obj : list) {
                //每次循環(huán)時重置列
                currentColumn = 0;
                //當(dāng)前行+1
                currentRow++;
                //創(chuàng)建行
                row = xssfSheet.createRow(currentRow);
                for (Field field : fields) {
                    // 檢查字段是否帶有ExcelExport注解
                    if (field.isAnnotationPresent(ExcelExport.class)) {
                        try {
                            //解除private限制
                            field.setAccessible(true);
                            // 獲取Explanation注解實例
                            ExcelExport explanation = field.getAnnotation(ExcelExport.class);
    
                            //判斷是否是需要寫入的數(shù)據(jù)類型
                            String[] typeArray = explanation.type();
                            Set<String> set = new HashSet<>(Arrays.asList(typeArray));
                            if (!set.contains(type)){
                                continue;
                            }
    
                            // 獲取屬性的值
                            Object value = field.get(obj);
                            //日期類型格式化
                            if (value != null && field.getType() == Date.class){
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                value = sdf.format(value);
                            }
                            //獲取對應(yīng)字典
                            String dict = explanation.dict();
                            if (!StringUtils.isEmpty(dict) && value != null){
                                List<String> parseArray = JSON.parseArray(dict, String.class);
                                for (String item : parseArray) {
                                    JSONObject jsonObject = JSON.parseObject(item);
                                    if(value == null ? false : jsonObject.getString("value").equals(value.toString()) ){
                                        value = jsonObject.getString("label");
                                        break;
                                    }
                                }
                            }
                            //寫入單元格數(shù)據(jù)
                            PoiExcelUtil.writeCellData(row,xssfSheet,value,cellStyle,currentColumn,rowHeight,columnWidth);
                            //當(dāng)前列+1
                            currentColumn++;
    
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    }
    
    
  2. Two

    import com.lzj.entity.User;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.xssf.usermodel.*;
    
    import java.io.*;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     * <p>
     *
     * </p>
     *
     * @author:雷子杰
     * @date:2023/7/4
     */
    public class Two {
        public static void main(String[] args) throws IOException {
            List<User> userList = new ArrayList<>();
            userList.add(new User("lzj1",1,1,new Date()));
            userList.add(new User("lzj2",2,0,new Date()));
            userList.add(new User("lzj3",3,1,new Date()));
            userList.add(new User("lzj4",4,0,new Date()));
    
            //聲明XSSF對象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
            //創(chuàng)建sheet
            XSSFSheet userSheet = xssfWorkbook.createSheet("user");
    
            //創(chuàng)建標(biāo)題字體
            XSSFFont titleFont = PoiExcelUtil.getTitleFont(xssfWorkbook);
            //創(chuàng)建通用字體
            XSSFFont commonFont = PoiExcelUtil.getCommonFont(xssfWorkbook);
            // 創(chuàng)建標(biāo)題行單元格樣式
            CellStyle titleCellStyle = PoiExcelUtil.getTitleCellStyle(xssfWorkbook,titleFont);
            //創(chuàng)建通用行單元格樣式
            CellStyle commonCellStyle = PoiExcelUtil.getCommonCellStyle(xssfWorkbook,commonFont);
            //獲取實體類中全部屬性
            Field[] fields = User.class.getDeclaredFields();
            //當(dāng)前行
            int currentRow = 0;
            //當(dāng)前列
            int currentColumn = 0;
            //行高
            float rowHeight = 40.1f;
            //列寬
            int columnWidth = 33 * 256;
            //創(chuàng)建行
            XSSFRow row = userSheet.createRow(currentRow);
            //創(chuàng)建標(biāo)題行
            PoiExcelUtil.writeTitleCellData(row,userSheet,titleCellStyle,fields,currentColumn,rowHeight,columnWidth,"b");
            //重置當(dāng)前列
            currentColumn = 0;
            //創(chuàng)建數(shù)據(jù)行
            PoiExcelUtil.writeCommonRowCellData(userList,currentRow,currentColumn,row,userSheet,commonCellStyle,fields,rowHeight,columnWidth,"b");
    
    
            // 將生成的excel文件輸出流轉(zhuǎn)為字節(jié)數(shù)組
            byte[] bytes = null;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            xssfWorkbook.write(outputStream);
            outputStream.close();
            bytes = outputStream.toByteArray();
    
            //讀取字節(jié)數(shù)組為文件輸入流
            InputStream inputStream = new ByteArrayInputStream(bytes);
            inputStream.close();
    
    
            //在聲明一個輸出流將文件下載到本地
            File file = new File("C:\\Users\\86158\\Desktop\\zzzzzz.xlsx");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            //將bytes中的內(nèi)容寫入
            bufferedOutputStream.write(bytes);
            //刷新輸出流,否則不會寫出數(shù)據(jù)
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
    
        }
    }
    

結(jié)果展示

這是我初始化時的數(shù)據(jù)

java中使用POI生成Excel并導(dǎo)出,java,python,開發(fā)語言

下面的是我type參數(shù)不同時的數(shù)據(jù),均是以版本2來進行的寫入導(dǎo)出。type參數(shù)修改位置如下:

java中使用POI生成Excel并導(dǎo)出,java,python,開發(fā)語言

type參數(shù)為a

java中使用POI生成Excel并導(dǎo)出,java,python,開發(fā)語言

type參數(shù)為b

java中使用POI生成Excel并導(dǎo)出,java,python,開發(fā)語言

總結(jié)

在項目開發(fā)過程中總會遇到各式各樣的問題,只有不斷的學(xué)習(xí),不斷的積累,自身水平才能提高。文章來源地址http://www.zghlxwxcb.cn/news/detail-586128.html

到了這里,關(guān)于java中使用POI生成Excel并導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包