Apache POI工作簿的默认样式

Gur*_*rtz 5 java apache-poi

我正在尝试使用更改整个Excel工作簿(XSSF)的默认单元格样式Apache POI.这应该应用于用户可能创建的新单元格(在保存工作簿之后POI).我试图通过调用workbook.getCellStyleAt(0)- 我理解为工作簿的默认样式 - 然后通过将此样式修改为我想要的新默认样式来执行此操作.

这在我读入现有XSLX文件("模板"文件)并修改默认样式时有效.但是,当我创建一个新的XSLX使用临时文件POI,它并没有正常工作.

当单步使用调试器时,我可以看到,当使用"模板"文件时,在索引0处为单元格样式分配了一个"主题"(可能是因为模板文件最初是使用Excel创建的).但是,当从头开始创建文件(使用POI)时,索引0处的单元格样式具有空主题.(这可能是为什么使用一种方法而不是另一种方法的原因.)

有关如何可靠地更改工作簿(XSSF)的默认单元格样式的任何建议,无论最初如何创建工作簿?谢谢!

Axe*_*ter 11

使用XSSF有两种可能性.

第一步:如果在Excel中的XSSF工作表中选择所有单元格并对其应用样式,则会向工作表cols添加一个元素,其中包含所有列的样式定义:

<cols>
 <col min="1" max="16384" style="1"/>
</cols>
Run Code Online (Sandbox Code Playgroud)

这可以通过apache poi实现,如下所示:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;

class ExcelCellStyleAllColumns
 {

 public static void main(String[] args) {
  try {

    Workbook wb = new XSSFWorkbook();

    Font font = wb.createFont();
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setBold(true);

    CellStyle style = wb.createCellStyle();
    style.setFont(font);

    Sheet sheet = wb.createSheet();

    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol cTCol = 
      ((XSSFSheet)sheet).getCTWorksheet().getColsArray(0).addNewCol();
    cTCol.setMin(1);
    cTCol.setMax(16384);
    cTCol.setWidth(12.7109375);
    cTCol.setStyle(style.getIndex());

    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("test");
    cell.setCellStyle(style);

    FileOutputStream os = new FileOutputStream("ExcelCellStyleAllColumns.xlsx");
    wb.write(os);
    os.close();

  } catch (IOException ioex) {
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

这将更改工作表中所有单元格的默认单元格样式.


第二:您可以修改正常单元格样式的样式定义,如下所示:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;

class ExcelDefaultCellStyle {

 public static void main(String[] args) {
  try {

    Workbook wb = new XSSFWorkbook();

    Font font = wb.getFontAt((short)0);
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    ((XSSFFont)font).setFamily(3);
    ((XSSFFont)font).setScheme(FontScheme.NONE);
    font.setItalic(true);
    font.setBold(true);

    CellStyle style = wb.getCellStyleAt(0);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setWrapText(true);

    ((XSSFWorkbook) wb).getStylesSource().getCTStylesheet().addNewCellStyles().addNewCellStyle().setXfId(0);

    ((XSSFCellStyle)style).getStyleXf().addNewAlignment().setVertical(
     org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment.CENTER);
    ((XSSFCellStyle)style).getStyleXf().getAlignment().setWrapText(true);

    Sheet sheet = wb.createSheet();

    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("test");

    FileOutputStream os = new FileOutputStream("ExcelDefaultCellStyle.xlsx");
    wb.write(os);
    os.close();

  } catch (IOException ioex) {
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

这将更改整个工作簿中所有单元格的默认单元格样式.

XML中styles.xml显示:

<cellStyleXfs count="1">
 <xf numFmtId="0" fontId="0" fillId="0" borderId="0">
  <alignment vertical="center" wrapText="true"/>
 </xf>
</cellStyleXfs>
<cellXfs count="1">
 <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0">
  <alignment vertical="center" wrapText="true"/>
 </xf>
</cellXfs>
<cellStyles>
 <cellStyle xfId="0"/>
</cellStyles>
Run Code Online (Sandbox Code Playgroud)

如您所见,正常的细胞样式是第一个cellStyles.它指的是xfId="0"哪个numFmtId="0" fontId="0" fillId="0" borderId="0".这意味着数字格式,字体,填充格式和边框的最初定义用于正常的单元格样式.