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

推荐订阅源

K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
GbyAI
GbyAI
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
D
Docker
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
美团技术团队
The Register - Security
The Register - Security
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
量子位
B
Blog
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
L
LangChain Blog
Latest news
Latest news
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
T
Tenable Blog
T
Tor Project blog

博客园 - 风也无奈

事件学习 信息系统需求分析阶段的实践经验之二---如何有效地获得用户需求【转】 信息系统需求分析阶段的实践经验之一---需求分析概述[转] Lambda表达式【转】 C#委托的介绍(delegate、Action、Func、predicate)【转】 “DllRegisterServer的调用失败”问题解决办法 什么是RFID技术 SQL Server集群服务器的优缺点 asp中的ckEditor的详细配置 UTF8转成GB2312乱码问题解决思路 winform更新程序代码 C#开发串口总结,并提炼串口辅助类到公用类库中 SQLXML]FOR XML语法导出XML的易错之处 C#下载功能代码<转> C#通用类库--QQ吸附窗体类 C#通用类库--数字转为人民币汉字大写表示 - 风也无奈 - 博客园 link和@import的区别 - 风也无奈 - 博客园 Winform中的DataGridView控件内容自动保存《转载》 SQL中获取一个长字符串中某个字符串出现次数的简单方法
C#操作word并格式化
风也无奈 · 2010-11-09 · via 博客园 - 风也无奈

1.    概述

C#基于com组件将数据导入到word中分栏并格式化。

2.    准备步骤

A.        在项目中添加引用Microsoft Word 11.0 Object Library

B.        引入命名空间

using Microsoft.Office.Core;

using MSWord = Word;

3.    思路

A.      首先创建word应用程序(MSWord.Application)word文档对象(MSWord.Document)

MSWord.Application wordApp;           //Word应用程序变量

MSWord.Document wordDoc;              //Word文档变量

B.      初始化并向word应用程序添加一个word文档。

wordApp = new MSWord.ApplicationClass(); //初始化

//由于使用的是COM库,因此有许多变量需要用Missing.Value代替

Object Nothing = Missing.Value;

wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

C.      设置分栏:分栏有整篇文档的分栏和选中部分的分栏

Ø        整篇文档的分栏:

直接设置wordDoc.PageSetup.TextColumns.SetCount(count);count为1-45的整数。

Ø        选中部分的分栏:

设置选中的区域Range,wordDoc.Range(ref start, ref end).Select(),start开始位置和end结束位置必须为oject,之后分栏wordDoc.Sections.PageSetup.TextColumns.SetCount(2), Sections表示刚选中的部分。

再加上object nt = WdBreakType.wdSectionBreakContinuous;

wordApp.ActiveDocument.Range(refend, ref end).InsertBreak(ref nt);具体意思不太清楚,但是如果不加就不能产生选中部分的分栏。

D.    设置字体的样式,段落的缩进:

Ø        首先要选中设置字体的区域,之后设置选中区域字体Font。

Ø        段落缩进IndentCharWidth(charcount),charcount表示缩进的字符,

Ø        段落的悬挂缩进TabHangingIndent(charcount).

E.      设置表格的样式:

//定义一个Word中的表格对象

MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, rows, columns, ref Nothing, ref Nothing);

Ø        合并单元格:

table.Cell(1, 1).Merge(table.Cell(2, 1));//表示从第几行的第几列到第几行的第几列合并。

Ø        设置重复标题行:

table.Rows[1].HeadingFormat = (int)WdConstants.wdToggle;

F.      设置文档格式并保存:

//WdSaveFormat为Word 2003文档的保持格式wdFormatDocument;

object format = MSWord.WdSaveFormat.wdFormatDocument;

//将wordDoc文档对象的内容保存为DOCX文档

wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

//关闭wordDoc文档对象

wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);

//关闭wordApp组件对象

wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);