Fine UI 中Grid导出真实Excel文件

此方法并不是写Html代码生成伪Excel文件,添加引用 using Microsoft.Office.Interop.Excel;


 /// <summary>
        /// 导出Grid数据到excel
        /// </summary>
        /// <param name="gridView"></param>
        private void ExportToExcel(Grid gridView)
        {
            if(gridView.Rows.Count == 0)
            {
                Alert.Show("No data");
                return;
            }
            //First fetch all records from grid to dataset
            DataSet dset = new DataSet();
            dset.Tables.Add();
            //First Add Columns from gridview to excel
            for (int i = 0; i < columns.Length; i++) //GridView is id of gridview
            {
                dset.Tables[0].Columns.Add(columns[i]);
            }
            //add rows to the table 
            System.Data.DataRow dr1;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                dr1 = dset.Tables[0].NewRow();
                string PortName = gridView.Rows[i].Values[1].ToString();
                string BitHi = gridView.Rows[i].Values[2].ToString();
                string BitLow = gridView.Rows[i].Values[3].ToString();
                string Direction = gridView.Rows[i].Values[4].ToString();
                string GroupName = gridView.Rows[i].Values[5].ToString();
                string AliasInGroup = gridView.Rows[i].Values[6].ToString();
                dr1[0] = PortName;
                dr1[1] = BitHi;
                dr1[2] = BitLow;
                dr1[3] = Direction;
                dr1[4] = GroupName;
                dr1[5] = AliasInGroup;
                dset.Tables[0].Rows.Add(dr1);
            }
            //below code is export dset to excel
            Application excel = new Application();
            Workbook wBook;
            Worksheet wSheet;
            wBook = excel.Workbooks.Add(System.Reflection.Missing.Value);
            wSheet = (Worksheet)wBook.ActiveSheet;
            System.Data.DataTable dt = dset.Tables[0];
            System.Data.DataColumn dc = new DataColumn();
            int colIndex = 0;
            int rowIndex = 0;
            foreach (DataColumn dcol in dt.Columns)
            {
                colIndex = colIndex + 1;
                excel.Cells[1, colIndex] = dcol.ColumnName;
            }
            foreach (DataRow drow in dt.Rows)
            {
                rowIndex = rowIndex + 1;
                colIndex = 0;
                foreach (DataColumn dcol in dt.Columns)
                {
                    colIndex = colIndex + 1;
                    excel.Cells[rowIndex + 1, colIndex] = drow[dcol.ColumnName];
                }
            }
            wSheet.Columns.AutoFit();
            String strFileName ="d:\\importExcel.xls"; //  File Path Where you want to save excel file.
            Boolean blnFileOpen = false;
            try
            {
                System.IO.FileStream fileTemp = File.OpenWrite(strFileName);
                fileTemp.Close();
            }
            catch
            {
                blnFileOpen = false;
            }
            if (System.IO.File.Exists(strFileName)) //It checks if file exists then it delete that file.
            {
                System.IO.File.Delete(strFileName);
            }
            wBook.SaveAs(strFileName, XlFileFormat.xlExcel12, System.Reflection.Missing.Value, System.Reflection.Missing.Value
                , false, false, XlSaveAsAccessMode.xlShared, XlSaveConflictResolution.xlLocalSessionChanges, false, System.Reflection.Missing.Value,
                System.Reflection.Missing.Value, false);
            Alert.Show("File has been saved in " + strFileName);
        }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值