Openxml: 导出excel 设置 cell的格式
在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>
?
在創建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();
????????}
?
創建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);
????????}
?
注意 cell.StyleIndex = 1; 1 對應的是stylesheet 中的 cellfortmat 的index。
?
轉載于:https://www.cnblogs.com/skyfei/archive/2009/07/16/1524695.html
總結
以上是生活随笔為你收集整理的Openxml: 导出excel 设置 cell的格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Google 发布浏览器3D插件 O3D
- 下一篇: SilverLight学习笔记--建立S