欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2025年03月31日 09:44:18   作者:愛(ài)學(xué)習(xí)的大雄  
這篇文章主要介紹了java中使用POI生成Excel并導(dǎo)出過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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

需求說(shuō)明及實(shí)現(xiàn)方式

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

  • 我的想法是通過(guò)在實(shí)體屬性上寫自定義注解的方式去完成。因?yàn)槲覀冊(cè)诖a中可以通過(guò)反射的方式去獲取實(shí)體類中全部的注解及屬性名稱等等。
  • 我們可以在自定義注解中聲明一個(gè)參數(shù)value,這里面就存儲(chǔ)其標(biāo)題,這樣我們

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

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

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

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

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

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

本來(lái)我是想著通過(guò)easyExcel來(lái)完成這些功能,但是由于項(xiàng)目中已經(jīng)引入了POI的3.9版本依賴,然后easyExcel中POI的依賴版本又高于該版本,而且不管是版本升級(jí)還是版本排除降級(jí),總會(huì)有一個(gè)出現(xiàn)問(wèn)題,最終也只能通過(guò)最基礎(chǔ)的POI編寫代碼實(shí)現(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>

通用代碼

1.ExcelExport

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

2.User

  • @ExcelExport:即自定義的注解,其中值的含義在上面已經(jīng)說(shuō)清楚了
@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中未實(shí)現(xiàn)數(shù)據(jù)查詢type不同,則顯示的標(biāo)題不同(同一個(gè)字段)這一功能,如需要加請(qǐng)看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對(duì)象
        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);//水平對(duì)齊方式
        titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直對(duì)齊方式
        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);//自動(dòng)換行

        //獲取實(shí)體類中全部屬性
        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)題行
        // 遍歷每個(gè)字段
        for (Field field : fields) {
            // 檢查字段是否帶有Explanation注解
            if (field.isAnnotationPresent(ExcelExport.class)) {
                // 獲取Explanation注解實(shí)例
                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)時(shí)重置列
            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注解實(shí)例
                        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);
                        }
                        //獲取對(duì)應(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();


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


    }
}

版本2

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

  • 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);//水平對(duì)齊方式
        titleCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直對(duì)齊方式
        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);//自動(dòng)換行

        return commonCellStyle;
    }

    /**
     * 寫入單個(gè)單元格數(shù)據(jù)
     * @param row 行對(duì)象
     * @param xssfSheet sheet對(duì)象
     * @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 行對(duì)象
     * @param xssfSheet sheet對(duì)象
     * @param cellStyle 單元格樣式
     * @param fields 反射獲取得到的實(shí)體對(duì)象的全部屬性
     * @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)題行
        // 遍歷每個(gè)字段
        for (Field field : fields) {
            // 檢查字段是否帶有ExcelExport注解
            if (field.isAnnotationPresent(ExcelExport.class)) {
                // 獲取Explanation注解實(shí)例
                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ù),則給他對(duì)應(yīng)對(duì)應(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 行對(duì)象
     * @param xssfSheet sheet對(duì)象
     * @param cellStyle 單元格樣式
     * @param fields 反射獲取得到的實(shí)體對(duì)象的全部屬性
     * @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)時(shí)重置列
            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注解實(shí)例
                        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);
                        }
                        //獲取對(duì)應(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();
                    }
                }
            }
        }
    }

}
  • 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對(duì)象
        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);
        //獲取實(shí)體類中全部屬性
        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();


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

    }
}

結(jié)果展示

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

下面的是我type參數(shù)不同時(shí)的數(shù)據(jù),均是以版本2來(lái)進(jìn)行的寫入導(dǎo)出。

type參數(shù)修改位置如下:

type參數(shù)為a

type參數(shù)為b

總結(jié)

在項(xiàng)目開(kāi)發(fā)過(guò)程中總會(huì)遇到各式各樣的問(wèn)題,只有不斷的學(xué)習(xí),不斷的積累,自身水平才能提高。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java結(jié)構(gòu)型設(shè)計(jì)模式中代理模式示例詳解

    Java結(jié)構(gòu)型設(shè)計(jì)模式中代理模式示例詳解

    代理模式(Proxy Parttern)為一個(gè)對(duì)象提供一個(gè)替身,來(lái)控制這個(gè)對(duì)象的訪問(wèn),即通過(guò)代理對(duì)象來(lái)訪問(wèn)目標(biāo)對(duì)象。本文將通過(guò)示例詳細(xì)講解一下這個(gè)模式,需要的可以參考一下
    2022-09-09
  • Java異常處理原理與用法實(shí)例分析

    Java異常處理原理與用法實(shí)例分析

    這篇文章主要介紹了Java異常處理原理與用法,結(jié)合實(shí)例形式分析了Java異常處理相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • 如何利用Stream改變list中特定對(duì)象的某一屬性

    如何利用Stream改變list中特定對(duì)象的某一屬性

    這篇文章主要介紹了如何利用Stream改變list中特定對(duì)象的某一屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • springboot整合RabbitMQ 中的 TTL實(shí)例代碼

    springboot整合RabbitMQ 中的 TTL實(shí)例代碼

    TTL 是 RabbitMQ 中一個(gè)消息或者隊(duì)列的屬性,表明一條消息或者該隊(duì)列中的所有消息的最大存活時(shí)間,單位是毫秒,這篇文章主要介紹了springboot整合RabbitMQ 中的 TTL,需要的朋友可以參考下
    2022-09-09
  • JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例

    JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例

    這篇文章主要介紹了JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下
    2013-11-11
  • Java中實(shí)現(xiàn)二叉樹(shù)的遍歷與重構(gòu)

    Java中實(shí)現(xiàn)二叉樹(shù)的遍歷與重構(gòu)

    這篇文章主要介紹了Java中實(shí)現(xiàn)二叉樹(shù)的遍歷與重構(gòu),樹(shù)是一種非線性的數(shù)據(jù)結(jié)構(gòu),它是由n(n>=0)個(gè)有限結(jié)點(diǎn)組成一個(gè)具有層次關(guān)系的集合,把它叫做樹(shù)是因?yàn)樗雌饋?lái)像一棵倒掛的樹(shù),也就是說(shuō)它是根朝上,而葉朝下的,需要的朋友可以參考下
    2023-10-10
  • springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法

    springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法

    這篇文章主要介紹了springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Java Swing中的文本框(JTextField)與文本區(qū)(JTextArea)使用實(shí)例

    Java Swing中的文本框(JTextField)與文本區(qū)(JTextArea)使用實(shí)例

    這篇文章主要介紹了Java Swing中的文本框(JTextField)與文本區(qū)(JTextArea)使用實(shí)例,Swing是一個(gè)用于開(kāi)發(fā)Java應(yīng)用程序用戶界面的開(kāi)發(fā)工具包,需要的朋友可以參考下
    2014-10-10
  • Java實(shí)現(xiàn)優(yōu)雅停止線程的有效方法詳解

    Java實(shí)現(xiàn)優(yōu)雅停止線程的有效方法詳解

    這篇文章主要為大家詳細(xì)如何安全有效停止 Java 線程的,確保多線程應(yīng)用程序平穩(wěn)運(yùn)行并實(shí)現(xiàn)最佳資源管理,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)

    SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)

    本文主要介紹了SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論