.NET 导出指定格式的Excel 或World

 一、导出方法

  1. /// <summary>  
  2.   /// 导出指定格式的Excel或World信息  
  3.   /// </summary>  
  4.   /// <param name="FileName">导出文件名称</param>  
  5.   /// <param name="FilePath">导出文件路径</param>  
  6.   /// <param name="WorldContent">导出内容</param>  
  7.   /// <param name="type">导出类型,1为Excel、2为World</param>  
  8.   public static void ExportFile(String FileName, String FilePath, String WorldContent, Int32 type)  
  9.   {  
  10.   
  11.       CreateFiles(HttpContext.Current.Server.MapPath(FilePath), WorldContent);  
  12.       HttpResponse res = HttpContext.Current.Response;  
  13.       res.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");  
  14.       if (type != 1)  
  15.           res.ContentType = "application/vnd.ms-world";  
  16.       else  
  17.           res.ContentType = "application/vnd.ms-excel";  
  18.       res.AppendHeader("Content-Disposition""attachment;filename=" + FileName);  
  19.       res.Write(WorldContent);  
  20.       res.Redirect(FilePath);  
  21.       res.End();  
  22.   }  
  23.   private static bool CreateFiles(string fileurl, string str)  
  24.   {  
  25.       try  
  26.       {  
  27.           File.WriteAllText(fileurl, str);  
  28.           return true;  
  29.       }  
  30.       catch  
  31.       {  
  32.           return false;  
  33.       }  
  34.   }  



二、导出实例

  1. /// <summary>  
  2.     /// 导出详细  
  3.     /// </summary>  
  4.     /// <param name="sender"></param>  
  5.     /// <param name="e"></param>  
  6.     protected void btn_Export_Click(object sender, EventArgs e)  
  7.     {  
  8.         string fileName = "导出.doc";  
  9.         string content = ExportTabDetail();  
  10.         ExcelExport.ExportFile(fileName, "rptFiles/" + fileName, content,1);  
  11.     }  
  12.     /// <summary>  
  13.     /// 导出表详细  
  14.     /// </summary>  
  15.     /// <returns></returns>  
  16.     <p>private string ExportTabDetail()  
  17.     {  
  18.         StringBuilder sbAppent = new StringBuilder();  
  19.         int t_num = 1;  
  20.         string ids = string.Empty;  
  21.         //获取表的详细信息  
  22.         DataSet ds = bllTable.GetT_BaseList(this.txtUser.Value.Trim().Replace("'""''"), drop_Type.SelectedValue, AnpGridView.CurrentPageIndex, AnpGridView.RecordCount);  
  23.         if (ds == null || ds.Tables[0].Rows.Count == 0) return null;  
  24.         sbAppent.Append("<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td align='center'>");  
  25.         foreach (DataRow rowTitle in ds.Tables[0].Rows)  
  26.         {  
  27.             if (int.Parse(rowTitle["TableType"].ToString()) > 2) continue;  
  28.             sbAppent.Append("<table border='1' bordercolor='#000000' bordercolordark='#FFFFFF' cellpadding='0' cellspacing='0' width='60%'>");  
  29.             sbAppent.Append("<tr><td colspan='6'>名称:" + rowTitle["TableName"] + "</td></tr>");  
  30.             sbAppent.Append("<tr>");  
  31.             sbAppent.Append("<td  colspan='6' >描述:" + rowTitle["TableTitle"] + "</td>");  
  32.             sbAppent.Append("</tr>");  
  33.             sbAppent.Append("<tr style='background-color:#BFBFBF;font-family:@仿宋_GB2312;font-size:larger;text-align:left;'><td>数据元名称</td><td>数据源中文名称</td><td>数据类型</td><td>数据长度</td><td>默认值</td><td>是否关键字</td></tr>");  
  34.             string tabID = "'" + rowTitle["TableId"].ToString() + "'";  
  35.             DataSet datasetField = bll.GetFieldListByTabIds(tabID);  
  36.             if (datasetField == null || datasetField.Tables[0].Rows.Count == 0)  
  37.                 continue;  
  38.             foreach (DataRow rowContent in datasetField.Tables[0].Rows)  
  39.             {  
  40.                 string isPrimary = string.Empty;  
  41.                 if (bool.Parse(rowContent["FieldIsPriKey"].ToString()) == true)  
  42.                     isPrimary = "是";  
  43.                 else  
  44.                     isPrimary = "否";  
  45.                 sbAppent.Append("<tr style='text-align:left;'><td style='width: 15%;'>" + rowContent["FieldName"] + "</td><td style='width: 20%;'>" + rowContent["FieldTitle"] + "</td><td style='width: 10%;'>" + rowContent["FieldType"] + "</td><td style='width: 10%;'>" + rowContent["FieldSize"] + "</td><td style='width: 8%;'>" + rowContent["DefaultValue"] + "</td><td style='width: 10%;'>" + isPrimary + "</td></tr>");  
  46.             }  
  47.             sbAppent.Append("</table>");  
  48.             t_num++;  
  49.             sbAppent.Append("<table border='0'><tr rowspan='3' cellpadding='0' cellspacing='0'><td>&nbsp;</td></tr></table>");</p><p>        }  
  50.         sbAppent.Append("</td></tr></table>");  
  51.         return sbAppent.ToString();  
  52.     }</p>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值