导出,打印和保存本地日志文件 - 导出GridControl

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;
using System.Windows.Forms;

namespace CYYFCheckupMgr.OccupationalDisease.UC.UC.Query
{
    /// <summary>
    /// ExportUtil
    /// author CYTD Young
    /// 2016-05-06 15:25:14
    /// music: Kendrick Lamar - u
    /// </summary>
    public static class ExportUtil
    {

        public static void GridControlExortToExcel(GridControl gridControl, string fileName = "", bool fileNameWithTime = false)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Excel文件(.xls)|*.xls";
            fileName = string.IsNullOrWhiteSpace(fileName) ? "导出数据" : fileName;
            fileName = fileNameWithTime ? fileName + DateTime.Now.ToString("yyyyMMddHHmmssffff") : fileName;
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.OverwritePrompt = false; //已存在文件是否覆盖提示
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            //已存在文件是否覆盖提示
            while (System.IO.File.Exists(saveFileDialog1.FileName) &&
                DevExpress.XtraEditors.XtraMessageBox.Show("该文件名已存在,是否覆盖?",
                "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                    return;
            }
            if (saveFileDialog1.FileName != "")
            {
                try
                {
                    System.IO.FileStream fs =
                       (System.IO.FileStream)saveFileDialog1.OpenFile();
                    gridControl.ExportToXls(fs);
                    fs.Close();
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据导出成功!", "提示");
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("正由另一进程使用"))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!文件正由另一个程序占用!", "提示");
                    }
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!数据量过大,请分别统计再导出!", "提示");
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 DevExpress 的 GridControl 控件来导出 Excel 文件,并指定字体。下面是一个示例代码,演示如何设置导出的 Excel 单元格的字体: ```csharp using DevExpress.XtraGrid.Export; using DevExpress.XtraPrinting; using DevExpress.XtraPrintingLinks; // 创建一个 GridControl 实例 GridControl gridControl = new GridControl(); // 在 GridControl 中添加数据 // 创建一个 GridView 实例 GridView gridView = new GridView(gridControl); // 将 GridView 添加到 GridControl gridControl.MainView = gridView; // 创建一个导出器实例 GridExportOptions exportOptions = new GridExportOptions(); exportOptions.ExportType = ExportType.WYSIWYG; exportOptions.ExportMode = ExportMode.SingleFile; exportOptions.SheetName = "Sheet1"; // 设置导出的 Excel 单元格字体 exportOptions.Font.Name = "Arial"; exportOptions.Font.Size = 12; exportOptions.Font.Bold = true; // 导出到 Excel 文件 gridControl.ExportToXlsx("path_to_excel_file.xlsx", exportOptions); ``` 在上面的示例代码中,我们首先创建了一个 GridControl 实例,并添加了数据。然后,我们创建了一个 GridView 实例,并将其设置为 GridControl 的 MainView。接下来,我们创建了一个 GridExportOptions 实例,并设置了导出的选项。在导出选项中,我们使用 Font 属性来指定导出的 Excel 单元格的字体。最后,通过调用 gridControl.ExportToXlsx 方法,将数据导出到指定路径的 Excel 文件中。 请注意,上述示例代码中的 "path_to_excel_file.xlsx" 应替换为您想要保存 Excel 文件的实际路径。另外,您可以根据需要调整字体的其他属性,如颜色、字号、加粗等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值