找回密码
 立即注册

QQ登录

只需一步,快速开始

szkg001

金牌服务用户

8

主题

42

帖子

2330

积分

金牌服务用户

积分
2330
szkg001
金牌服务用户   /  发表于:2017-1-5 16:28  /   查看:3438  /  回复:7
您好,DataGrid怎么导出到Excel,请问有相关博客或Demo吗

7 个回复

倒序浏览
yuzhiyin
初级会员   /  发表于:2017-1-5 16:31:44
沙发
用一个插件NPOI导出,比较好,发布之后服务器不需要安装组件
回复 使用道具 举报
yuzhiyin
初级会员   /  发表于:2017-1-5 16:46:14
板凳
protected void btnDaochu_Click(object sender, EventArgs e)
        {
            string filePath = "";
            bool check = false;
            try
            {
                Log.WriteLog("执行数据作成(Excel)处理开始", CommConst.LOG_NORMAL, PageId);
                polybasic Polybasic = new polybasic();
                Polybasic=GetSearchInfo();
                //出力操作
                DataTable dtSource = new DataTable();
                dtSource = depbl.GetmasdictInfo(Polybasic).Tables[0];
            
                if (this.dgMst1.Rows.Count>0)
                {
                    if (dtSource.Rows.Count == 0 || dtSource == null)
                    {
                        ShowMsgBox(Messager.GetMessageInfo2("MSG_POC_01"));
                    }
                    else if (dtSource.Rows.Count > 0)
                    {
                        System.Data.DataTable dt = dtSource;
                        string filename = "NOKSJZC" + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
                        //临时存放路径

                        filePath = Server.MapPath("~/Template/" + filename);
                        //Excel模版文件的路径excel的模板

                        string masterPath = Server.MapPath("~/Template/polybasic1.xlt");
                        //复制Excel模版
                        File.Copy(masterPath, filePath);

                        // 先把文件的属性读取出来
                        FileAttributes attrs = File.GetAttributes(filePath);

                        // 下面表达式中的 1 是 FileAttributes.ReadOnly 的值
                        // 此表达式是把 ReadOnly 所在的位改成 0,
                        attrs = (FileAttributes)((int)attrs & ~(1));

                        FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                        HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
                        ISheet sheet = hssfworkbook.GetSheet("Sheet_polybasic");

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            sheet.CopyRow(0, i + 1);
                        }

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            //从第几行开始进行导出
                            IRow row = sheet.GetRow(i + 1);
                            row.GetCell(0).SetCellValue(i + 1);
                            row.GetCell(1).SetCellValue(dt.Rows[i]["type"].ToString());
                            row.GetCell(2).SetCellValue(dt.Rows[i]["type_name"].ToString());
                            row.GetCell(3).SetCellValue(dt.Rows[i]["code"].ToString());
                            row.GetCell(4).SetCellValue(dt.Rows[i]["code_name"].ToString());
                            row.GetCell(5).SetCellValue(dt.Rows[i]["code_name_cn"].ToString());
                            row.GetCell(6).SetCellValue(dt.Rows[i]["code_name_en"].ToString());
                            row.GetCell(7).SetCellValue(dt.Rows[i]["code_name_jp"].ToString());
                            row.GetCell(8).SetCellValue(dt.Rows[i]["memo"].ToString());
                        }
                        sheet.ForceFormulaRecalculation = true;

                        using (FileStream filess = File.OpenWrite(filePath))
                        {
                            hssfworkbook.Write(filess);
                        }

                        check = true;

                        // 输出副本的二进制字节流
                        HttpContext.Current.Response.Charset = "UTF-8"; // 或UTF-7 以防乱码
                        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                        Response.ContentType = "application/ms-excel";
                        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.GetEncoding(65001).GetBytes(filename)));
                        Response.BinaryWrite(File.ReadAllBytes(filePath));
                    }
                }
                else
                {
                    ShowMsgBox(Messager.GetMessageInfo2("MSG_POC_01"));
                }
                if (Session["polybasicManager" + CommConst.PAGE_DATASOURCE_SESSION] != null)
                {
                    SetGdvListData(1);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("执行数据作成(Excel)处理异常,异常信息:" + ex.Message, CommConst.LOG_ERROR, PageId);
                throw ex;
            }
            finally
            {
                Log.WriteLog("执行数据作成(Excel)处理结束", CommConst.LOG_NORMAL, PageId);
                if (check)
                {
                    //删除副本
                    File.Delete(filePath);
                }
            }
        }
回复 使用道具 举报
szkg001
金牌服务用户   /  发表于:2017-1-5 16:59:00
地板
yuzhiyin 发表于 2017-1-5 16:46
protected void btnDaochu_Click(object sender, EventArgs e)
        {
            string filePath  ...

谢谢你,
C1本身不支持吗?有没有不装插件的方法呢?
回复 使用道具 举报
yuzhiyin
初级会员   /  发表于:2017-1-5 17:03:23
5#
szkg001 发表于 2017-1-5 16:59
谢谢你,
C1本身不支持吗?有没有不装插件的方法呢?

不属于插件,就是几个引用dll,而且这个比较好,发布到服务器上不需要安装office组件,代码也少

评分

参与人数 1满意度 +5 收起 理由
szkg001 + 5 赞一个!

查看全部评分

回复 使用道具 举报
Alice
社区贡献组   /  发表于:2017-1-5 17:22:54
6#
谢谢您的反馈。
在您的工程里添加引用:
using C1.WPF.DataGrid.Excel
然后就可以使用C1DataGrid.Save方法直接保存成xlsx文件。
代码参考:
  1. C1Datagrid1.Save("Test.xlsx", FileFormat.Xlsx);
复制代码


评分

参与人数 1满意度 +5 收起 理由
szkg001 + 5 赞一个!

查看全部评分

请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
szkg001
金牌服务用户   /  发表于:2017-1-6 08:53:32
7#
Alice 发表于 2017-1-5 17:22
谢谢您的反馈。
在您的工程里添加引用:
using C1.WPF.DataGrid.Excel

好的,谢谢您!
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2017-1-6 10:45:20
8#
szkg001 发表于 2017-1-6 08:53
好的,谢谢您!

不用客气。
此问题关闭,如果有新问题欢迎开新帖。
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部