Browse Source

修改宽度策略

bugfix
zhuangjiaju 5 years ago
parent
commit
41460978d4
  1. 12
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  2. 10
      src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java
  3. 11
      src/main/java/com/alibaba/excel/write/style/column/AbstractHeadColumnWidthStyleStrategy.java
  4. 15
      src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java

12
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -8,9 +8,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.ConverterKeyBuild;
@ -37,7 +35,6 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty; import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
import com.alibaba.excel.write.style.RowCellStyleStrategy; import com.alibaba.excel.write.style.RowCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy; import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
@ -248,15 +245,16 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
} }
private void dealColumnWidth(List<WriteHandler> handlerList) { private void dealColumnWidth(List<WriteHandler> handlerList) {
WriteHandler columnWidthStyleStrategy = new AbstractColumnWidthStyleStrategy() { WriteHandler columnWidthStyleStrategy = new AbstractHeadColumnWidthStyleStrategy() {
@Override @Override
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) { protected Integer columnWidth(Head head) {
if (head == null) { if (head == null) {
return; return null;
} }
if (head.getColumnWidthProperty() != null) { if (head.getColumnWidthProperty() != null) {
sheet.setColumnWidth(head.getColumnIndex(), head.getColumnWidthProperty().getWidth()); return head.getColumnWidthProperty().getWidth();
} }
return null;
} }
}; };
handlerList.add(columnWidthStyleStrategy); handlerList.add(columnWidthStyleStrategy);

10
src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java

@ -32,10 +32,7 @@ public abstract class AbstractColumnWidthStyleStrategy implements CellWriteHandl
@Override @Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
Head head, int relativeRowIndex, boolean isHead) { Head head, int relativeRowIndex, boolean isHead) {
if (!isHead && relativeRowIndex != 0) { setColumnWidth(writeSheetHolder.getSheet(), cell, head, relativeRowIndex, isHead);
return;
}
setColumnWidth(writeSheetHolder.getSheet(), cell, head);
} }
/** /**
@ -44,7 +41,10 @@ public abstract class AbstractColumnWidthStyleStrategy implements CellWriteHandl
* @param sheet * @param sheet
* @param cell * @param cell
* @param head * @param head
* @param relativeRowIndex
* @param isHead
*/ */
protected abstract void setColumnWidth(Sheet sheet, Cell cell, @Nullable Head head); protected abstract void setColumnWidth(Sheet sheet, Cell cell, @Nullable Head head, int relativeRowIndex,
boolean isHead);
} }

11
src/main/java/com/alibaba/excel/write/style/column/AbstractHeadColumnWidthStyleStrategy.java

@ -8,12 +8,15 @@ import com.sun.istack.internal.Nullable;
/** /**
* Returns the column width according to each column header * Returns the column width according to each column header
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public abstract class AbstractHeadColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { public abstract class AbstractHeadColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
@Override @Override
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) { protected void setColumnWidth(Sheet sheet, Cell cell, Head head, int relativeRowIndex, boolean isHead) {
if (!isHead && relativeRowIndex != 0) {
return;
}
Integer width = columnWidth(head); Integer width = columnWidth(head);
if (width != null) { if (width != null) {
sheet.setColumnWidth(cell.getColumnIndex(), columnWidth(head)); sheet.setColumnWidth(cell.getColumnIndex(), columnWidth(head));
@ -22,9 +25,9 @@ public abstract class AbstractHeadColumnWidthStyleStrategy extends AbstractColum
/** /**
* Returns the column width corresponding to each column head. * Returns the column width corresponding to each column head.
* *
* <li>if return null,ignore * <li>if return null,ignore
* *
* @param head * @param head
* @return the width in units of 1/256th of a character width . Using the Calibri font as an example, the maximum * @return the width in units of 1/256th of a character width . Using the Calibri font as an example, the maximum
* digit width of 11 point font size is 7 pixels (at 96 dpi). If you set a column width to be eight * digit width of 11 point font size is 7 pixels (at 96 dpi). If you set a column width to be eight

15
src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java

@ -1,22 +1,19 @@
package com.alibaba.excel.write.style.column; package com.alibaba.excel.write.style.column;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
/** /**
* All the columns are the same width * All the columns are the same width
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class SimpleColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy {
private Integer columnWidth; private Integer columnWidth;
/** /**
* Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi). * If * Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi). * If
* you set a column width to be eight characters wide, e.g. <code>SimpleColumnWidthStyleStrategy( 8*256)</code>, * you set a column width to be eight characters wide, e.g. <code>SimpleColumnWidthStyleStrategy( 8*256)</code>,
* *
* @param columnWidth * @param columnWidth
* the width in units of 1/256th of a character width * the width in units of 1/256th of a character width
*/ */
@ -25,9 +22,7 @@ public class SimpleColumnWidthStyleStrategy extends AbstractColumnWidthStyleStra
} }
@Override @Override
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) { protected Integer columnWidth(Head head) {
if (columnWidth != null) { return columnWidth;
sheet.setColumnWidth(cell.getColumnIndex(), columnWidth);
}
} }
} }

Loading…
Cancel
Save