惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
小众软件
小众软件
爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
美团技术团队
博客园 - 【当耐特】
The Cloudflare Blog
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
IT之家
IT之家
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
WordPress大学
WordPress大学
V
Visual Studio Blog
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队

博客园 - skyfei

Xcode 文档注释方法 查看iOS模拟器应用的沙箱文件 Show in Finder OC代码 C# x86应用x64系统上读取x64位应用的注册表 Make webclient support upload the large file which are larger than 1G Shortcut key for WPF get Android information with adb, the build version Decodes a QuotedPrintable encoded string C# USB Detection - winform and WPF [转] 线程同步 C# 常见面试题(2) 转:C# Interview Questions OpenXML: excel 插入BarChart图表 OpenXML: Asp.net利用OpenXML 导出Excel. TRIGGERS :Cannot use text, ntext, or image columns in the 'inserted' and ' deleted' tables. 'String or binary data would be truncated' error message .net Create Excel 2007 file with open xml 利用.Net Framework2.0 zip压缩、解压 string 数据 C#中文和UNICODE字符转换方法 - skyfei - 博客园
Openxml: 导出excel 设置 cell的格式 - skyfei
skyfei · 2009-07-16 · via 博客园 - skyfei

在cell中如果cell中的文本有换行符, 默认是不显示换行的, 只有点了excel 工具栏中的“Wrap Text" 按钮, 才会显示换行, 见下图:

 

这个效果, 可以通过设置openxml的 style sheet 来实现。

xml

在创建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

创建cell时, 对style的引用:

Code

注意 cell.StyleIndex = 1; 1 对应的是stylesheet 中的 cellfortmat 的index。