资源简介

支持将多个DEV GridControl的GridView导出到同一个Excel文件中的Sheet中,每个GridView保存到不同的Sheet中,含源码,真实可用。

资源截图

代码片段和文件信息

using DevExpress.XtraGrid.Views.Grid;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GridControlToExcel
{
    public class GridControlToExcel
    {
        /// 
        /// 可以自定义导出Excel的格式,传的参数为GridView
        /// 

        /// 需要保存的GridView
        /// Sheet名
        /// 文件名
        public static void ExportGridViewToExcel(List gridView List sheetName string filename)
        {
            //System.Data.DataTable dt = (System.Data.DataTable)gridView.DataSource;
            if (gridView.Count == 0)
                return;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = filename;
            sfd.Filter = “Excel files (*xls) | *.xls“;
            sfd.RestoreDirectory = true;

            if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Trim() != null)
            {
                int colIndex = 0;
                System.Reflection.Missing miss = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
                xlapp.Visible = false; //是否显示导出过程 ,建议关闭,否则在导出过程中鼠标点击Excel文件时会出错。
                Microsoft.Office.Interop.Excel.Workbooks mBooks = (Microsoft.Office.Interop.Excel.Workbooks)xlapp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook mBook = (Microsoft.Office.Interop.Excel.Workbook)mBooks.Add(miss);
                Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss miss gridView.Count - 1 Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);

                //设置文字自动换行 
                //mSheet.Cells.WrapText = true;
                try
                {

                    int gridViewIndex = 1;
                    foreach (GridView gd in gridView)
                    {
                        int rowIndex = 1;
                        Worksheet sheet = mBook.Worksheets[gridViewIndex];
                        sheet.Name = sheetName[gridViewIndex - 1];

                        //设置对齐方式
                        sheet.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                        //设置第一行高度,即标题栏
                        ((Microsoft.Office.Interop.Excel.Range)sheet.Rows[“1:1“ System.Type.Missing]).RowHeight = 20;

                        //设置数据行行高度
                        ((Microsoft.Office.Interop.Excel.Range)sheet.Rows[“2:“ + gd.RowCount + 1 System.Type.Missing]).RowHeight = 16;

                        //设置字体大小(10号字体)
                        sheet.Range[sheet.Cells[1 1] sheet.Cells[gd.RowCount + 1 gd.Columns.Count]].Font.Size = 10;

                        //设置单元格边框
  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-03-31 14:08  GridControlToExcel\
     目录           0  2014-03-31 14:10  GridControlToExcel\GridControlToExcel\
     文件        9772  2014-03-31 14:10  GridControlToExcel\GridControlToExcel\GridControlToExcel.cs
     文件        3810  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\GridControlToExcel.csproj
     目录           0  2014-03-31 14:01  GridControlToExcel\GridControlToExcel\Properties\
     文件        1356  2014-03-31 14:01  GridControlToExcel\GridControlToExcel\Properties\AssemblyInfo.cs
     目录           0  2014-03-31 14:10  GridControlToExcel\GridControlToExcel\bin\
     目录           0  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\bin\Debug\
     文件       17408  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\bin\Debug\GridControlToExcel.dll
     文件       15872  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\bin\Debug\GridControlToExcel.pdb
     目录           0  2014-03-31 14:17  GridControlToExcel\GridControlToExcel\bin\Release\
     目录           0  2014-03-31 14:01  GridControlToExcel\GridControlToExcel\obj\
     目录           0  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\
     文件        7182  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         586  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\GridControlToExcel.csproj.FileListAbsolute.txt
     文件         771  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\GridControlToExcel.csproj.ResolveComReference.cache
     文件       17408  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\GridControlToExcel.dll
     文件       15872  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\GridControlToExcel.pdb
     文件      407040  2014-03-31 14:15  GridControlToExcel\GridControlToExcel\obj\Debug\Interop.Microsoft.Office.Core.dll
     目录           0  2014-03-31 14:17  GridControlToExcel\GridControlToExcel\obj\Debug\TempPE\
     文件           0  2014-03-31 14:01  GridControlToExcel\GridControlToExcel\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2014-03-31 14:01  GridControlToExcel\GridControlToExcel\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2014-03-31 14:01  GridControlToExcel\GridControlToExcel\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     文件         944  2014-03-31 14:01  GridControlToExcel\GridControlToExcel.sln
     文件       35840  2014-03-31 14:16  GridControlToExcel\GridControlToExcel.v11.suo
     文件     1048576  2002-09-06 12:52  GridControlToExcel\Microsoft.Office.Interop.Excel.dll

评论

共有 条评论