Excel导出实例

导出

<!--poi-->
        <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>
package com.zyf.mall.tiny.test1;

import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


public class CreateExcel {

    public static void main(String[] args) throws IOException {
        generateExcel();
    }

    public static void generateExcel() throws IOException {
        //创建工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建表单
        XSSFSheet sheet = genSheet(workbook,"testExcel");
        //创建表单样式
        XSSFCellStyle titleStyle = genTitleStyle(workbook);//创建标题样式
        XSSFCellStyle contextStyle = genContextStyle(workbook);//创建文本样式

        //创建Excel
        genExcel(sheet,titleStyle,contextStyle);

        //将生成的Excel文件保存到本地
        FileOutputStream out = new FileOutputStream(new File("D://zhangyunfei/test.xlsx"));
        //将工作薄写入文件输出流中
        workbook.write(out);
        //文本文件输出流,释放资源
        out.close();
    }

    public static void genExcel(XSSFSheet sheet,XSSFCellStyle titleStyle,XSSFCellStyle contextStyle) {

        //根据Excel列名长度,指定列名宽度  Excel总共10列
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                sheet.setColumnWidth(i, 1500);
            } else if (i == 2 || i == 3 || i == 4) {
                sheet.setColumnWidth(i, 4000);
            } else if (i == 9) {
                sheet.setColumnWidth(i, 3000);
            } else {
                sheet.setColumnWidth(i, 2000);
            }
        }

        //设置标题位置
        sheet.addMergedRegion(new CellRangeAddress(
                0, //first row
                0, //last row
                0, //first column
                11 //last column
        ));

        XSSFRow row = sheet.createRow(0);//创建第一行,为标题,index从0开始
        XSSFCell cell;
        cell = row.createCell(0);//创建一列
        cell.setCellValue("xxx 幼儿园一年级二班学生信息");//标题
        cell.setCellStyle(titleStyle);//设置标题样式

        /**
         * 这里可以是数据库所对应的字段 如果字段比较少 记得更改设置标题位置的  lastCol
         */
        row = sheet.createRow(1);//创建第二行
        cell = row.createCell(0);//创建第二行第一列
        cell.setCellValue("姓名");//第二行第一列内容
        cell.setCellStyle(contextStyle);//设置样式
        cell = row.createCell(1);//2 row 1 column
        cell.setCellValue("性别");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(2);//2 row 2 column
        cell.setCellValue("学号");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(3);//2 row 3 column
        cell.setCellValue("出生年月");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(4);//2 row 4 column
        cell.setCellValue("家庭住址");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(5);//2 row 5 column
        cell.setCellValue("语文");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(6);//2 row 6 column
        cell.setCellValue("英语");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(7);//2 row 7 column
        cell.setCellValue("数学");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(8);//2 row 8 column
        cell.setCellValue("自然");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(9);//2 row 9 column
        cell.setCellValue("科学");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(10);//2 row 9 column
        cell.setCellValue("生物");
        cell.setCellStyle(contextStyle);
        cell = row.createCell(11);//2 row 9 column
        cell.setCellValue("生物1");
        cell.setCellStyle(contextStyle);
		/*
			实际填充数据的时候,对可能为空的数据要进行处理,要先进行判断,否则报错
			String value = Test.getVal();
			if(StringUtils.isNotBlank(value)) {
				cell.setCellValue(value);
			} else{
			    cell.setCellValue(" ");
			}
		*/
        //从数据库取数据填充到Excel,这步省略,添加模拟数据
        //这里i<10其实 就是的到的集合的长度
        for(int i = 2 ; i<10;i++){//i从2开始计数,因为上面已经创建了 0 1行
            row=sheet.createRow(i);//创建第三行
            cell=row.createCell(0);//创建第三行第一列
            cell.setCellValue("小明");//第三行第一列的值
            cell.setCellStyle(contextStyle);//设置样式
            cell=row.createCell(1);//2 row 1 column
            cell.setCellValue("男");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(2);//2 row 2 column
            cell.setCellValue("NO20180901"+i);
            cell.setCellStyle(contextStyle);
            cell=row.createCell(3);//2 row 3 column
            cell.setCellValue("2000/02/01");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(4);//2 row 4 column
            cell.setCellValue("成都市天府广场");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(5);//2 row 5 column
            cell.setCellValue("90");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(6);//2 row 6 column
            cell.setCellValue("90");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(7);//2 row 7 column
            cell.setCellValue("90");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(8);//2 row 8 column
            cell.setCellValue("98");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(9);//2 row 9 column
            cell.setCellValue("100");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(10);//2 row 9 column
            cell.setCellValue("100");
            cell.setCellStyle(contextStyle);
            cell=row.createCell(11);//2 row 9 column
            cell.setCellValue("100");
            cell.setCellStyle(contextStyle);
        }
    }

    //设置表单,并生成表单
    public static XSSFSheet genSheet(XSSFWorkbook workbook, String sheetName){
        //生成表单
        XSSFSheet sheet = workbook.createSheet(sheetName);
        //设置表单文本居中
        sheet.setHorizontallyCenter(true);
        sheet.setFitToPage(false);
        //打印时在底部右边显示文本页信息
        Footer footer = sheet.getFooter();
        footer.setRight( "Page " + HeaderFooter.numPages()+ " Of "+ HeaderFooter.page());
        //打印时在头部右边显示Excel创建日期信息
        Header header = sheet.getHeader();
        header.setRight("Create Date " + HeaderFooter.date() + " " + HeaderFooter.time());
        //设置打印方式
        XSSFPrintSetup ps = sheet.getPrintSetup();
        ps.setLandscape(true); // true:横向打印,false:竖向打印 ,因为列数较多,推荐在打印时横向打印
        ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //打印尺寸大小设置为A4纸大小
        return sheet;
    }

    //创建文本样式
    public static XSSFCellStyle genContextStyle(XSSFWorkbook workbook){
        XSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);//文本水平居中显示
        style.setVerticalAlignment(VerticalAlignment.CENTER);//文本竖直居中显示
        style.setWrapText(true);//文本自动换行

        //生成Excel表单,需要给文本添加边框样式和颜色
        /*
             CellStyle.BORDER_DOUBLE      双边线
             CellStyle.BORDER_THIN        细边线
             CellStyle.BORDER_MEDIUM      中等边线
             CellStyle.BORDER_DASHED      虚线边线
             CellStyle.BORDER_HAIR        小圆点虚线边线
             CellStyle.BORDER_THICK       粗边线
         */
        style.setBorderBottom(BorderStyle.THIN);//设置文本边框
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        //set border color
        style.setTopBorderColor(new XSSFColor(Color.BLACK));//设置文本边框颜色
        style.setBottomBorderColor(new XSSFColor(Color.BLACK));
        style.setLeftBorderColor(new XSSFColor(Color.BLACK));
        style.setRightBorderColor(new XSSFColor(Color.BLACK));

        return style;
    }

    //生成标题样式
    public static XSSFCellStyle genTitleStyle(XSSFWorkbook workbook){

        XSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setWrapText(true);

        //标题居中,没有边框,所以这里没有设置边框,设置标题文字样式
        XSSFFont titleFont = workbook.createFont();
        titleFont.setBold(true);//加粗
        titleFont.setFontHeight((short)15);//文字尺寸
        titleFont.setFontHeightInPoints((short)15);
        style.setFont(titleFont);

        return style;
    }


}


导入

package com.zyf.mall.tiny.test1;

import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
import java.io.IOException;


public class TestExcel1 {
//    public static void main(String[] args) {
//        try {
//            //创建工作簿
//            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\zhangyunfei\\zhanhui.xlsx"));
//            System.out.println("xssfWorkbook对象:" + xssfWorkbook);
//            //读取第一个工作表(这里的下标与list一样的,从0开始取,之后的也是如此)
//            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
//            System.out.println("sheet对象:" + sheet);
//            //获取第一行的数据
//            XSSFRow row = sheet.getRow(0);
//            System.out.println("row对象:" + row);
//            //获取该行第一个单元格的数据
//            XSSFCell cell0 = row.getCell(0);
//            System.out.println("cello对象:" + cell0);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static void main(String[] args) {
        try {
            //创建工作簿
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:\\zhangyunfei\\test.xlsx"));
            System.out.println("xssfWorkbook对象:" + xssfWorkbook);
            //读取第一个工作表
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            System.out.println("sheet对象:" + sheet);
//      //获取最后一行的num,即总行数。此处从0开始计数
            int maxRow = sheet.getLastRowNum();
            System.out.println("总行数为:" + maxRow);
            for (int row = 0; row < maxRow; row++) {
                //获取最后单元格num,即总单元格数 ***注意:此处从0开始计数***
                XSSFRow row1 = sheet.getRow(row);
                short lastCellNum = 0;
                if(row1!=null){
                     lastCellNum = row1.getLastCellNum();
                }else continue;
                System.out.println("--------第" + row + "行的数据如下--------");
                for (int rol = 0; rol < lastCellNum; rol++){
                    System.out.print(sheet.getRow(row).getCell(rol) + "  ");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zyf_fly66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值