Search in sources :

Example 1 with ExcelRow

use of com.fruit.manage.util.excel.ExcelRow in project fruit-manage by liuzhaozhao.

the class ExcelCommon method createExcelModul.

/**
 * @param path     保存的路径+文件名
 * @param fileName 保存的路径+文件名
 * @param title    标题
 * @param createBy 创建者
 * @param header   行头
 * @param listData 要导出的数据
 * @return 保存地址
 * @throws ExcelException 异常
 */
public static String createExcelModul(String path, String fileName, String title, String createBy, String[] header, List<Object[]> listData) throws ExcelException {
    // 判断文件夹是否存在
    if (!new File(path).exists() || !new File(path).isDirectory()) {
        new File(path).mkdirs();
    }
    String savePath = path + File.separator + fileName;
    Excel excel = new Excel();
    if (StrKit.notBlank(title)) {
        excel.setTitle(title);
    }
    if (!StrKit.notBlank(savePath)) {
        throw new ExcelException("保存路径不能为空");
    } else {
        excel.setSavePath(savePath);
    }
    if (StrKit.notBlank(createBy)) {
        excel.setCreateBy(createBy);
    }
    if (StrKit.notBlank(header)) {
        excel.setHeader(header);
    }
    for (Object[] dataRow : listData) {
        ExcelRow row = excel.createRow();
        for (Object dataCell : dataRow) {
            row.addCell(dataCell);
        }
    }
    try {
        return excel.CreateXlsx();
    } catch (IOException e) {
        return null;
    }
}
Also used : Excel(com.fruit.manage.util.excel.Excel) ExcelException(com.fruit.manage.util.excel.ExcelException) IOException(java.io.IOException) File(java.io.File) ExcelRow(com.fruit.manage.util.excel.ExcelRow)

Example 2 with ExcelRow

use of com.fruit.manage.util.excel.ExcelRow in project fruit-manage by liuzhaozhao.

the class ExcelTest method excel.

public static Excel excel(String savePath) {
    System.out.println("数据准备:" + sdf.format(new Date()));
    Excel excel = new Excel();
    excel.setTitle("标题");
    excel.setCreateBy("虾米");
    excel.setDateFrom("2017-07-01");
    excel.setDateTo("2017-07-12");
    excel.setSavePath(savePath);
    String[] header = { "序号", "日期", "时间", "数字", "row合并", "col合并1", "col合并2", "超长文字自动换行" };
    excel.setHeader(header);
    for (int i = 0; i < 120; i++) {
        ExcelRow row = excel.createRow();
        // 序号
        row.addCell(i + 1);
        // 日期
        row.addCell(new java.sql.Date(new Date().getTime()));
        // 时间
        row.addCell(new Date());
        // 数字
        row.addCell(12.1222);
        if (// row合并
        i % 3 == 0)
            row.addCell("row合并", 1, 3);
        // col合并
        row.addCell("col合并", 2, 1);
        // 超长文字自动换行
        row.addCell("超长文字自动换行,靠左边,超长文字自动换行,靠左边,超长文字自动换行,超长文字自动换行,靠左边,超长文字自动换行,靠左边,超长文字自动换行,靠左边,超长文字自动换行,靠左边", ExcelUtil.ALIGN_LEFT);
    }
    System.out.println("数据准备完成,准备生成excel:" + sdf.format(new Date()));
    return excel;
}
Also used : Excel(com.fruit.manage.util.excel.Excel) ExcelRow(com.fruit.manage.util.excel.ExcelRow) Date(java.util.Date)

Aggregations

Excel (com.fruit.manage.util.excel.Excel)2 ExcelRow (com.fruit.manage.util.excel.ExcelRow)2 ExcelException (com.fruit.manage.util.excel.ExcelException)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1