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();
}
查看全文
相关阅读:
范仁义css3课程---7、文本样式2
android图片缓存框架Android-Universal-Image-Loader(二)
Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读
憨人 音译
Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解
Android开发
87狂热
迟志强
翟惠民
原文地址:https://www.cnblogs.com/smallmuda/p/352183.html
最新文章
mini2440驱动奇谭——ADC驱动与測试(动态挂载驱动)
(转)使用Spring的注解方式实现AOP的细节
(转)使用Spring的注解方式实现AOP入门
(转)使用CGLIB实现AOP功能与AOP概念解释
(转)使用JDK中的Proxy技术实现AOP功能
(转)让Spring自动扫描和管理Bean
(转)@Autowire注解与自动装配
(转)编码剖析@Resource注解的实现原理
(转)用@Resource注解完成属性装配
(转)Spring如何装配各种集合类型的属性
热门文章
(转)编码剖析Spring装配基本属性的原理
范仁义css3课程---13、尺寸
范仁义css3课程---12、盒子模型小实例
min.css和普通css有什么不同
laravel如何去掉google相关的css(因为速度慢)
范仁义css3课程---11、外边距margin
范仁义css3课程---10、内边距
范仁义css3课程---9、边框
范仁义css3课程---8、盒模型
盒模型总结
Copyright © 2011-2022 走看看