ExcelWrite excel创建导出


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 
 * @author admin
 *
 */
public class ExcelWrite {
    
    private static XSSFWorkbook workbook = null;

    /**
     * 判断文件是否存在.
     * 
     * @param fileDir 文件路径
     * @return
     */
    public static boolean fileExist(String fileDir) {
        boolean flag = false;
        File file = new File(fileDir);
        flag = file.exists();
        return flag;
    }

    /**
     * 判断文件的sheet是否存在.
     * 
     * @param fileDir   文件路径
     * @param sheetName 表格索引名
     * @return
     */
    public static boolean sheetExist(String fileDir, String sheetName) throws Exception {
        boolean flag = false;
        File file = new File(fileDir);
        if (file.exists()) { // 文件存在
            // 创建workbook
            try {
                workbook = new XSSFWorkbook(new FileInputStream(file));
                // 添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
                XSSFSheet sheet = workbook.getSheet(sheetName);
                if (sheet != null)
                    flag = true;
            } catch (Exception e) {
                throw e;
            }

        } else { // 文件不存在
            flag = false;
        }
        return flag;
    }

    /**
     * 创建新excel.
     * 
     * @param fileDir   excel的路径
     * @param sheetName 要创建的表格索引
     * @param titleRow  excel的第一行即表格头
     */
    @SuppressWarnings("unused")
    public static void createExcel(String fileDir, String sheetName, String titleRow[]) throws Exception {
        // 创建workbook
        workbook = new XSSFWorkbook();
        // 添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
        XSSFSheet sheet1 = workbook.createSheet(sheetName);
        // 新建文件
        FileOutputStream out = null;
        try {
            // 添加表头
            XSSFRow row = workbook.getSheet(sheetName).createRow(0); // 创建第一行
            for (short i = 0; i < titleRow.length; i++) {
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(titleRow[i]);
            }
            out = new FileOutputStream(fileDir);
            workbook.write(out);
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除文件.
     * 
     * @param fileDir 文件路径
     */
    public static boolean deleteExcel(String fileDir) {
        boolean flag = false;
        File file = new File(fileDir);
        // 判断目录或文件是否存在
        if (!file.exists()) { // 不存在返回 false
            return flag;
        } else {
            // 判断是否为文件
            if (file.isFile()) { // 为文件时调用删除文件方法
                file.delete();
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 往excel中写入(已存在的数据无法写入).
     * 
     * @param fileDir   文件路径
     * @param sheetName 表格索引
     * @param object
     * @throws Exception
     */
    public static void writeToExcel(String fileDir, String sheetName, List<Map<Object, Object>> mapList)
            throws Exception {
        // 创建workbook
        File file = new File(fileDir);
        try {
            workbook = new XSSFWorkbook(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 流
        FileOutputStream out = null;
        XSSFSheet sheet = workbook.getSheet(sheetName);
        // 获取表格的总行数
        // int rowCount = sheet.getLastRowNum() + 1; // 需要加一
        // 获取表头的列数
        int columnCount = sheet.getRow(0).getLastCellNum();
        try {
            // 获得表头行对象
            XSSFRow titleRow = sheet.getRow(0);
            if (titleRow != null) {
                for (int rowId = 0; rowId < mapList.size(); rowId++) {
                    Map<Object, Object> map = mapList.get(rowId);
                    XSSFRow newRow = sheet.createRow(rowId + 1);
                    for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) { // 遍历表头
                        String mapKey = titleRow.getCell(columnIndex).toString().trim().toString().trim();
                        XSSFCell cell = newRow.createCell(columnIndex);
                        cell.setCellValue(map.get(mapKey) == null ? null : map.get(mapKey).toString());
                    }
                }
            }

            out = new FileOutputStream(fileDir);
            workbook.write(out);
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

转载于:https://my.oschina.net/u/4122764/blog/3055356

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值