usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Windows.Forms;usingDevExpress.XtraPrinting;usingDevExpress.XtraPrintingLinks;namespaceDevWinApp
{public partial classForm1 : Form
{publicForm1()
{
InitializeComponent();
DataTable dt= new DataTable("test");
dt.Columns.Add(new DataColumn("col1", typeof(string)));
dt.Columns.Add(new DataColumn("col2", typeof(string)));
dt.Columns.Add(new DataColumn("col3", typeof(string)));
dt.Columns.Add(new DataColumn("col4", typeof(string)));for (int i = 0; i <= 6; i++)
{
DataRow dr=dt.NewRow();foreach (DataColumn dc indt.Columns)
{
dr[dc]= string.Format("行:{0},列:{1}", i + 1, dc.ColumnName);
}
dt.Rows.Add(dr);
i++;
}
gridControl1.DataSource=dt.DefaultView;
DevExpress.XtraPrinting.XlsxExportOptions xlsxExport= newDevExpress.XtraPrinting.XlsxExportOptions();
gridView1.ExportToXlsx(@"E:\ExcelTest01.xlsx", xlsxExport);
DataTable dt2=dt.Copy();foreach (DataRow dr indt2.Rows)
{foreach (DataColumn dc indt2.Columns)
{
dr[dc]= string.Format("第二Sheet,", dr[dc]);
}
}
gridControl2.DataSource=dt2.DefaultView;
DevExpress.XtraGrid.GridControl[] grids={ gridControl1, gridControl2 };var defautps = newDevExpress.XtraPrinting.PrintingSystem();
CompositeLink compLink= newCompositeLink(defautps);foreach (var gd ingrids)
{
PrintableComponentLink pclink= newPrintableComponentLink();
pclink.Component=gd;
compLink.Links.Add(pclink);
}#region 分Sheet
//compLink.CreatePageForEachLink();//用于每个Link生成一个Sheet,不使用此方法,则合并在一个Sheet内//compLink.ExportToXlsx(@"D:\DevExcel.xlsx", new XlsxExportOptions() { ExportMode = XlsxExportMode.SingleFilePageByPage });
#endregion 分Sheet
#region 放同一Sheet (须屏蔽 "分Sheet")compLink.ExportToXlsx(@"D:\DevExcel.xlsx");#endregion 放同一Sheet}
}
}