gzsxy8013
导航
博客园
首页
新随笔
联系
订阅
管理
<
2005年11月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
统计
随笔 - 16
文章 - 29
评论 - 9
引用 - 0
公告
昵称:
申晓园
园龄:
6年5个月
粉丝:
0
关注:
0
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
随笔档案
(16)
2005年12月 (1)
2005年11月 (14)
2005年10月 (1)
文章档案
(29)
2006年6月 (1)
2006年5月 (1)
2006年4月 (3)
2005年11月 (8)
2005年10月 (16)
cnblogs收藏
::吞硬币的小猪::
『 天道酬勤 』
12个.net 开发者值得去读的国外Blog
AntingZ的专栏
Bonny.Wong:致力于企业信息化。。。
dudu-快乐程序员
erp软件
flygoldfish(长流支流)的专栏 争取开创网络程序员新职业先河 继《金制打印通》之后,隆重共享《WebMIS.NET快速开发平台》
haibodotnet的水晶报表世界
http://esshs.cnblogs.com/
http://www.kaifz.com/
IT 菜园子
Rickie Lee's blog
System Information--任宇
uml论坛
vb delphi vc asp.net
VB.NET初学者园地
程序员的世界
工作流的网站
蝈蝈俊.net
嗨,您好!欢迎到zjcxc(邹建)的专栏>
继《金制打印通》之后,隆重共享《WebMIS.NET快速开发平台》
进销存软件
骆驼刺的专栏
门斧的专栏
孟子e章专栏
闵峰csdn
青润心情
阮高的blog
软件名称: Rational Rose 2003
思归的专栏
速马的专栏
随风.NET点滴
王洪生的BLog
闲云野鹤的专栏
小山
一个轻量级的数据访问解决方案
愚翁专栏
愚翁专栏
云中客的专栏
erp资料
http://www.fjit.com.cn/smb/
it168解决
SMB生态调查二:“第三梯队”信息化之“怪”现状
计世网
中国国家企业网
java
javascript
http://www.activewidgets.com/javascript.forum.473.2/onenter-event.html
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20834313.html
http://www.javascriptfr.com
http://www.javascriptworld.com/js4e/scripts/index.html
http://www.zdnet.com.cn/developer/webdevelop/story/0,3800067013,39391180,00.htm
互联网页面浏览限制实现总结
用Javascript制作一个可自动填写的文本框(全文完) 选择自 yjgx007 的 Blog
net资料
Convert VB.NET to C#
http://www.chinaitpower.com/Dev/Programme/Net/
http://www.syncfusion.com
pdm
pdm
uml
uml 软件以用为本
个人技术网站
51windows.net
http://www.zahui.com
吴 迪 专栏
实事政治讨论
sohu评论
我的收藏连接
C# ->VB.NET
c#国外开发网站
c-sharpcorner
datagrid girl
DotNetBips
DotnetJoin
dotnetjunkies
Eggheadcafe
MSDN技术归档
MSDN中关于winform的FAQ
MSDN中文网站
ORACLE中国用户讨论组
SourceForge.NET
support.apress.com
the code project
theserverside.net
vb.net国外开发网站
vbcity.com
vb国外网站
windows forms FAQ
windows forms快速入门
windowsforms.net
www.tech-archive.net/
报表网站
编程爱好者
插件简介:一个基于.net平台的可以替代VSS版本控制工具采用XML Web Services作为通信协议.
多线程
反编译工具下载
高海东的技术资料
共创软件联盟
共享软件下载
关于SOCKETS
华录集团
欢迎光临Soft8.Net[中国首家专业软件搜索引擎],在这里能轻松地找到几乎所有的软件,以后您要找软件请来这里
慧都控件网
技术文档
开发net应用系统架构
开源网站
柳州市制造业信息化促进中心
麦高网
闵峰的专栏
如何设计一个继承自collection的类
商业智能
商业智能cnblogs
上海朗风条码-中国最权威的条码解答网站
水如烟•村的专栏
天天接活
天新网
文档在线
系统分析
下载110
学海
椰子林DotNet的天空
应用软件下载
源码天下
源码下载
支点企业信息化
中国.NET俱乐部
中国ActiveX控件下载网 - 程序员之友
中国DOTNET俱乐部
中国网盟
中文版权所有:虎霸下载在线
资讯网
组件的设计时特性
最好的.net反编译工具
资源下载网站
三好在线
最新评论
推荐排行榜
C#操作Excel,套用模板并对数据进行分页
1
using
System;
2
using
System.IO;
3
using
System.Data;
4
using
System.Reflection;
5
using
System.Diagnostics;
6
using
cfg
=
System.Configuration;
7
//
using Excel;
8
9
namespace
ExcelHelperTest
10
{
11
/**/
///
<summary>
12
///
功能说明:套用模板输出Excel,并对数据进行分页
13
///
作 者:Lingyun_k
14
///
创建日期:2005-7-12
15
///
</summary>
16
public
class
ExcelHelper
17
{
18
protected
string
templetFile
=
null
;
19
protected
string
outputFile
=
null
;
20
protected
object
missing
=
Missing.Value;
21
22
/**/
///
<summary>
23
///
构造函数,需指定模板文件和输出文件完整路径
24
///
</summary>
25
///
<param name="templetFilePath">
Excel模板文件路径
</param>
26
///
<param name="outputFilePath">
输出Excel文件路径
</param>
27
public
ExcelHelper(
string
templetFilePath,
string
outputFilePath)
28
{
29
if
(templetFilePath
==
null
)
30
throw
new
Exception(
"
Excel模板文件路径不能为空!
"
);
31
32
if
(outputFilePath
==
null
)
33
throw
new
Exception(
"
输出Excel文件路径不能为空!
"
);
34
35
if
(
!
File.Exists(templetFilePath))
36
throw
new
Exception(
"
指定路径的Excel模板文件不存在!
"
);
37
38
this
.templetFile
=
templetFilePath;
39
this
.outputFile
=
outputFilePath;
40
41
}
42
43
/**/
///
<summary>
44
///
将DataTable数据写入Excel文件(套用模板并分页)
45
///
</summary>
46
///
<param name="dt">
DataTable
</param>
47
///
<param name="rows">
每个WorkSheet写入多少行数据
</param>
48
///
<param name="top">
行索引
</param>
49
///
<param name="left">
列索引
</param>
50
///
<param name="sheetPrefixName">
WorkSheet前缀名,比如:前缀名为“Sheet”,那么WorkSheet名称依次为“Sheet-1,Sheet-2
”
</param>
51
public
void
DataTableToExcel(DataTable dt,
int
rows,
int
top,
int
left,
string
sheetPrefixName)
52
{
53
int
rowCount
=
dt.Rows.Count;
//
源DataTable行数
54
int
colCount
=
dt.Columns.Count;
//
源DataTable列数
55
int
sheetCount
=
this
.GetSheetCount(rowCount,rows);
//
WorkSheet个数
56
DateTime beforeTime;
57
DateTime afterTime;
58
59
if
(sheetPrefixName
==
null
||
sheetPrefixName.Trim()
==
""
)
60
sheetPrefixName
=
"
Sheet
"
;
61
62
//
创建一个Application对象并使其可见
63
beforeTime
=
DateTime.Now;
64
Excel.Application app
=
new
Excel.ApplicationClass();
65
app.Visible
=
true
;
66
afterTime
=
DateTime.Now;
67
68
//
打开模板文件,得到WorkBook对象
69
Excel.Workbook workBook
=
app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing,
70
missing,missing,missing,missing,missing,missing,missing);
71
72
//
得到WorkSheet对象
73
Excel.Worksheet workSheet
=
(Excel.Worksheet)workBook.Sheets.get_Item(
1
);
74
75
//
复制sheetCount-1个WorkSheet对象
76
for
(
int
i
=
1
;i
<
sheetCount;i
++
)
77
{
78
((Excel.Worksheet)workBook.Worksheets.get_Item(i)).Copy(missing,workBook.Worksheets[i]);
79
}
80
81
将源DataTable数据写入Excel
#region
将源DataTable数据写入Excel
82
for
(
int
i
=
1
;i
<=
sheetCount;i
++
)
83
{
84
int
startRow
=
(i
-
1
)
*
rows;
//
记录起始行索引
85
int
endRow
=
i
*
rows;
//
记录结束行索引
86
87
//
若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
88
if
(i
==
sheetCount)
89
endRow
=
rowCount;
90
91
//
获取要写入数据的WorkSheet对象,并重命名
92
Excel.Worksheet sheet
=
(Excel.Worksheet)workBook.Worksheets.get_Item(i);
93
sheet.Name
=
sheetPrefixName
+
"
-
"
+
i.ToString();
94
95
//
将dt中的数据写入WorkSheet
96
for
(
int
j
=
0
;j
<
endRow
-
startRow;j
++
)
97
{
98
for
(
int
k
=
0
;k
<
colCount;k
++
)
99
{
100
sheet.Cells[top
+
j,left
+
k]
=
dt.Rows[startRow
+
j][k].ToString();
101
}
102
}
103
104
//
写文本框数据
105
Excel.TextBox txtAuthor
=
(Excel.TextBox)sheet.TextBoxes(
"
txtAuthor
"
);
106
Excel.TextBox txtDate
=
(Excel.TextBox)sheet.TextBoxes(
"
txtDate
"
);
107
Excel.TextBox txtVersion
=
(Excel.TextBox)sheet.TextBoxes(
"
txtVersion
"
);
108
109
txtAuthor.Text
=
"
KLY.NET的Blog
"
;
110
txtDate.Text
=
DateTime.Now.ToShortDateString();
111
txtVersion.Text
=
"
1.0.0.0
"
;
112
}
113
#endregion
114
115
//
输出Excel文件并退出
116
try
117
{
118
workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
119
workBook.Close(
null
,
null
,
null
);
120
app.Workbooks.Close();
121
app.Application.Quit();
122
app.Quit();
123
124
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
125
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
126
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
127
128
workSheet
=
null
;
129
workBook
=
null
;
130
app
=
null
;
131
132
GC.Collect();
133
}
134
catch
(Exception e)
135
{
136
throw
e;
137
}
138
finally
139
{
140
Process[] myProcesses;
141
DateTime startTime;
142
myProcesses
=
Process.GetProcessesByName(
"
Excel
"
);
143
144
//
得不到Excel进程ID,暂时只能判断进程启动时间
145
foreach
(Process myProcess
in
myProcesses)
146
{
147
startTime
=
myProcess.StartTime;
148
149
if
(startTime
>
beforeTime
&&
startTime
<
afterTime)
150
{
151
myProcess.Kill();
152
}
153
}
154
}
155
156
}
157
158
159
/**/
///
<summary>
160
///
获取WorkSheet数量
161
///
</summary>
162
///
<param name="rowCount">
记录总行数
</param>
163
///
<param name="rows">
每WorkSheet行数
</param>
164
private
int
GetSheetCount(
int
rowCount,
int
rows)
165
{
166
int
n
=
rowCount
%
rows;
//
余数
167
168
if
(n
==
0
)
169
return
rowCount
/
rows;
170
else
171
return
Convert.ToInt32(rowCount
/
rows)
+
1
;
172
}
173
174
175
/**/
///
<summary>
176
///
将二维数组数据写入Excel文件(套用模板并分页)
177
///
</summary>
178
///
<param name="arr">
二维数组
</param>
179
///
<param name="rows">
每个WorkSheet写入多少行数据
</param>
180
///
<param name="top">
行索引
</param>
181
///
<param name="left">
列索引
</param>
182
///
<param name="sheetPrefixName">
WorkSheet前缀名,比如:前缀名为“Sheet”,那么WorkSheet名称依次为“Sheet-1,Sheet-2
”
</param>
183
public
void
ArrayToExcel(
string
[,] arr,
int
rows,
int
top,
int
left,
string
sheetPrefixName)
184
{
185
int
rowCount
=
arr.GetLength(
0
);
//
二维数组行数(一维长度)
186
int
colCount
=
arr.GetLength(
1
);
//
二维数据列数(二维长度)
187
int
sheetCount
=
this
.GetSheetCount(rowCount,rows);
//
WorkSheet个数
188
DateTime beforeTime;
189
DateTime afterTime;
190
191
if
(sheetPrefixName
==
null
||
sheetPrefixName.Trim()
==
""
)
192
sheetPrefixName
=
"
Sheet
"
;
193
194
//
创建一个Application对象并使其可见
195
beforeTime
=
DateTime.Now;
196
Excel.Application app
=
new
Excel.ApplicationClass();
197
app.Visible
=
true
;
198
afterTime
=
DateTime.Now;
199
200
//
打开模板文件,得到WorkBook对象
201
Excel.Workbook workBook
=
app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing,
202
missing,missing,missing,missing,missing,missing,missing);
203
204
//
得到WorkSheet对象
205
Excel.Worksheet workSheet
=
(Excel.Worksheet)workBook.Sheets.get_Item(
1
);
206
207
//
复制sheetCount-1个WorkSheet对象
208
for
(
int
i
=
1
;i
<
sheetCount;i
++
)
209
{
210
((Excel.Worksheet)workBook.Worksheets.get_Item(i)).Copy(missing,workBook.Worksheets[i]);
211
}
212
213
将二维数组数据写入Excel
#region
将二维数组数据写入Excel
214
for
(
int
i
=
1
;i
<=
sheetCount;i
++
)
215
{
216
int
startRow
=
(i
-
1
)
*
rows;
//
记录起始行索引
217
int
endRow
=
i
*
rows;
//
记录结束行索引
218
219
//
若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
220
if
(i
==
sheetCount)
221
endRow
=
rowCount;
222
223
//
获取要写入数据的WorkSheet对象,并重命名
224
Excel.Worksheet sheet
=
(Excel.Worksheet)workBook.Worksheets.get_Item(i);
225
sheet.Name
=
sheetPrefixName
+
"
-
"
+
i.ToString();
226
227
//
将二维数组中的数据写入WorkSheet
228
for
(
int
j
=
0
;j
<
endRow
-
startRow;j
++
)
229
{
230
for
(
int
k
=
0
;k
<
colCount;k
++
)
231
{
232
sheet.Cells[top
+
j,left
+
k]
=
arr[startRow
+
j,k];
233
}
234
}
235
236
Excel.TextBox txtAuthor
=
(Excel.TextBox)sheet.TextBoxes(
"
txtAuthor
"
);
237
Excel.TextBox txtDate
=
(Excel.TextBox)sheet.TextBoxes(
"
txtDate
"
);
238
Excel.TextBox txtVersion
=
(Excel.TextBox)sheet.TextBoxes(
"
txtVersion
"
);
239
240
txtAuthor.Text
=
"
KLY.NET的Blog
"
;
241
txtDate.Text
=
DateTime.Now.ToShortDateString();
242
txtVersion.Text
=
"
1.0.0.0
"
;
243
}
244
#endregion
245
246
//
输出Excel文件并退出
247
try
248
{
249
workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing);
250
workBook.Close(
null
,
null
,
null
);
251
app.Workbooks.Close();
252
app.Application.Quit();
253
app.Quit();
254
255
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
256
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
257
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
258
259
workSheet
=
null
;
260
workBook
=
null
;
261
app
=
null
;
262
263
GC.Collect();
264
}
265
catch
(Exception e)
266
{
267
throw
e;
268
}
269
finally
270
{
271
Process[] myProcesses;
272
DateTime startTime;
273
myProcesses
=
Process.GetProcessesByName(
"
Excel
"
);
274
275
//
得不到Excel进程ID,暂时只能判断进程启动时间
276
foreach
(Process myProcess
in
myProcesses)
277
{
278
startTime
=
myProcess.StartTime;
279
280
if
(startTime
>
beforeTime
&&
startTime
<
afterTime)
281
{
282
myProcess.Kill();
283
}
284
}
285
}
286
287
}
288
}
289
}
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted on 2005-11-09 13:15
申晓园
阅读(201)
评论(0)
编辑
收藏
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
谷歌将出售Clearwire股份 不到收购价1/10
·
为什么Google比苹果更令微软恐惧?
·
思科拟2.71亿美元收购私有公司Lightwire
·
社交商务公司Bazaarvoice将于2月24日进行IPO
·
戴尔收购备份软件公司AppAssure
»
更多新闻...
最新知识库文章
:
·
领域模型管理与AOP
·
编程的艺术:漂亮的代码和漂亮的软件
·
GIT分支管理是一门艺术
·
编程:是一门艺术
·
编程是一门艺术吗?
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
Powered by:
博客园
Copyright © 申晓园