国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

java實(shí)現(xiàn)導(dǎo)出excel文件

王林
發(fā)布: 2020-10-27 17:14:35
轉(zhuǎn)載
3217人瀏覽過(guò)

java實(shí)現(xiàn)導(dǎo)出excel文件

實(shí)現(xiàn)方法如下:

(視頻教程推薦:java課程

1、首先新建一個(gè)SpringBoot項(xiàng)目

2、導(dǎo)入依賴(lài)–pom.xml

立即學(xué)習(xí)Java免費(fèi)學(xué)習(xí)筆記(深入)”;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.briup</groupId>
	<artifactId>demo3</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>demo3</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.6</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
登錄后復(fù)制

3、建各種類(lèi)

新建實(shí)體類(lèi)

記得添加get/set方法

public class User {
	
	private String username;
	private String email;
	private String createTime;
	private String LastLoginTime;
	private String roleName;
	private String enable;
	public User() {
		super();
	}
}
登錄后復(fù)制

新建接口Service

import java.util.List;

public interface UserService {
	public List<User> findAllUser();
}
登錄后復(fù)制

新建實(shí)現(xiàn)Service接口的Impl

import java.util.List;

public class UserServiceImpl implements UserService {
	@Override
	public List<User> findAllUser() {
		User user = new User();
		return null;
	}

}
登錄后復(fù)制

新建ExcelUtil工具類(lèi)

import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelUtil {
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String sheetName1,String sheetName2, String []title, String[]  content,String[] app){
    	 
        // 第一步,創(chuàng)建一個(gè)HSSFWorkbook,對(duì)應(yīng)一個(gè)Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
 
        // 第二步,在workbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);
        HSSFSheet sheet1 = wb.createSheet(sheetName1);
        HSSFSheet sheet2 = wb.createSheet(sheetName2);
 
        // 第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)Excel的行數(shù)列數(shù)有限制
        HSSFRow row = sheet.createRow(0);
        HSSFRow row1 = sheet1.createRow(0);
        HSSFRow row2 = sheet2.createRow(0);
 
        // 第四步,創(chuàng)建單元格樣式,并設(shè)置值表頭 設(shè)置表頭居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創(chuàng)建一個(gè)居中格式
 
        //聲明單元格
        HSSFCell cell = null;
 
        //創(chuàng)建標(biāo)題
        for(int i=0;i<title.length;i++){
            //創(chuàng)建一個(gè)單元格
            cell = row.createCell(i);
            //給單元格賦值
            cell.setCellValue(title[i]);
            //給單元格設(shè)置樣式
            cell.setCellStyle(style);
        }
        //創(chuàng)建標(biāo)題
        for(int i=0;i<title.length;i++){
            //創(chuàng)建一個(gè)單元格
            cell = row1.createCell(i);
            //給單元格賦值
            cell.setCellValue(title[i]);
            //給單元格設(shè)置樣式
            cell.setCellStyle(style);
        }
 
        //創(chuàng)建內(nèi)容
        if (content != null && content.length > 0){
        	for(int i=0;i<content.length;i++){
        		row = sheet.createRow(i + 1);
        		for(int j=0;j<content.length;j++){
                    //將內(nèi)容按順序賦給對(duì)應(yīng)的列對(duì)象
                    row.createCell(j).setCellValue(content[j]);
                }

            }
        }

        if (content != null && content.length > 0){
        	for(int i=0;i<content.length;i++){
        		row1 = sheet1.createRow(i + 1);
        		for(int j=0;j<content.length;j++){
                    //將內(nèi)容按順序賦給對(duì)應(yīng)的列對(duì)象
                    row1.createCell(j).setCellValue(content[j]);
                }

            }
        }
        if (app != null && app.length > 0){
        	for(int i=0;i<app.length;i++){
        		row2 = sheet2.createRow(i + 1);
        		for(int j=0;j<app.length;j++){
                    //將內(nèi)容按順序賦給對(duì)應(yīng)的列對(duì)象
                    row2.createCell(j).setCellValue(app[j]);
                }
            }
        }
        return wb;
    }
}
登錄后復(fù)制

新建Controller類(lèi)

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/MyTest")
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public void export(@RequestBody(required = false) User user,String username,HttpServletResponse response) throws Exception {
    	
        if (user ==null && !StringUtils.isEmpty(username)){
            //GET 請(qǐng)求的參數(shù)
            user = new User();
            user.setUsername(username);
        }
        UserService userService = new UserServiceImpl();
		//獲取數(shù)據(jù)
        List<User> list = userService.findAllUser();
       
        //excel標(biāo)題
        String[] title = {"姓名", "郵箱", "創(chuàng)建時(shí)間", "最近登錄時(shí)間","角色","是否可用"};
 
        //excel文件名
        String fileName = System.currentTimeMillis() + ".xls";
 
        //sheet名
        String sheetName = "用戶信息";
        String sheetName1 = "hello";
        String sheetName2 = "xixi";
 
        //沒(méi)有數(shù)據(jù)就傳入null吧,Excel工具類(lèi)有對(duì)null判斷
        String[] content= {"ali","aaa","ddd","aaa","aaa","aaaa"};
        String[] app= {"bbbb","bbbb","bbbb","bbbb","bbbb","bbbb",};
        if (list != null && list.size() > 0){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < list.size(); i++) {
                User obj = list.get(i);
                
                content[1] = obj.getUsername();
                content[1] = obj.getEmail();
                content[2] = obj.getCreateTime() == null ? "" : sdf.format(obj.getCreateTime());
                content[3] = obj.getLastLoginTime() == null ? "": sdf.format(obj.getLastLoginTime());
                content[4] = obj.getRoleName();
            }
        }
        if (list != null && list.size() > 0){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (int i = 0; i < list.size(); i++) {
                User obj = list.get(i);
                
                app[1] = obj.getUsername();
                app[1] = obj.getEmail();
                app[2] = obj.getCreateTime() == null ? "" : sdf.format(obj.getCreateTime());
                app[3] = obj.getLastLoginTime() == null ? "": sdf.format(obj.getLastLoginTime());
                app[4] = obj.getRoleName();
            }
        }
 
        //創(chuàng)建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,sheetName1,sheetName2, title, content,app);
//        HSSFWorkbook wb1 = ExcelUtil.getHSSFWorkbook(sheetName1, title, content);
 
        //響應(yīng)到客戶端
        try {
            fileName = new String(fileName.getBytes(), "UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
登錄后復(fù)制

設(shè)置application.properties
server.port=8081
最重要的一定要注意:Application類(lèi)一定要在最外側(cè)的包中?。?!

4、最后訪問(wèn)

localhost:8081/MyTest/hello

結(jié)果:

98d20ee61fe318631c2920080984855.png

沒(méi)有寫(xiě)前端,可以寫(xiě)一個(gè)html,設(shè)置一個(gè)a標(biāo)簽,點(diǎn)擊事件。

相關(guān)推薦:java入門(mén)

以上就是java實(shí)現(xiàn)導(dǎo)出excel文件的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

WPS零基礎(chǔ)入門(mén)到精通全套教程!
WPS零基礎(chǔ)入門(mén)到精通全套教程!

全網(wǎng)最新最細(xì)最實(shí)用WPS零基礎(chǔ)入門(mén)到精通全套教程!帶你真正掌握WPS辦公! 內(nèi)含Excel基礎(chǔ)操作、函數(shù)設(shè)計(jì)、數(shù)據(jù)透視表等

下載
相關(guān)標(biāo)簽:
來(lái)源:csdn網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn
最新問(wèn)題
開(kāi)源免費(fèi)商場(chǎng)系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)