栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#导出生成excel文件的方法小结(xml,html方式)

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#导出生成excel文件的方法小结(xml,html方式)

直接贴上代码,里面都有注释
复制代码 代码如下:
///

        /// xml格式生成excel文件并存盘;
        ///


        /// 生成报表的页面,没有传null
        /// 数据表
        /// 报表标题,sheet1名
        /// 存盘文件名,全路径
        /// 生成文件后是否提示下载,只有web下才有效
        public static void CreateExcelByXml(System.Web.UI.Page page, DataTable dt, String TableTitle, string fileName, bool IsDown)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append("             strb.Append("xmlns:x="urn:schemas-microsoft-com:office:excel"");
            strb.Append("xmlns="");
            strb.Append(" ");
            strb.Append(" ");
            strb.Append("body");
            strb.Append(" {mso-style-parent:style0;");
            strb.Append(" font-family:"Times New Roman", serif;");
            strb.Append(" mso-font-charset:0;");
            strb.Append(" mso-number-format:"@";}");
            strb.Append("table");
            //strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;font-size:12px;color:#222; margin:0px;}");
            strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;color:#222; margin:0px;}");
            strb.Append("thead tr td");
            strb.Append(" {background-color:#e3e6ea;color:#6e6e6e;text-align:center;font-size:14px;}");
            strb.Append("tbody tr td");
            strb.Append(" {font-size:12px;color:#666;}");
            strb.Append(" ");
            strb.Append(" ");
            strb.Append(" ");
            strb.Append(" ");
            strb.Append(" ");
            //设置工作表 sheet1的名称
            strb.Append(" " + TableTitle + " ");
            strb.Append(" ");
            strb.Append(" 285 ");
            strb.Append(" ");
            strb.Append(" ");
            strb.Append(" ");
            strb.Append(" 3 ");
            strb.Append(" 1 ");
            strb.Append("
");
            strb.Append("
");
            strb.Append(" False ");
            strb.Append(" False ");
            strb.Append(" False ");
            strb.Append("
");
            strb.Append("
");
            strb.Append(" 6750 ");
            strb.Append(" 10620 ");
            strb.Append(" 480 ");
            strb.Append(" 75 ");
            strb.Append(" False ");
            strb.Append(" False ");
            strb.Append("
");
            strb.Append("
");
            strb.Append("");
            strb.Append(" ");
            strb.Append(" ");
            //合格所有列并显示标题
            strb.Append(" ");
            strb.Append(" ");
            strb.Append(" ");
            if (dt != null)
            {
                //写列标题
                int columncount = dt.Columns.Count;
                for (int columi = 0; columi < columncount; columi++)
                {
                    strb.Append(" ");
                }
                strb.Append(" ");
                //写数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    strb.Append(" ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        strb.Append(" ");
                    }
                    strb.Append(" ");
                }
            }
            strb.Append("
");
            strb.Append(TableTitle);
            strb.Append("
" + dt.Columns[columi] + "
" + dt.Rows[i][j].ToString() + "
");
            strb.Append(" ");


            string ExcelFileName = fileName;
            //string ExcelFileName = Path.Combine(page.Request.PhysicalApplicationPath, path+"/guestData.xls");
            //报表文件存在则先删除
            if (File.Exists(ExcelFileName))
            {
                File.Delete(ExcelFileName);
            }
            StreamWriter writer = new StreamWriter(ExcelFileName, false);
            writer.WriteLine(strb.ToString());
            writer.Close();
            //如果需下载则提示下载对话框
            if (IsDown)
            {
                DownloadExcelFile(page, ExcelFileName);
            }
        }
---------
///


        /// web下提示下载
        ///

        ///
        /// 文件名,全路径
        public static void DownloadExcelFile(System.Web.UI.Page page, string FileName)
        {
            page.Response.Write("path:" + FileName);
            if (!System.IO.File.Exists(FileName))
            {
                MessageBox.ShowAndRedirect(page, "文件不存在!", FileName);
            }
            else
            {
                FileInfo f = new FileInfo(FileName);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + f.Name);
                HttpContext.Current.Response.AddHeader("Content-Length", f.Length.ToString());
                HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.WriteFile(f.FullName);
                HttpContext.Current.Response.End();
            }

        }

需要cs类文件的可以去下载  点击下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/127018.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号