当前位置:  开发笔记 > 编程语言 > 正文

将Datagrid导出为ex​​cel asp

如何解决《将Datagrid导出为ex​​celasp》经验,为你挑选了1个好方法。

什么是将Datagrid导出为ex​​cel的最佳方法?我没有任何经验将datagrid导出到excel,所以我想知道你们如何将datagrid导出到excel.我读到有很多方法,但我想只是为datagrid函数做一个简单的导出excel.我正在使用asp.net C#

干杯..



1> Marc Gravell..:

最简单的方法是简单地将csv或html(特别是a

...
...
)写入输出,并简单地通过content-type标头假装它是excel格式.Excel将很乐意加载; csv更简单......

这是一个类似的例子(它实际上需要一个IEnumerable,但它与任何源类似(例如DataTable,循环遍历行).

        public static void WriteCsv(string[] headers, IEnumerable data, string filename)
        {
            if (data == null) throw new ArgumentNullException("data");
            if (string.IsNullOrEmpty(filename)) filename = "export.csv";

            HttpResponse resp = System.Web.HttpContext.Current.Response;
            resp.Clear();
            // remove this line if you don't want to prompt the user to save the file
            resp.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            // if not saving, try: "application/ms-excel"
            resp.ContentType = "text/csv";
            string csv = GetCsv(headers, data);
            byte[] buffer = resp.ContentEncoding.GetBytes(csv);
            resp.AddHeader("Content-Length", buffer.Length.ToString());
            resp.BinaryWrite(buffer);
            resp.End();
        }
        static void WriteRow(string[] row, StringBuilder destination)
        {
            if (row == null) return;
            int fields = row.Length;
            for (int i = 0; i < fields; i++)
            {
                string field = row[i];
                if (i > 0)
                {
                    destination.Append(',');
                }
                if (string.IsNullOrEmpty(field)) continue; // empty field

                bool quote = false;
                if (field.Contains("\""))
                {
                    // if contains quotes, then needs quoting and escaping
                    quote = true;
                    field = field.Replace("\"", "\"\"");
                }
                else
                {
                    // commas, line-breaks, and leading-trailing space also require quoting
                    if (field.Contains(",") || field.Contains("\n") || field.Contains("\r")
                        || field.StartsWith(" ") || field.EndsWith(" "))
                    {
                        quote = true;
                    }
                }
                if (quote)
                {
                    destination.Append('\"');
                    destination.Append(field);
                    destination.Append('\"');
                }
                else
                {
                    destination.Append(field);
                }

            }
            destination.AppendLine();
        }
        static string GetCsv(string[] headers, IEnumerable data)
        {
            StringBuilder sb = new StringBuilder();
            if (data == null) throw new ArgumentNullException("data");
            WriteRow(headers, sb);
            foreach (string[] row in data)
            {
                WriteRow(row, sb);

            }
            return sb.ToString();
        }

推荐阅读
Gbom2402851125
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有