Search in sources :

Example 36 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project notes by menhuan.

the class OnirigiFrontApp1liactionTest method test1.

@Test
public void test1() throws FileNotFoundException {
    OutputStream out = new FileOutputStream("F:/vscode/78.xlsx");
    try {
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        // 写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
        Sheet sheet1 = new Sheet(1, 0, CustomerMessage.class);
        writer.write(getData(), sheet1);
        writer.finish();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ExcelWriter(com.alibaba.excel.ExcelWriter) IOException(java.io.IOException) Sheet(com.alibaba.excel.metadata.Sheet) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 37 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project SpringBoot-Hello by ruiyeclub.

the class ExcelUtil method writeExcel.

/**
 * @param outputStream  Excel的输出流
 * @param data          要写入的以 模型 为单位的数据
 * @param clazz         模型的类
 * @param excelTypeEnum Excel的格式(XLS或XLSX)
 * @Description: 使用模型来写入Excel,单sheet,单table
 * @Date: 2020/1/16 21:43
 * @Return: byte[]
 * @Throws: Exception
 */
public static void writeExcel(OutputStream outputStream, List<? extends BaseRowModel> data, Class<? extends BaseRowModel> clazz, ExcelTypeEnum excelTypeEnum) throws Exception {
    // 这里指定需要表头,因为model通常包含表头信息
    ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum, true);
    // 写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
    Sheet sheet1 = new Sheet(1, 0, clazz);
    writer.write(data, sheet1);
    writer.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) Sheet(com.alibaba.excel.metadata.Sheet)

Example 38 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project SpringBoot-Hello by ruiyeclub.

the class ExcelUtil method writeExcel.

/**
 * @param outputStream  Excel的输出流
 * @param sheetName     sheet名集合
 * @param datas         要写入的以 模型 为单位的数据
 * @param clazzs        模型的类
 * @param excelTypeEnum Excel的格式(XLS或XLSX)
 * @Description: 使用模型来写入Excel,多sheet,单table (返回字节数组)
 * @Date: 2020/1/16 21:43
 * @Return: byte[]
 * @Throws: Exception
 */
public static byte[] writeExcel(ByteArrayOutputStream outputStream, List<String> sheetName, List<List<? extends BaseRowModel>> datas, List<Class<? extends BaseRowModel>> clazzs, ExcelTypeEnum excelTypeEnum) throws Exception {
    // 这里指定需要表头,因为model通常包含表头信息
    ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum, true);
    if (sheetName.size() != datas.size() || datas.size() != clazzs.size()) {
        throw new ArrayIndexOutOfBoundsException();
    }
    int i = 0;
    // 写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
    for (String name : sheetName) {
        Sheet sheet1 = new Sheet(1, 0, clazzs.get(i));
        sheet1.setSheetName(name);
        writer.write(datas.get(i), sheet1);
    }
    writer.finish();
    return outputStream.toByteArray();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) Sheet(com.alibaba.excel.metadata.Sheet)

Example 39 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project SpringBoot-Hello by ruiyeclub.

the class ExcelUtil method writeExcel.

/**
 * @param outputStream  Excel的输出流
 * @param data          要写入的以StringList为单位的数据
 * @param table         配置Excel的表的属性
 * @param excelTypeEnum Excel的格式(XLS或XLSX)
 * @Description: 使用StringList来写入Excel,单sheet,单table
 * @Date: 2020/1/16 21:42
 * @Return: void
 * @Throws: Exception
 */
public static void writeExcel(OutputStream outputStream, List<List<String>> data, Table table, ExcelTypeEnum excelTypeEnum) throws Exception {
    // 这里指定不需要表头,因为String通常表头已被包含在data里
    ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum, false);
    // 写第一个sheet, sheet1  数据全是List<String> 无模型映射关系,无表头
    Sheet sheet1 = new Sheet(0, 0);
    writer.write0(data, sheet1, table);
    writer.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) Sheet(com.alibaba.excel.metadata.Sheet)

Example 40 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project SpringBoot-Hello by ruiyeclub.

the class ExcelUtil method writeExcel.

/**
 * @param outputStream  Excel的输出流
 * @param data          要写入的以StringList为单位的数据
 * @param table         配置Excel的表的属性
 * @param excelTypeEnum Excel的格式(XLS或XLSX)
 * @Description: 使用StringList来写入Excel,单sheet,单table(返回byte数组)
 * @Date: 2020/1/16 21:43
 * @Return: byte[]
 * @Throws: Exception
 */
public static byte[] writeExcel(ByteArrayOutputStream outputStream, List<List<String>> data, Table table, ExcelTypeEnum excelTypeEnum) throws Exception {
    // 这里指定不需要表头,因为String通常表头已被包含在data里
    ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum, false);
    // 写第一个sheet, sheet1  数据全是List<String> 无模型映射关系,无表头
    Sheet sheet1 = new Sheet(0, 0);
    writer.write0(data, sheet1, table);
    writer.finish();
    return outputStream.toByteArray();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) Sheet(com.alibaba.excel.metadata.Sheet)

Aggregations

ExcelWriter (com.alibaba.excel.ExcelWriter)67 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)52 Test (org.junit.Test)31 Sheet (com.alibaba.excel.metadata.Sheet)13 HashMap (java.util.HashMap)13 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)11 List (java.util.List)9 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)7 WriteTable (com.alibaba.excel.write.metadata.WriteTable)6 ArrayList (java.util.ArrayList)6 ExcelReader (com.alibaba.excel.ExcelReader)5 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)5 FillWrapper (com.alibaba.excel.write.metadata.fill.FillWrapper)4 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)4 OutputStream (java.io.OutputStream)4 Map (java.util.Map)4 LargeDataTest (com.alibaba.easyexcel.test.core.large.LargeDataTest)3 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)3 WriteExcelException (com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)3