• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个具有上进心的码农
因为一篇文章中有很多是从很多篇文章中摘取的,请恕我没有一一说明摘取出处,如果没有说明,则该文章默认是摘取,如有侵犯您的权益,请与我联系,将会马上删除。
博客园    首页    新随笔    联系   管理    订阅  订阅

导出EXCEL

 此导出的不是EXCEL。而是一个XML文件。只不过它可以用EXCEL形式打开,并且不会出任何问题。

因为毕竟生成XML文件比生成EXCEL要来的简单很多。并且操作也很方便。 只不过是图标的不同而已,

网上很多的方法都太麻烦并且要这装组件。不太好。 还有很重要的一点就是它可以以任何HTML代码放入其中。

并且能在EXCEL表现出来。下面是代码。

 public  bool strCreateExcelAndReturnFileName(string strData,string sheetName,string FilePath)
    
{
        
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
            sb.Append(
"xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
            sb.Append(
"xmlns=\"http://www.w3.org/TR/REC-html40\">");
            sb.Append("<head>");
            sb.Append(
"<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
            sb.Append(
"<meta name=ProgId content=Excel.Sheet>");
            sb.Append(
"<meta name=Generator content=\"Microsoft Excel 10\">");
            sb.Append(
"<!--[if gte mso 9]><xml>");
            sb.Append(
"<x:ExcelWorkbook>");
            sb.Append(
"<x:ExcelWorksheets>");
            sb.Append(
"<x:ExcelWorksheet>");
            sb.Append(
"<x:Name>" + sheetName + "</x:Name>");
            sb.Append(
"<x:WorksheetOptions>");
            sb.Append(
"<x:Print>");
            sb.Append(
"<x:ValidPrinterInfo/>");
            sb.Append(
"</x:Print>");
            sb.Append(
"</x:WorksheetOptions>");
            sb.Append(
"</x:ExcelWorksheet>");
            sb.Append(
"</x:ExcelWorksheets>");
            sb.Append(
"</x:ExcelWorkbook>");
            sb.Append(
"</xml>");
            sb.Append(
"<![endif]--> ");
            sb.Append(
"</head>");
            sb.Append(
"<body>");
            sb.Append(strData);
            sb.Append(
"</body>");
            sb.Append(
"</html>");
            
if (WriteExcelFile(FilePath, sb.ToString()))
            
{
                
return true;
            }

            
else 
            
{
                
return false;
            }

        }

      

    
public bool WriteExcelFile(string strFileName, string strData)
    
{
        
try
        
{
          
            StreamWriter sw 
= new StreamWriter(strFileName, true, System.Text.Encoding.Default);
            sw.WriteLine(strData);
            sw.Close();
            
return true;
        }

        
catch (Exception e)
        
{
            
string str = e.Message;
            
return false;
        }

    }

测试代码:
 strCreateExcelAndReturnFileName("<div><font color=red>this is test</font></div>","sheet1",HttpContext.Current.Server.MapPath("test.xml"));

posted @ 2008-06-20 17:47  不若相忘于江湖  阅读(314)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3