Class ExcelWriter

All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
BigExcelWriter

public class ExcelWriter extends ExcelBase<ExcelWriter,ExcelWriteConfig>
Excel 写入器 此工具用于通过POI将数据写出到Excel,此对象可完成以下两个功能
 1. 编辑已存在的Excel,可写出原Excel文件,也可写出到其它地方(到文件或到流)
 2. 新建一个空的Excel工作簿,完成数据填充后写出(到文件或到流)
 
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • ExcelWriter

      public ExcelWriter()
      构造,默认生成xlsx格式的Excel文件 此构造不传入写出的Excel文件路径,只能调用flush(OutputStream)方法写出到流 若写出到文件,还需调用setTargetFile(File)方法自定义写出的文件,然后调用flush()方法写出到文件
    • ExcelWriter

      public ExcelWriter(boolean isXlsx)
      此构造不传入写出的Excel文件路径,只能调用flush(OutputStream)方法写出到流 若写出到文件,需要调用flush(File, boolean) 写出到文件
      Parameters:
      isXlsx - 是否为xlsx格式
    • ExcelWriter

      public ExcelWriter(String templateFilePath)
      构造,默认写出到第一个sheet,第一个sheet名为sheet1
      Parameters:
      templateFilePath - 模板文件,不存在则默认为目标写出文件
    • ExcelWriter

      public ExcelWriter(boolean isXlsx, String sheetName)
      构造 此构造不传入写出的Excel文件路径,只能调用flush(OutputStream)方法写出到流 若写出到文件,需要调用flush(File, boolean) 写出到文件
      Parameters:
      isXlsx - 是否为xlsx格式
      sheetName - sheet名,第一个sheet名并写出到此sheet,例如sheet1
    • ExcelWriter

      public ExcelWriter(String templateFilePath, String sheetName)
      构造
      Parameters:
      templateFilePath - 模板文件,不存在则默认为目标写出文件
      sheetName - sheet名,第一个sheet名并写出到此sheet,例如sheet1
    • ExcelWriter

      public ExcelWriter(File templateFile)
      构造,默认写出到第一个sheet,第一个sheet名为sheet1
      Parameters:
      templateFile - 模板文件,不存在则默认为目标写出文件
    • ExcelWriter

      public ExcelWriter(File templateFile, String sheetName)
      构造
      Parameters:
      templateFile - 模板文件,不存在则默认为目标写出文件
      sheetName - sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
    • ExcelWriter

      public ExcelWriter(org.apache.poi.ss.usermodel.Workbook templateWorkbook, String sheetName)
      构造 此构造不传入写出的Excel文件路径,只能调用flush(OutputStream)方法写出到流 若写出到文件,还需调用setTargetFile(File)方法自定义写出的文件,然后调用flush()方法写出到文件
      Parameters:
      templateWorkbook - 模板Workbook
      sheetName - sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
    • ExcelWriter

      public ExcelWriter(org.apache.poi.ss.usermodel.Sheet sheet)
      构造 此构造不传入写出的Excel文件路径,只能调用flush(OutputStream)方法写出到流 若写出到文件,还需调用setTargetFile(File)方法自定义写出的文件,然后调用flush()方法写出到文件
      Parameters:
      sheet - Sheet
  • Method Details

    • addSelect

      public static void addSelect(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.util.CellRangeAddressList regions, String... selectList)
      增加下拉列表
      Parameters:
      sheet - Sheet
      regions - CellRangeAddressList 指定下拉列表所占的单元格范围
      selectList - 下拉列表内容
    • setConfig

      public ExcelWriter setConfig(ExcelWriteConfig config)
      Description copied from class: ExcelBase
      设置Excel配置
      Overrides:
      setConfig in class ExcelBase<ExcelWriter,ExcelWriteConfig>
      Parameters:
      config - Excel配置
      Returns:
      this
    • reset

      public ExcelWriter reset()
      重置Writer,包括:
       1. 当前行游标归零
       2. 清除标题缓存
       
      Returns:
      this
    • close

      public void close()
      关闭工作簿 如果用户设定了目标文件,先写出目标文件后给关闭工作簿
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ExcelBase<ExcelWriter,ExcelWriteConfig>
    • closeWithoutFlush

      protected void closeWithoutFlush()
      关闭工作簿但是不写出
    • setSheet

      public ExcelWriter setSheet(int sheetIndex)
      Description copied from class: ExcelBase
      自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认) 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet
      Overrides:
      setSheet in class ExcelBase<ExcelWriter,ExcelWriteConfig>
      Parameters:
      sheetIndex - sheet序号,从0开始计数
      Returns:
      this
    • setSheet

      public ExcelWriter setSheet(String sheetName)
      Description copied from class: ExcelBase
      自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet。
      Overrides:
      setSheet in class ExcelBase<ExcelWriter,ExcelWriteConfig>
      Parameters:
      sheetName - sheet名
      Returns:
      this
    • renameSheet

      public ExcelWriter renameSheet(String sheetName)
      重命名当前sheet
      Overrides:
      renameSheet in class ExcelBase<ExcelWriter,ExcelWriteConfig>
      Parameters:
      sheetName - 新的sheet名
      Returns:
      this
      See Also:
      • Workbook.setSheetName(int, String)
    • renameSheet

      public ExcelWriter renameSheet(int sheet, String sheetName)
      重命名sheet
      Parameters:
      sheet - sheet序号,0表示第一个sheet
      sheetName - 新的sheet名
      Returns:
      this
    • autoSizeColumnAll

      public ExcelWriter autoSizeColumnAll(boolean useMergedCells, float widthRatio)
      设置所有列为自动宽度,不考虑合并单元格 此方法必须在指定列数据完全写出后调用才有效。 列数计算是通过第一行计算的
      Parameters:
      useMergedCells - 是否适用于合并单元格
      widthRatio - 列宽的倍数。如果所有内容都是英文,可以设为1,如果有中文,建议设置为 1.6-2.0之间。
      Returns:
      this
    • autoSizeColumn

      public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells, float widthRatio)
      设置某列为自动宽度。注意有中文的情况下,需要根据需求调整宽度扩大比例。 此方法必须在指定列数据完全写出后调用才有效。
      Parameters:
      columnIndex - 第几列,从0计数
      useMergedCells - 是否适用于合并单元格
      widthRatio - 列宽的倍数。如果所有内容都是英文,可以设为1,如果有中文,建议设置为 1.6-2.0之间。
      Returns:
      this
    • disableDefaultStyle

      public ExcelWriter disableDefaultStyle()
      禁用默认样式
      Returns:
      this
      See Also:
    • getStyleSet

      public StyleSet getStyleSet()
      获取样式集,样式集可以自定义包括:
       1. 头部样式
       2. 一般单元格样式
       3. 默认数字样式
       4. 默认日期样式
       
      Returns:
      样式集
    • setStyleSet

      public ExcelWriter setStyleSet(StyleSet styleSet)
      设置样式集,如果不使用样式,传入null
      Parameters:
      styleSet - 样式集,null表示无样式
      Returns:
      this
    • getCurrentRow

      public int getCurrentRow()
      获得当前行
      Returns:
      当前行
    • setCurrentRow

      public ExcelWriter setCurrentRow(int rowIndex)
      设置当前所在行
      Parameters:
      rowIndex - 行号
      Returns:
      this
    • setCurrentRowToEnd

      public ExcelWriter setCurrentRowToEnd()
      定位到最后一行的后边,用于追加数据
      Returns:
      this
    • passCurrentRow

      public ExcelWriter passCurrentRow()
      跳过当前行
      Returns:
      this
    • passRows

      public ExcelWriter passRows(int rowNum)
      跳过指定行数
      Parameters:
      rowNum - 跳过的行数
      Returns:
      this
    • resetRow

      public ExcelWriter resetRow()
      重置当前行为0
      Returns:
      this
    • setTargetFile

      public ExcelWriter setTargetFile(File targetFile)
      设置写出的目标文件 注意这个文件不能存在,存在则flush()时会被覆盖
      Parameters:
      targetFile - 目标文件
      Returns:
      this
    • setFreezePane

      public ExcelWriter setFreezePane(int rowSplit)
      设置窗口冻结,之前冻结的窗口会被覆盖,如果rowSplit为0表示取消冻结
      Parameters:
      rowSplit - 冻结的行及行数,2表示前两行
      Returns:
      this
    • setFreezePane

      public ExcelWriter setFreezePane(int colSplit, int rowSplit)
      设置窗口冻结,之前冻结的窗口会被覆盖,如果colSplit和rowSplit为0表示取消冻结
      Parameters:
      colSplit - 冻结的列及列数,2表示前两列
      rowSplit - 冻结的行及行数,2表示前两行
      Returns:
      this
    • setColumnWidth

      public ExcelWriter setColumnWidth(int columnIndex, int width)
      设置列宽(单位为一个字符的宽度,例如传入width为10,表示10个字符的宽度)
      Parameters:
      columnIndex - 列号(从0开始计数,-1表示所有列的默认宽度)
      width - 宽度(单位1~255个字符宽度)
      Returns:
      this
    • setDefaultRowHeight

      public ExcelWriter setDefaultRowHeight(int height)
      设置默认行高,值为一个点的高度
      Parameters:
      height - 高度
      Returns:
      this
    • setRowHeight

      public ExcelWriter setRowHeight(int rowNum, int height)
      设置行高,值为一个点的高度
      Parameters:
      rowNum - 行号(从0开始计数,-1表示所有行的默认高度)
      height - 高度
      Returns:
      this
    • setHeaderOrFooter

      public ExcelWriter setHeaderOrFooter(String text, org.miaixz.bus.core.lang.EnumValue.Align align, boolean isFooter)
      设置Excel页眉或页脚
      Parameters:
      text - 页脚的文本
      align - 对齐方式枚举 EnumValue.Align
      isFooter - 是否为页脚,false表示页眉,true表示页脚
      Returns:
      this
    • addIgnoredErrors

      public ExcelWriter addIgnoredErrors(org.apache.poi.ss.util.CellRangeAddress cellRangeAddress, org.apache.poi.ss.usermodel.IgnoredErrorType... ignoredErrorTypes) throws UnsupportedOperationException
      设置忽略错误,即Excel中的绿色警告小标,只支持XSSFSheet和SXSSFSheet 见:https://stackoverflow.com/questions/23488221/how-to-remove-warning-in-excel-using-apache-poi-in-java
      Parameters:
      cellRangeAddress - 指定单元格范围
      ignoredErrorTypes - 忽略的错误类型列表
      Returns:
      this
      Throws:
      UnsupportedOperationException - 如果sheet不是XSSFSheet
    • addSelect

      public ExcelWriter addSelect(int x, int y, String... selectList)
      增加下拉列表
      Parameters:
      x - x坐标,列号,从0开始
      y - y坐标,行号,从0开始
      selectList - 下拉列表
      Returns:
      this
    • addSelect

      public ExcelWriter addSelect(org.apache.poi.ss.util.CellRangeAddressList regions, String... selectList)
      增加下拉列表
      Parameters:
      regions - CellRangeAddressList 指定下拉列表所占的单元格范围
      selectList - 下拉列表内容
      Returns:
      this
    • addValidationData

      public ExcelWriter addValidationData(org.apache.poi.ss.usermodel.DataValidation dataValidation)
      增加单元格控制,比如下拉列表、日期验证、数字范围验证等
      Parameters:
      dataValidation - DataValidation
      Returns:
      this
    • merge

      public ExcelWriter merge(int lastColumn)
      合并当前行的单元格
      Parameters:
      lastColumn - 合并到的最后一个列号
      Returns:
      this
    • merge

      public ExcelWriter merge(int lastColumn, Object content)
      合并当前行的单元格,并写入对象到单元格 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变
      Parameters:
      lastColumn - 合并到的最后一个列号
      content - 合并单元格后的内容
      Returns:
      this
    • merge

      public ExcelWriter merge(int lastColumn, Object content, boolean isSetHeaderStyle)
      合并某行的单元格,并写入对象到单元格 如果写到单元格中的内容非null,行号自动+1,否则当前行号不变
      Parameters:
      lastColumn - 合并到的最后一个列号
      content - 合并单元格后的内容
      isSetHeaderStyle - 是否为合并后的单元格设置默认标题样式,只提取边框样式
      Returns:
      this
    • merge

      public ExcelWriter merge(org.apache.poi.ss.util.CellRangeAddress cellRangeAddress, Object content, boolean isSetHeaderStyle)
      合并某行的单元格,并写入对象到单元格
      Parameters:
      cellRangeAddress - 合并单元格范围,定义了起始行列和结束行列
      content - 合并单元格后的内容
      isSetHeaderStyle - 是否为合并后的单元格设置默认标题样式,只提取边框样式
      Returns:
      this
    • merge

      public ExcelWriter merge(org.apache.poi.ss.util.CellRangeAddress cellRangeAddress, Object content, org.apache.poi.ss.usermodel.CellStyle cellStyle)
      合并单元格,并写入对象到单元格,使用指定的样式 指定样式传入null,则不使用任何样式
      Parameters:
      cellRangeAddress - 合并单元格范围,定义了起始行列和结束行列
      content - 合并单元格后的内容
      cellStyle - 合并后单元格使用的样式,可以为null
      Returns:
      this
    • write

      public ExcelWriter write(Iterable<?> data)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动增加 默认的,当当前行号为0时,写出标题(如果为Map或Bean),否则不写标题

      data中元素支持的类型有:

       1. Iterable,即元素为一个集合,元素被当作一行,data表示多行
       2. Map,即元素为一个Map,第一个Map的keys作为首行,剩下的行为Map的values,data表示多行 
       3. Bean,即元素为一个Bean,第一个Bean的字段名列表会作为首行,剩下的行为Bean的字段值列表,data表示多行 
       4. 其它类型,按照基本类型输出(例如字符串)
       
      Parameters:
      data - 数据
      Returns:
      this
    • write

      public ExcelWriter write(Iterable<?> data, boolean isWriteKeyAsHead)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动增加

      data中元素支持的类型有:

       1. Iterable,即元素为一个集合,元素被当作一行,data表示多行
       2. Map,即元素为一个Map,第一个Map的keys作为首行,剩下的行为Map的values,data表示多行 
       3. Bean,即元素为一个Bean,第一个Bean的字段名列表会作为首行,剩下的行为Bean的字段值列表,data表示多行 
       4. 其它类型,按照基本类型输出(例如字符串)
       
      Parameters:
      data - 数据
      isWriteKeyAsHead - 是否强制写出标题行(Map或Bean)
      Returns:
      this
    • write

      public ExcelWriter write(Iterable<?> data, Comparator<String> comparator)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动增加 data中元素支持的类型有:

      1. Map,即元素为一个Map,第一个Map的keys作为首行,剩下的行为Map的values,data表示多行 2. Bean,即元素为一个Bean,第一个Bean的字段名列表会作为首行,剩下的行为Bean的字段值列表,data表示多行

      Parameters:
      data - 数据
      comparator - 比较器,用于字段名的排序
      Returns:
      this
    • writeHeader

      public ExcelWriter writeHeader(RowGroup rowGroup)
      写出分组标题行
      Parameters:
      rowGroup - 分组行
      Returns:
      this
    • writeHeader

      public ExcelWriter writeHeader(int x, int y, int rowCount, RowGroup rowGroup)
      写出分组标题行
      Parameters:
      x - 开始的列,下标从0开始
      y - 开始的行,下标从0开始
      rowCount - 当前分组行所占行数,此数值为标题占用行数+子分组占用的最大行数,不确定传1
      rowGroup - 分组行
      Returns:
      this
    • writeImg

      public ExcelWriter writeImg(File imgFile, int col1, int row1, int col2, int row2)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 添加图片到当前sheet中 / 默认图片类型png / 默认的起始坐标和结束坐标都为0
      Parameters:
      imgFile - 图片文件
      col1 - 指定起始的列,下标从0开始
      row1 - 指定起始的行,下标从0开始
      col2 - 指定结束的列,下标从0开始
      row2 - 指定结束的行,下标从0开始
      Returns:
      this
    • writeImg

      public ExcelWriter writeImg(File imgFile, SimpleAnchor clientAnchor)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 添加图片到当前sheet中 / 默认图片类型png
      Parameters:
      imgFile - 图片文件
      clientAnchor - 图片的位置和大小信息
      Returns:
      this
    • writeImg

      public ExcelWriter writeImg(File imgFile, ExcelPictureType imgType, SimpleAnchor clientAnchor)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 添加图片到当前sheet中
      Parameters:
      imgFile - 图片文件
      imgType - 图片类型,对应poi中Workbook类中的图片类型2-7变量
      clientAnchor - 图片的位置和大小信息
      Returns:
      this
    • writeImg

      public ExcelWriter writeImg(byte[] pictureData, ExcelPictureType imgType, SimpleAnchor clientAnchor)
      写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件 添加图片到当前sheet中
      Parameters:
      pictureData - 数据bytes
      imgType - 图片类型,对应poi中Workbook类中的图片类型2-7变量
      clientAnchor - 图片的位置和大小信息
      Returns:
      this
    • writeLineShape

      public ExcelWriter writeLineShape(SimpleAnchor clientAnchor)
      绘制线条
      Parameters:
      clientAnchor - 绘制区域信息
      Returns:
      this
    • writeLineShape

      public ExcelWriter writeLineShape(SimpleAnchor clientAnchor, LineStyle lineStyle, int lineWidth, Color lineColor)
      绘制线条
      Parameters:
      clientAnchor - 绘制区域信息
      lineStyle - 线条样式
      lineWidth - 线条粗细
      lineColor - 线条颜色
      Returns:
      this
    • writeSimpleShape

      public ExcelWriter writeSimpleShape(SimpleAnchor clientAnchor, ShapeConfig shapeConfig)
      绘制简单形状
      Parameters:
      clientAnchor - 绘制区域信息
      shapeConfig - 形状配置,包括形状类型、线条样式、线条宽度、线条颜色、填充颜色等
      Returns:
      this
    • writeHeaderRow

      public ExcelWriter writeHeaderRow(Iterable<?> rowData)
      写出一行标题数据 本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动+1
      Parameters:
      rowData - 一行的数据
      Returns:
      this
    • fillOnce

      public ExcelWriter fillOnce(Map<?,?> rowMap)
      填充非列表模板变量(一次性变量)
      Parameters:
      rowMap - 行数据
      Returns:
      this
    • writeRow

      public ExcelWriter writeRow(Object rowBean, boolean isWriteKeyAsHead)
      写出一行,根据rowBean数据类型不同,写出情况如下:
       1、如果为Iterable,直接写出一行
       2、如果为Map,isWriteKeyAsHead为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
       3、如果为Bean,转为Map写出,isWriteKeyAsHead为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
       
      Parameters:
      rowBean - 写出的Bean,可以是Map、Bean或Iterable
      isWriteKeyAsHead - 为true写出两行,Map的keys做为一行,values做为第二行,否则只写出一行values
      Returns:
      this
    • writeRow

      public ExcelWriter writeRow(Iterable<?> rowData)
      写出一行数据 本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动+1
      Parameters:
      rowData - 一行的数据
      Returns:
      this
    • writeCol

      public ExcelWriter writeCol(Map<?,? extends Iterable<?>> colMap, boolean isWriteKeyAsHead)
      从第1列开始按列写入数据(index 从0开始) 本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动+1
      Parameters:
      colMap - 一列的数据
      isWriteKeyAsHead - 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values
      Returns:
      this
    • writeCol

      public ExcelWriter writeCol(Map<?,? extends Iterable<?>> colMap, int startColIndex, boolean isWriteKeyAsHead)
      从指定列开始按列写入数据(index 从0开始) 本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动+1
      Parameters:
      colMap - 一列的数据
      startColIndex - 起始的列号,从0开始
      isWriteKeyAsHead - 是否将Map的Key作为表头输出,如果为True第一行为表头,紧接着为values
      Returns:
      this
    • writeCol

      public ExcelWriter writeCol(Object headerVal, Iterable<?> colData, boolean isResetRowIndex)
      为第一列写入数据 本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动+1
      Parameters:
      headerVal - 表头名称,如果为null则不写入
      colData - 需要写入的列数据
      isResetRowIndex - 如果为true,写入完毕后Row index 将会重置为写入之前的未知,如果为false,写入完毕后Row index将会在写完的数据下方
      Returns:
      this
    • writeCol

      public ExcelWriter writeCol(Object headerVal, int colIndex, Iterable<?> colData, boolean isResetRowIndex)
      为第指定列写入数据 本方法只是将数据写入Workbook中的Sheet,并不写出到文件 写出的起始行为当前行号,可使用getCurrentRow()方法调用,根据写出的的行数,当前行号自动+1
      Parameters:
      headerVal - 表头名称,如果为null则不写入
      colIndex - 列index
      colData - 需要写入的列数据
      isResetRowIndex - 如果为true,写入完毕后Row index 将会重置为写入之前的未知,如果为false,写入完毕后Row index将会在写完的数据下方
      Returns:
      this
    • writeCellValue

      public ExcelWriter writeCellValue(String locationRef, Object value)
      给指定单元格赋值,使用默认单元格样式
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      value - 值
      Returns:
      this
    • writeCellValue

      public ExcelWriter writeCellValue(int x, int y, Object value)
      给指定单元格赋值,使用默认单元格样式
      Parameters:
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      value - 值
      Returns:
      this
    • writeCellValue

      public ExcelWriter writeCellValue(int x, int y, Object value, boolean isHeader)
      给指定单元格赋值,使用默认单元格样式
      Parameters:
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      value - 值
      isHeader - 是否为Header
      Returns:
      this
    • setStyle

      public ExcelWriter setStyle(org.apache.poi.ss.usermodel.CellStyle style, String locationRef)
      设置某个单元格的样式 此方法用于多个单元格共享样式的情况 可以调用ExcelBase.getOrCreateCellStyle(int, int) 方法创建或取得一个样式对象。 需要注意的是,共享样式会共享同一个CellStyle,一个单元格样式改变,全部改变。
      Parameters:
      style - 单元格样式
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      this
    • setStyle

      public ExcelWriter setStyle(org.apache.poi.ss.usermodel.CellStyle style, int x, int y)
      设置某个单元格的样式 此方法用于多个单元格共享样式的情况 可以调用ExcelBase.getOrCreateCellStyle(int, int) 方法创建或取得一个样式对象。 需要注意的是,共享样式会共享同一个CellStyle,一个单元格样式改变,全部改变。
      Parameters:
      style - 单元格样式
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      Returns:
      this
    • setRowStyle

      public ExcelWriter setRowStyle(int y, org.apache.poi.ss.usermodel.CellStyle style)
      设置行样式
      Parameters:
      y - Y坐标,从0计数,即行号
      style - 样式
      Returns:
      this
      See Also:
      • Row.setRowStyle(CellStyle)
    • setRowStyleIfHasData

      public ExcelWriter setRowStyleIfHasData(int y, org.apache.poi.ss.usermodel.CellStyle style)
      对数据行整行加自定义样式 仅对数据单元格设置 write后调用 setRowStyle(int, org.apache.poi.ss.usermodel.CellStyle) 这个方法加的样式会使整行没有数据的单元格也有样式 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
      Parameters:
      y - 行坐标
      style - 自定义的样式
      Returns:
      this
    • setColumnStyle

      public ExcelWriter setColumnStyle(int x, org.apache.poi.ss.usermodel.CellStyle style)
      设置列的默认样式
      Parameters:
      x - 列号,从0开始
      style - 样式
      Returns:
      this
    • setColumnStyleIfHasData

      public ExcelWriter setColumnStyleIfHasData(int x, int y, org.apache.poi.ss.usermodel.CellStyle style)
      设置整个列的样式 仅对数据单元格设置 write后调用 setColumnStyle(int, org.apache.poi.ss.usermodel.CellStyle) 这个方法加的样式会使整列没有数据的单元格也有样式 特别是加背景色时很不美观 且有数据的单元格样式会被StyleSet中的样式覆盖掉
      Parameters:
      x - 列的索引
      y - 起始行
      style - 样式
      Returns:
      this
    • flush

      public ExcelWriter flush() throws org.miaixz.bus.core.lang.exception.InternalException
      将Excel Workbook刷出到预定义的文件 如果用户未自定义输出的文件,将抛出NullPointerException 预定义文件可以通过setTargetFile(File) 方法预定义,或者通过构造定义
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • flush

      public ExcelWriter flush(boolean override) throws org.miaixz.bus.core.lang.exception.InternalException
      将Excel Workbook刷出到预定义的文件 如果用户未自定义输出的文件,将抛出NullPointerException 预定义文件可以通过setTargetFile(File) 方法预定义,或者通过构造定义
      Parameters:
      override - 是否覆盖已有文件
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • flush

      public ExcelWriter flush(File targetFile, boolean override) throws org.miaixz.bus.core.lang.exception.InternalException
      将Excel Workbook刷出到文件 如果用户未自定义输出的文件,将抛出NullPointerException
      Parameters:
      targetFile - 写出到的文件
      override - 是否覆盖已有文件
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • flush

      public ExcelWriter flush(OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      将Excel Workbook刷出到输出流
      Parameters:
      out - 输出流
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常
    • flush

      public ExcelWriter flush(OutputStream out, boolean isCloseOut) throws org.miaixz.bus.core.lang.exception.InternalException
      将Excel Workbook刷出到输出流
      Parameters:
      out - 输出流
      isCloseOut - 是否关闭输出流
      Returns:
      this
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - IO异常