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();
}
查看全文
相关阅读:
python之基础知识篇
OpenGL 着色器管理类 Shader
OpenGL 彩色三角形
OpenGL 你好,三角形 练习一 二 三
OpenGL 矩阵绘画
OpenGL 第一个三角形!!!!
OpenGL 读取,使用 .vert .frag 着色器 文件 C++
C++ 一定要使用strcpy_s()函数 等来操作方法c_str()返回的指针
OpenGL 入门指南(配置GLFW ,着色语言高亮,以及一些常见问题)
汽车加工厂
原文地址:https://www.cnblogs.com/smallmuda/p/352183.html
最新文章
如何获取url上面的参数
jS判断浏览器终端
o2o的一些看法
重来
windows phone版的一个儿教app
重构与模式
西安的那两个月
Android里面的命名规范
windows phone 新手引导
windows phone listbox虚拟化(下)
热门文章
Windows Phone Emoji
典型用户和用户场景分析
第四次站立会议
第三次站立会议
第二次站立会议
第一次站立会议
团队项目电梯会议视频
团队题目需求分析-NABCD
找“水王”
python之文件操作
Copyright © 2011-2022 走看看