Dev原创教程之-Gridview导出EXCEL(多页)
如果大家觉不错请给个赞扬,谢谢!
导出效果如下图所示:
程序界面
EXCEL文件Sheet1
EXCEL文件Sheet2
下面是程序代码:
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraPrintingLinks;
using DevExpress.XtraPrinting;
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("a1");
dt1.Columns.Add("a2");
dt1.Columns.Add("a3");
dt1.Columns.Add("a4");
DataTable dt2 = dt1.Copy();
dt1.Rows.Add("A", "22222", "22222", "22222");
dt1.Rows.Add("B", "33333333333", "33333333333", "3");
dt1.Rows.Add("C", "12", "24", "3");
dt2.Rows.Add("AA", "1", "1", "3");
dt2.Rows.Add("AA", "222", "555", "3");
dt2.Rows.Add("BB", "12", "24", "3");
dt2.Rows.Add("BB", "13", "22222", "3");
gridControl1.DataSource = dt1;
gridControl3.DataSource = dt2;
}
private void Btn_Excel_Click(object sender, EventArgs e)
{
CompositeLink complink = new CompositeLink(new PrintingSystem());
PrintableComponentLink link = new PrintableComponentLink();
link.Component = gridControl1;
PrintableComponentLink link2 = new PrintableComponentLink();
link2.Component = gridControl3;
complink.Links.Add(link);
complink.Links.Add(link2);
complink.CreatePageForEachLink();
complink.ExportToXlsx("file1.xlsx", new XlsxExportOptions() { ExportMode = XlsxExportMode.SingleFilePageByPage });
}
如果大家觉不错请给个赞扬,谢谢!
|