博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如果导出Excel2007?

Posted on 2010-09-22 17:05  Frank's Blog  阅读(1213)  评论(0)    收藏  举报
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <typeparam name="T">泛型实体</typeparam>
        /// <param name="response"></param>
        /// <param name="listColumes">要显示的列名</param>
        /// <param name="listProperty">要显示的导出属性名  和实体的属性名有关,顺序由显示的列确定 可以同listColumes</param>
        /// <param name="listModel">实体集合</param>
        public static void ExportExcel<T>(HttpResponse response, string sheetName, IList<string> listColumns, IList<string> listProperty, IList<T> listModel) where T : class, new()
        {
            if (listColumns.Count == 0)
            {
                throw new IndexOutOfRangeException("No Columnes!");
            }
            if (listColumns.Count != listProperty.Count)
            {
                throw new ArgumentException("Columns and properties length are not equal.");
            }
            using (StringWriter writer = new StringWriter())
            {
                writer.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
                writer.WriteLine("<head>");
                writer.WriteLine("<!--[if gte mso 9]>");
                writer.WriteLine("<xml>");
                writer.WriteLine(" <x:ExcelWorkbook>");
                writer.WriteLine("  <x:ExcelWorksheets>");
                writer.WriteLine("   <x:ExcelWorksheet>");
                writer.WriteLine("    <x:Name>" + sheetName + "</x:Name>");
                writer.WriteLine("    <x:WorksheetOptions>");
                writer.WriteLine("      <x:Print>");
                writer.WriteLine("       <x:ValidPrinterInfo />");
                writer.WriteLine("      </x:Print>");
                writer.WriteLine("    </x:WorksheetOptions>");
                writer.WriteLine("   </x:ExcelWorksheet>");
                writer.WriteLine("  </x:ExcelWorksheets>");
                writer.WriteLine("</x:ExcelWorkbook>");
                writer.WriteLine("</xml>");
                writer.WriteLine("<![endif]-->");
                writer.WriteLine("</head>");
                writer.WriteLine("<body>");
                writer.WriteLine("<table>");
                writer.WriteLine("<tr>");
                foreach (string item in listColumns)
                {
                    writer.WriteLine("<td>" + item + "</td>"); //列名
                }
                writer.WriteLine("</tr>");
                //通过反射 显示要显示的列
                BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;//反射标识
                Type objType = typeof(T);
                PropertyInfo[] propInfoArr = objType.GetProperties(bf);
                foreach (T model in listModel)
                {
                    writer.WriteLine("<tr>");
                    foreach (string propName in listProperty)
                    {
                        foreach (PropertyInfo propInfo in propInfoArr)
                        {
                            if (string.Compare(propInfo.Name.ToUpper(), propName.ToUpper()) == 0)
                            {
                                PropertyInfo modelProperty = model.GetType().GetProperty(propName);
                                if (modelProperty != null)
                                {
                                    object objResult = modelProperty.GetValue(model, null);
                                    writer.WriteLine("<td>" + ((objResult == null) ? string.Empty : objResult) + "</td>");
                                }
                                else
                                {
                                    throw new Exception("Property name may be not exists!");
                                }
                            }
                        }
                    }
                    writer.WriteLine("</tr>");
                }
                writer.WriteLine("</table>");
                writer.WriteLine("</body>");
                writer.WriteLine("</html>");
                writer.Close();
                response.Clear();
                response.Buffer = true;
                response.Charset = "UTF-8";
                currentPage.EnableViewState = false;
                response.AddHeader("Content-Disposition", "attachment; filename=" + CreateExcelName() + ".xls");
                response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                response.Write(writer);
                response.End();
            }
        }

 

 从网上找到以上代码可以导出一个伪Excel,但对于"只需展示"来说正合适.

 不过有点问题: 当前默认导出的是Excel2003格式,开发时就会出现"您尝试打开的文件xxx.xls的格式与文件扩展名指定的格式不一致.打开文件前请验证文件没有损坏且来源可信.是否立即打开该文件?"的提示信息.

请问以上代码如何能导出Excel2007格式的文件呢?