• DEV GridControl导入导出Excel文件


    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;
    }
    }
    }

  • 相关阅读:
    (网页)中的简单的遮罩层
    (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
    (网页)jQuery的时间datetime控件在AngularJs中使用实例
    Maven Myeclipse 搭建项目
    MyBatis 环境搭建 (一)
    java 常用方法
    XML 基础
    JS BOM
    js 事件
    js 的使用原则
  • 原文地址:https://www.cnblogs.com/zhf123/p/14606444.html
Copyright © 2020-2023  润新知