在cell中如果cell中的文本有换行符, 默认是不显示换行的, 只有点了excel 工具栏中的“Wrap Text" 按钮, 才会显示换行, 见下图:
这个效果, 可以通过设置openxml的 style sheet 来实现。
xml
xml version="1.0" encoding="utf-8" ?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:fonts count="1">
<x:font>
<x:sz val="11" />
<x:color theme="1" />
<x:name val="Calibri" />
<x:family val="2" />
<x:scheme val="minor" />
x:font>
x:fonts>
<x:fills count="2">
<x:fill>
<x:patternFill patternType="none" />
x:fill>
x:fills>
<x:borders count="1">
<x:border>
<x:left />
<x:right />
<x:top />
<x:bottom />
<x:diagonal />
x:border>
x:borders>
<x:cellXfs count="2">
<x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" />
<x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyAlignment="1">
<x:alignment wrapText="1" />
x:xf>
x:cellXfs>
x:styleSheet>
xml version="1.0" encoding="utf-8" ?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:fonts count="1">
<x:font>
<x:sz val="11" />
<x:color theme="1" />
<x:name val="Calibri" />
<x:family val="2" />
<x:scheme val="minor" />
x:font>
x:fonts>
<x:fills count="2">
<x:fill>
<x:patternFill patternType="none" />
x:fill>
x:fills>
<x:borders count="1">
<x:border>
<x:left />
<x:right />
<x:top />
<x:bottom />
<x:diagonal />
x:border>
x:borders>
<x:cellXfs count="2">
<x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" />
<x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyAlignment="1">
<x:alignment wrapText="1" />
x:xf>
x:cellXfs>
x:styleSheet>
在创建stylesheet时, 必须创建fonts, Fills,Borders 和cellXfs(CellFormats) 四个节点,
在显示cell是通过StyleIndex 来关联 cellXfs的Index 来改变cell 的显示样式, 注意, 这个index只能从1 开始,因此需要在cellXfs中加两个CellFormat子节点, 我们这里要设置 wrap text, 因此在第二个节点设置applyAlignment 并设wrap Text ="1". 上个关于openxml的帖子, 有人问怎么设置cell的 font,答案就是加一个font 子节点到fonts, 得到index, 再加一个cellformat 子节点 并设置fontid 为刚加的font的index。 把这个cellformat的id 给 要设置的cell的StyleIndex。
初始stylesheet 并加 wraptext style:
Code
private void InitializeStyleSheet()
{
spreadSheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet = new Stylesheet();
Stylesheet stylesheet = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;
stylesheet.Fonts = new DocumentFormat.OpenXml.Spreadsheet.Fonts(new Font(new FontSize() { Val = 11D }, new Color() { Theme = (UInt32Value)1U },
new FontName() { Val = "Calibri" }, new FontFamily() { Val = 2 },
new DocumentFormat.OpenXml.Spreadsheet.FontScheme() { Val = FontSchemeValues.Minor })) { Count = (UInt32Value)1U };
stylesheet.Fills = new Fills(new DocumentFormat.OpenXml.Spreadsheet.Fill(new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = PatternValues.None })) { Count = (UInt32Value)2U };
stylesheet.Borders = new Borders(new Border(new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(),
new DocumentFormat.OpenXml.Spreadsheet.RightBorder(), new DocumentFormat.OpenXml.Spreadsheet.TopBorder(),
new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(), new DiagonalBorder())) { Count = (UInt32Value)1U };
stylesheet.CellFormats = new CellFormats();
stylesheet.CellFormats.Count = 2;
CellFormat cf0 = stylesheet.CellFormats.AppendChild(new CellFormat());
cf0.NumberFormatId = 0;
cf0.FontId = 0;
cf0.BorderId = 0;
cf0.FillId = 0;
CellFormat cf = stylesheet.CellFormats.AppendChild(new CellFormat());
cf.Alignment = new Alignment();
cf.ApplyAlignment = true;
cf.NumberFormatId = 0;
cf.FontId = 0;
cf.BorderId = 0;
cf.FillId = 0;
cf.Alignment.WrapText = true;
spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();
}
private void InitializeStyleSheet()
{
spreadSheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet = new Stylesheet();
Stylesheet stylesheet = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;
stylesheet.Fonts = new DocumentFormat.OpenXml.Spreadsheet.Fonts(new Font(new FontSize() { Val = 11D }, new Color() { Theme = (UInt32Value)1U },
new FontName() { Val = "Calibri" }, new FontFamily() { Val = 2 },
new DocumentFormat.OpenXml.Spreadsheet.FontScheme() { Val = FontSchemeValues.Minor })) { Count = (UInt32Value)1U };
stylesheet.Fills = new Fills(new DocumentFormat.OpenXml.Spreadsheet.Fill(new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = PatternValues.None })) { Count = (UInt32Value)2U };
stylesheet.Borders = new Borders(new Border(new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(),
new DocumentFormat.OpenXml.Spreadsheet.RightBorder(), new DocumentFormat.OpenXml.Spreadsheet.TopBorder(),
new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(), new DiagonalBorder())) { Count = (UInt32Value)1U };
stylesheet.CellFormats = new CellFormats();
stylesheet.CellFormats.Count = 2;
CellFormat cf0 = stylesheet.CellFormats.AppendChild(new CellFormat());
cf0.NumberFormatId = 0;
cf0.FontId = 0;
cf0.BorderId = 0;
cf0.FillId = 0;
CellFormat cf = stylesheet.CellFormats.AppendChild(new CellFormat());
cf.Alignment = new Alignment();
cf.ApplyAlignment = true;
cf.NumberFormatId = 0;
cf.FontId = 0;
cf.BorderId = 0;
cf.FillId = 0;
cf.Alignment.WrapText = true;
spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();
}
创建cell时, 对style的引用:
Code
public void WriteNewCell(Row row, string text, string cellName)
{
int index = InsertSharedStringItem2(text, shareStringPart);
Cell cell = new Cell() { CellReference = new StringValue(cellName) };
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
cell.StyleIndex = 1;
row.Append(cell);
}
public void WriteNewCell(Row row, string text, string cellName)
{
int index = InsertSharedStringItem2(text, shareStringPart);
Cell cell = new Cell() { CellReference = new StringValue(cellName) };
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
cell.StyleIndex = 1;
row.Append(cell);
}
注意 cell.StyleIndex = 1; 1 对应的是stylesheet 中的 cellfortmat 的index。