using DevExpress.XtraLayout;
using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JCZHTS.Common.UI
{
public class ExportUtil
{
public static void exportGridControl(DevExpress.XtraGrid.GridControl gridcontrol)
{
SaveFileDialog frm = new SaveFileDialog();
frm.Filter = "(*.xlsx)|*.xlsx";
frm.FileName = "新文件" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (frm.CheckFileExists)
{
if (!(MessageBox.Show("该文件已经存在是否覆盖该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)) return;
}
string fileName = frm.FileName;
//XlsExportOptions options = new XlsExportOptions();
XlsxExportOptions options = new XlsxExportOptions();
//options.TextExportMode = TextExportMode.Text;
options.TextExportMode = TextExportMode.Value;
options.RawDataMode = false;
//gridcontrol.ExportToXls(fileName, options);
gridcontrol.ExportToXlsx(fileName, options);
if ((MessageBox.Show("是否打开该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
{
System.Diagnostics.Process.Start(fileName);
}
}
}
public static void exportGridControl(DevExpress.XtraGrid.GridControl gridcontrol, string fileName)
{
SaveFileDialog frm = new SaveFileDialog();
frm.Filter = "(*.xls)|*.xls";
frm.FileName = fileName;
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (frm.CheckFileExists)
{
if (!(MessageBox.Show("该文件已经存在是否覆盖该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)) return;
}
XlsExportOptions options = new XlsExportOptions();
//options.TextExportMode = TextExportMode.Text;
options.TextExportMode = TextExportMode.Value;
options.RawDataMode = false;
gridcontrol.ExportToXls(frm.FileName, options);
if ((MessageBox.Show("是否打开该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
{
System.Diagnostics.Process.Start(frm.FileName);
}
}
}
/// <summary>
/// 导出为xlsx
/// </summary>
/// <param name="gridcontrol"></param>
/// <param name="fileName"></param>
public static void exportGridControlToxlsx(DevExpress.XtraGrid.GridControl gridcontrol)
{
SaveFileDialog frm = new SaveFileDialog();
frm.Filter = "(*.xlsx)|*.xlsx";
frm.FileName = "新文件" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (frm.CheckFileExists)
{
if (!(MessageBox.Show("该文件已经存在是否覆盖该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)) return;
}
string fileName = frm.FileName;
XlsxExportOptions options = new XlsxExportOptions();
//options.TextExportMode = TextExportMode.Text;
options.TextExportMode = TextExportMode.Value;
options.RawDataMode = false;
// gridcontrol.ExportToXls(fileName, options);
gridcontrol.ExportToXlsx(fileName, options);
if ((MessageBox.Show("是否打开该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
{
System.Diagnostics.Process.Start(fileName);
}
}
}
public static string exportLayoutControlToPdf(LayoutControl layoutControl, bool open)
{
string fileName = string.Empty;
SaveFileDialog frm = new SaveFileDialog();
frm.Filter = "(*.pdf)|*.pdf";
frm.FileName = "新文件" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (frm.CheckFileExists)
{
if (!(MessageBox.Show("该文件已经存在是否覆盖该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
return fileName;
}
fileName = frm.FileName;
layoutControl.ExportToPdf(fileName);
if (open == true && (MessageBox.Show("是否打开该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
{
System.Diagnostics.Process.Start(fileName);
}
}
return fileName;
}
public static string exportLayoutControlToExistPdf(LayoutControl layoutControl, bool open, string fileName)
{
try
{
layoutControl.ExportToPdf(fileName);
if (open == true && (MessageBox.Show("是否打开该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
{
System.Diagnostics.Process.Start(fileName);
}
}
catch { }
return fileName;
}
public static string exportLayoutControlToXls(LayoutControl layoutControl, bool open)
{
string fileName = string.Empty;
SaveFileDialog frm = new SaveFileDialog();
frm.Filter = "(*.xls)|*.xls";
frm.FileName = "新文件" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (frm.CheckFileExists)
{
if (!(MessageBox.Show("该文件已经存在是否覆盖该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
return fileName;
}
fileName = frm.FileName;
XlsExportOptions options = new XlsExportOptions();
options.TextExportMode = TextExportMode.Text;
//options.TextExportMode = TextExportMode.Value;
options.RawDataMode = false;
layoutControl.ExportToXls(fileName, options);
if (open == true && (MessageBox.Show("是否打开该文件?", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
{
System.Diagnostics.Process.Start(fileName);
}
}
return fileName;
}
}
}