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

  • 相关阅读:
    为什么网站不被百度收录或收录清零?
    XmlSerializer序列化一组成员到文本文件
    windows phone不同页面间传值
    windows phone下进行Isolated的IO读写
    windows Phone 后退键历史的清除
    Window Phone ListBox的DataBinding:
    VGA的相关代码
    如何避免Quartus II自動將未宣告的信號視為wire?
    XINLINX约束心得
    VIM配置文件备份
  • 原文地址:https://www.cnblogs.com/zhf123/p/14606444.html
Copyright © 2011-2022 走看看