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

 

 一、导出方法

  /// <summary>
    /// 导出指定格式的Excel或World信息
    /// </summary>
    /// <param name="FileName">导出文件名称</param>
    /// <param name="FilePath">导出文件路径</param>
    /// <param name="WorldContent">导出内容</param>
    /// <param name="type">导出类型,1为Excel、2为World</param>
    public static void ExportFile(String FileName, String FilePath, String WorldContent, Int32 type)
    {

        CreateFiles(HttpContext.Current.Server.MapPath(FilePath), WorldContent);
        HttpResponse res = HttpContext.Current.Response;
        res.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        if (type != 1)
            res.ContentType = "application/vnd.ms-world";
        else
            res.ContentType = "application/vnd.ms-excel";
        res.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
        res.Write(WorldContent);
        res.Redirect(FilePath);
        res.End();
    }
    private static bool CreateFiles(string fileurl, string str)
    {
        try
        {
            File.WriteAllText(fileurl, str);
            return true;
        }
        catch
        {
            return false;
        }
    }


二、导出实例

/// <summary>
    /// 导出详细
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Export_Click(object sender, EventArgs e)
    {
        string fileName = "导出.doc";
        string content = ExportTabDetail();
        ExcelExport.ExportFile(fileName, "rptFiles/" + fileName, content,1);
    }
    /// <summary>
    /// 导出表详细
    /// </summary>
    /// <returns></returns>
    

private string ExportTabDetail()     {         StringBuilder sbAppent = new StringBuilder();         int t_num = 1;         string ids = string.Empty;         //获取表的详细信息         DataSet ds = bllTable.GetT_BaseList(this.txtUser.Value.Trim().Replace("'", "''"), drop_Type.SelectedValue, AnpGridView.CurrentPageIndex, AnpGridView.RecordCount);         if (ds == null || ds.Tables[0].Rows.Count == 0) return null;         sbAppent.Append("<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td align='center'>");         foreach (DataRow rowTitle in ds.Tables[0].Rows)         {             if (int.Parse(rowTitle["TableType"].ToString()) > 2) continue;             sbAppent.Append("<table border='1' bordercolor='#000000' bordercolordark='#FFFFFF' cellpadding='0' cellspacing='0' width='60%'>");             sbAppent.Append("<tr><td colspan='6'>名称:" + rowTitle["TableName"] + "</td></tr>");             sbAppent.Append("<tr>");             sbAppent.Append("<td  colspan='6' >描述:" + rowTitle["TableTitle"] + "</td>");             sbAppent.Append("</tr>");             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>");             string tabID = "'" + rowTitle["TableId"].ToString() + "'";             DataSet datasetField = bll.GetFieldListByTabIds(tabID);             if (datasetField == null || datasetField.Tables[0].Rows.Count == 0)                 continue;             foreach (DataRow rowContent in datasetField.Tables[0].Rows)             {                 string isPrimary = string.Empty;                 if (bool.Parse(rowContent["FieldIsPriKey"].ToString()) == true)                     isPrimary = "是";                 else                     isPrimary = "否";                 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>");             }             sbAppent.Append("</table>");             t_num++;             sbAppent.Append("<table border='0'><tr rowspan='3' cellpadding='0' cellspacing='0'><td>&nbsp;</td></tr></table>");

        }         sbAppent.Append("</td></tr></table>");         return sbAppent.ToString();     }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值