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();
}
查看全文
相关阅读:
Apache Solrj EmbeddedSolrServer使用
Apache Solr实现竞价排名
【Lucene词汇解释】positionIncrement用法
Zoie:基于Lucene实时的搜索引擎系统
Solr的创建者介绍Apache Solr : 基于Lucene的可扩展集群搜索服务器
Solr的自动完成实现方式(第三部分:Suggester方式续)
Solr的自动完成实现方式(第二部分:Suggester方式)
【JAVA基础】HashSet、LinkedHashSet、TreeSet使用区别
Katta:基于Lucene可伸缩分布式实时搜索方案
老罗Android开发视频教程(Android入门介绍)九集集合
原文地址:https://www.cnblogs.com/smallmuda/p/352183.html
最新文章
Standby数据库常见错误处理
关于data guard的结构常识
热点块竞争和解决cache buffers chains
演示一个TDE的数据加密示例,并用logminer验证加密效果
QC中UploadResource的用法
VBS获取命令行参数传值
自动化框架说明
QTP Set Ele_name=description.Create()
ruby安装
[转] 如何复制百度文库中的文章。。。这个必须留一份。。。
热门文章
ant+TestNGxslt生成selenium测试报告
watir添加新标签
ruby正则预查模式的使用
QTP往下拉IE的滚动条
操作QC的附件
QTP之旅前传
testng.xml引用测试类的几种方法
ruby解析XML
test and action parameter
Solr的自动完成实现方式(第一部分:facet方式)
Copyright © 2011-2022 走看看