zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }

  • 相关阅读:
    多线程协作wait、notify、notifyAll方法简介理解使用 多线程中篇(十四)
    深入解析ThreadLocal 详解、实现原理、使用场景方法以及内存泄漏防范 多线程中篇(十七)
    java线程通信与协作小结 多线程中篇(十六)
    sleep、yield、join方法简介与用法 sleep与wait区别 多线程中篇(十五)
    final 关键字与安全发布 多线程中篇(十三)
    java 轻量级同步volatile关键字简介与可见性有序性与synchronized区别 多线程中篇(十二)
    windows系统dokuwiki安装部署设置 xampp环境配置
    synchronized关键字简介 多线程中篇(十一)
    Java内存模型JMM 高并发原子性可见性有序性简介 多线程中篇(十)
    java锁与监视器概念 为什么wait、notify、notifyAll定义在Object中 多线程中篇(九)
  • 原文地址:https://www.cnblogs.com/zhf123/p/14606444.html
Copyright © 2011-2022 走看看