DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表
2017-12-25 13:29
357 查看
文章出自深圳-淘神网友提供方案,在此仅做记录,谢谢。
public static class ViewExport
{ /// <summary>
/// DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表
/// eg:ExportToXlsx("",gridControl1,gridControl2);
/// 将gridControl1和gridControl2的数据一同导出到同一张工作表
/// </summary>
/// <param name="title">文件名</param>
/// <param name="panels">控件集</param>
public static void ExportToExcel ( string title ,params IPrintable [ ] panels )
{
SaveFileDialog saveFileDialog = new SaveFileDialog ( );
saveFileDialog . FileName = title;
saveFileDialog . Title = "导出Excel";
saveFileDialog . Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls";
DialogResult dialogResult = saveFileDialog . ShowDialog ( );
if ( dialogResult == DialogResult . Cancel )
return;
string FileName = saveFileDialog . FileName;
PrintingSystem ps = new PrintingSystem ( );
CompositeLink link = new CompositeLink ( ps );
ps . Links . Add ( link );
foreach ( IPrintable panel in panels )
{
link . Links . Add ( CreatePrintableLink ( panel ) );
}
link . Landscape = true;//横向
//判断是否有标题,有则设置
//link.CreateDocument(); //建立文档
try
{
int count = 1;
//在重复名称后加(序号)
while ( File . Exists ( FileName ) )
{
if ( FileName . Contains ( ")." ) )
{
int start = FileName . LastIndexOf ( "(" );
int end = FileName . LastIndexOf ( ")." ) - FileName . LastIndexOf ( "(" ) + 2;
FileName = FileName . Replace ( FileName . Substring ( start ,end ) ,string . Format ( "({0})." ,count ) );
}
else
{
FileName = FileName . Replace ( "." ,string . Format ( "({0})." ,count ) );
}
count++;
}
if ( FileName . LastIndexOf ( ".xlsx" ) >= FileName . Length - 5 )
{
XlsxExportOptions options = new XlsxExportOptions ( );
link . ExportToXlsx ( FileName ,options );
}
else
{
XlsExportOptions options = new XlsExportOptions ( );
link . ExportToXls ( FileName ,options );
}
if ( DevExpress . XtraEditors . XtraMessageBox . Show ( "保存成功,是否打开文件?" ,"提示" ,MessageBoxButtons . YesNo ,MessageBoxIcon . Information ) == DialogResult . Yes )
System . Diagnostics . Process . Start ( FileName );//打开指定路径下的文件
}
catch ( Exception ex )
{
DevExpress . XtraEditors . XtraMessageBox . Show ( ex . Message );
}
}
/// <summary>
/// 创建打印Componet
/// </summary>
/// <param name="printable"></param>
/// <returns></returns>
static PrintableComponentLink CreatePrintableLink ( IPrintable printable )
{
ChartControl chart = printable as ChartControl;
if ( chart != null )
chart . OptionsPrint . SizeMode = DevExpress . XtraCharts . Printing . PrintSizeMode . Stretch;
PrintableComponentLink printableLink = new PrintableComponentLink ( ) { Component = printable };
return printableLink;
}
}调用方法:
ViewExport . ExportToExcel ( "GridControl内容导出" ,gridControl1 );
public static class ViewExport
{ /// <summary>
/// DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表
/// eg:ExportToXlsx("",gridControl1,gridControl2);
/// 将gridControl1和gridControl2的数据一同导出到同一张工作表
/// </summary>
/// <param name="title">文件名</param>
/// <param name="panels">控件集</param>
public static void ExportToExcel ( string title ,params IPrintable [ ] panels )
{
SaveFileDialog saveFileDialog = new SaveFileDialog ( );
saveFileDialog . FileName = title;
saveFileDialog . Title = "导出Excel";
saveFileDialog . Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls";
DialogResult dialogResult = saveFileDialog . ShowDialog ( );
if ( dialogResult == DialogResult . Cancel )
return;
string FileName = saveFileDialog . FileName;
PrintingSystem ps = new PrintingSystem ( );
CompositeLink link = new CompositeLink ( ps );
ps . Links . Add ( link );
foreach ( IPrintable panel in panels )
{
link . Links . Add ( CreatePrintableLink ( panel ) );
}
link . Landscape = true;//横向
//判断是否有标题,有则设置
//link.CreateDocument(); //建立文档
try
{
int count = 1;
//在重复名称后加(序号)
while ( File . Exists ( FileName ) )
{
if ( FileName . Contains ( ")." ) )
{
int start = FileName . LastIndexOf ( "(" );
int end = FileName . LastIndexOf ( ")." ) - FileName . LastIndexOf ( "(" ) + 2;
FileName = FileName . Replace ( FileName . Substring ( start ,end ) ,string . Format ( "({0})." ,count ) );
}
else
{
FileName = FileName . Replace ( "." ,string . Format ( "({0})." ,count ) );
}
count++;
}
if ( FileName . LastIndexOf ( ".xlsx" ) >= FileName . Length - 5 )
{
XlsxExportOptions options = new XlsxExportOptions ( );
link . ExportToXlsx ( FileName ,options );
}
else
{
XlsExportOptions options = new XlsExportOptions ( );
link . ExportToXls ( FileName ,options );
}
if ( DevExpress . XtraEditors . XtraMessageBox . Show ( "保存成功,是否打开文件?" ,"提示" ,MessageBoxButtons . YesNo ,MessageBoxIcon . Information ) == DialogResult . Yes )
System . Diagnostics . Process . Start ( FileName );//打开指定路径下的文件
}
catch ( Exception ex )
{
DevExpress . XtraEditors . XtraMessageBox . Show ( ex . Message );
}
}
/// <summary>
/// 创建打印Componet
/// </summary>
/// <param name="printable"></param>
/// <returns></returns>
static PrintableComponentLink CreatePrintableLink ( IPrintable printable )
{
ChartControl chart = printable as ChartControl;
if ( chart != null )
chart . OptionsPrint . SizeMode = DevExpress . XtraCharts . Printing . PrintSizeMode . Stretch;
PrintableComponentLink printableLink = new PrintableComponentLink ( ) { Component = printable };
return printableLink;
}
}调用方法:
ViewExport . ExportToExcel ( "GridControl内容导出" ,gridControl1 );
相关文章推荐
- 实现server程序,支持多个client同时连接,为每一个client分配一个进程
- 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)
- ZedGraph:一个同时支持WinForm和WebForm的开源图表控件(基于LGPL协议,.NET 2.0 C#源代码)
- javaWeb不允许两个客户端同时使用一个账号登录系统(支持多个tomcat)
- 最近在写导出excel表格的代码,发现网上大部分代码都是导出简单格式的excel,所以自己顺便就写了一个导出复杂表格的工具类,有些代码是借鉴网友的,同时也谢谢分享代码的朋友们.
- EXCEL增加审计功能,记录每个单元格的修改日志(支持记录多个单元格同时修改时记录审计信息)
- 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)
- excel之多个单元格同时除以一个数
- 【JS】导出table到excel,同时兼容FF和IE
- 使用phpExcel库构建一个导出excel的通用类
- 修改内核文件使系统同时支持多个特定usbserial设备
- datagridview控件--导出Excel
- nginx 同时监听uwsgi服务器两个端口,实现一个服务器运行多个项目
- 让一个软件系统同时支持多个多种数据库灵活配置,让客户可以任意配置各个核心数据库部分
- 如何在Excel里单击一个单元格弹出一个日期选择控件,选择日期值后返回给该单元格。
- C#将一个excel工作表根据指定范围拆分为多个excel文件
- 导出excel时数量太多的一个方法
- Java利用xml将大批量数据导出到excel的一个方法