- 4
- 0
- 约3.21千字
- 约 5页
- 2021-02-05 发布于天津
- 举报
C
C转导出EXCEL方法大全
PAGE
PAGE #
C#导出Excel方法大全
-CAL-FENGHAL-
C#中常需要把报表数据导出Excel,下面详细介绍6种方法:
第一种方法比较罗嗦,主要介绍如何调用,含注释给不熟导出EXCEL者知其原 理
第二方法直接套用,代码简单明了。个人推荐这种,直接套用,非常方便。 第三到第六种就不一一介绍了。
方法1
页面增加一个按钮,单击事件添加如下方法:
protected void Butt on l_Click(object sender, Eve ntArgs e)
{
ExportCapplication/ms-excel,学生信息列表.xlsH);
}
private void Export(string FileType, string FileName)
{
=,,GB2312H;
= (Content-Disposition, ,,attachment;filename= + (FileName, .ToStringO);
=FileType;
=false;
StringWriter tw = new StringWriter();
HtmITextWriter hw = new HtmlTextWriter(tw);
(hw);
0);
0;
}
ris11);
}
private void Export(string FileType, string FileName)
{
=,IGB2312;
= (Content?Disposition舄attachment;filename= + (FileName,?ToString());
=FileType;
=false;
StringWriter tw = new StringWriter();
HtmITextWriter hw = new HtmlTextWriter(tw);
(hw);
0);
0;
}
public override void VerifyRenderinglnServerForm(Control control)
PAGE
PAGE #
}
前台:
asp:GridView ID=l,GridViewl,1 runat=,,server11 AutoGenerateColumns=,,False11
CellPadding=,,3
BackColor=,,Whiteu BorderColor=,,#CCCCCCn BorderStyle=,,None,1 BorderWidth=l,lpxu Font-Size=,,12pxn
FooterStyle BackColor=,,White,1 ForeColor=,,#000066n /
Colum ns
asp:BoundField DataField=学号HeaderText=学号
Readonly-True” /
asp:BoundField DataField=n姓名11 HeaderText=H姓名,,/ asp:BoundField DataField=tB生日期HeaderText=出生 H J5J / asp:BoundField DataField=H专业” HeaderText=H专业” / asp:BoundField DataField=学院HeaderText=学院/
/Colum ns
RowStyle ForeColor=,,#000066H /
SelectedRowStyle BackColor=,,#669999n Font-Bold=,,True11 ForeColor=,,White,1 /
PagerStyle BackColor=,,Whiten ForeColor=,,#000066, HorizontalAlign=,,Left,, CssClass=,,ms-formlabel DataGridFixedHeader7
HeaderStyle BackColor=,,#006699, Font-Bold=,,True11
ForeColor二,,White /
/asp:GridView
asp:Button ID=l,Buttonln runat=,,serverH OnClick=”Buttonl_Click“ Text』导出“ /
读取Excel数据的代码
private DataSet CreateDataSource()
{
string strCon;
strCon = nProvider= Source』+ (,H,) + Extended Properties=Excel
OleDbConnection olecon 二 new OleDbConnec
原创力文档

文档评论(0)