zoukankan
html css js c++ java
winform下导出到execl方法
protected
void
ExportExcel()
{
gridbind();
if
(ds1
==
null
)
return
;
string
saveFileName
=
""
;
//
bool fileSaved=false;
SaveFileDialog saveDialog
=
new
SaveFileDialog();
saveDialog.DefaultExt
=
"
xls
"
;
saveDialog.Filter
=
"
Excel文件|*.xls
"
;
saveDialog.FileName
=
"
Sheet1
"
;
saveDialog.ShowDialog();
saveFileName
=
saveDialog.FileName;
if
(saveFileName.IndexOf(
"
:
"
)
<
0
)
return
;
//
被点了取消
//
excelapp.Workbooks.Open (App.path & \\工程进度表.xls)
Excel.Application xlApp
=
new
Excel.Application();
object
missing
=
System.Reflection.Missing.Value;
if
(xlApp
==
null
)
{
MessageBox.Show(
"
无法创建Excel对象,可能您的机子未安装Excel
"
);
return
;
}
Excel.Workbooks workbooks
=
xlApp.Workbooks;
Excel.Workbook workbook
=
workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet
=
(Excel.Worksheet)workbook.Worksheets[
1
];
//
取得sheet1
Excel.Range range;
string
oldCaption
=
Title_label .Text.Trim ();
long
totalCount
=
ds1.Tables[
0
].Rows.Count;
long
rowRead
=
0
;
float
percent
=
0
;
worksheet.Cells[
1
,
1
]
=
Title_label .Text.Trim ();
//
写入字段
for
(
int
i
=
0
;i
<
ds1.Tables[
0
].Columns.Count;i
++
)
{
worksheet.Cells[
2
,i
+
1
]
=
ds1.Tables[
0
].Columns.ColumnName;
range
=
(Excel.Range)worksheet.Cells[
2
,i
+
1
];
range.Interior.ColorIndex
=
15
;
range.Font.Bold
=
true
;
}
//
写入数值
Caption .Visible
=
true
;
for
(
int
r
=
0
;r
<
ds1.Tables[
0
].Rows.Count;r
++
)
{
for
(
int
i
=
0
;i
<
ds1.Tables[
0
].Columns.Count;i
++
)
{
worksheet.Cells[r
+
3
,i
+
1
]
=
ds1.Tables[
0
].Rows[r];
}
rowRead
++
;
percent
=
((
float
)(
100
*
rowRead))
/
totalCount;
this
.Caption.Text
=
"
正在导出数据[
"
+
percent.ToString(
"
0.00
"
)
+
"
%]
"
;
Application.DoEvents();
}
worksheet.SaveAs(saveFileName,missing,missing,missing,missing,missing,missing,missing,missing);
this
.Caption.Visible
=
false
;
this
.Caption.Text
=
oldCaption;
range
=
worksheet.get_Range(worksheet.Cells[
2
,
1
],worksheet.Cells[ds1.Tables[
0
].Rows.Count
+
2
,ds1.Tables[
0
].Columns.Count]);
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,
null
);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex
=
Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle
=
Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight
=
Excel.XlBorderWeight.xlThin;
if
(ds1.Tables[
0
].Columns.Count
>
1
)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex
=
Excel.XlColorIndex.xlColorIndexAutomatic;
}
workbook.Close(missing,missing,missing);
xlApp.Quit();
}
查看全文
相关阅读:
《将博客搬至CSDN》
选课系统
ATM_购物车
python基础 面向对象编程
python 基础 模块
python基础 函数基础 模块:总复习
第三篇:操作系统基础
浅谈红黑树
浅谈B和B+树
第二篇:网络基础
原文地址:https://www.cnblogs.com/smallmuda/p/352183.html
最新文章
十六、Mysql的数据备份与恢复(一)
十五、Mysql数据库日志
十四、InnoDB的ACID事务
十三、Mysql的存储引擎
十二、Mysql的索引
十一、Mysql常见的show命令及information_schema.tables视图说明
python day28--json,pickle,hashlib,logging
excel图片链接转图片
天气爬虫
python day27--常用模块 time,random,os,序列化
热门文章
python day 25--正则表达式
python day18--面向对象,继承
python day21 ——面向对像-反射 getattr,内置方法
python day20面向对象-属性,类方法,静态方法
python day19--面向对象,接口,封装
python day17面向对象-组合
放优酷系统
mysql 了解
pymysql 连接的基本使用,注入问题。
选课系统
Copyright © 2011-2022 走看看