Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将富文本导出为纯文本c#_Excel_Sql Server 2008_Export_Richtext - Fatal编程技术网

Excel 将富文本导出为纯文本c#

Excel 将富文本导出为纯文本c#,excel,sql-server-2008,export,richtext,Excel,Sql Server 2008,Export,Richtext,大家好 我需要一些专家的帮助。我有一个MVC4网络应用程序,它有几个由TinyMCE支持的富文本框字段。到目前为止,该系统运行良好。上周,我的客户通知我,他们想将Microsoft SQL中存储的数据导出到Excel以运行自定义报告 我能够用提供的代码将数据导出到excel。但是,它是以RTF格式而不是纯文本格式导出数据。这会在他们尝试阅读内容时引发问题 由于缺乏知识和/或理解,我无法理解这一点。我确实读过,可以使用正则表达式来实现这一点,但我不知道如何实现这一点。所以我向你求助 public

大家好

我需要一些专家的帮助。我有一个MVC4网络应用程序,它有几个由TinyMCE支持的富文本框字段。到目前为止,该系统运行良好。上周,我的客户通知我,他们想将Microsoft SQL中存储的数据导出到Excel以运行自定义报告

我能够用提供的代码将数据导出到excel。但是,它是以RTF格式而不是纯文本格式导出数据。这会在他们尝试阅读内容时引发问题

由于缺乏知识和/或理解,我无法理解这一点。我确实读过,可以使用正则表达式来实现这一点,但我不知道如何实现这一点。所以我向你求助

public ActionResult ExportReferralData()
    {
        GridView gv = new GridView();
        gv.DataSource = db.Referrals.ToList();
        gv.DataBind();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;    filename=UnderwritingReferrals.xls");
        Response.ContentType = "application/ms-excel";
        Response.AddHeader("Content-Type", "application/vnd.ms-excel");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gv.RenderControl(htw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();

        return RedirectToAction("Index");
    }
我非常感谢您的帮助。提前谢谢你

我在YouTube和网络论坛上寻找解决方案,但没有成功。 问候
Francois Muller

您可以执行的一个选项是对写入XML文件的数据进行处理

例如,在字符串中输入idenfity,并将其替换为string.Empty

同样

也可以替换为string.Empty


一旦确定了富文本HTML标记的所有变体,就可以创建一个标记列表,并在for循环中用合适的字符串替换每个标记。

是否尝试将文件保存为.xslx并发送到客户端


较新的Excel格式可能会更优雅地处理数据?

将此函数添加到代码中,然后可以调用以HTML字符串形式传递它的函数。返回的输出将是无HTML的

警告:这不适用于所有情况,不应用于处理不受信任的用户输入。请使用输入字符串的变体进行测试

 public static string StripTagsCharArray(string source)
    {
        char[] array = new char[source.Length];
        int arrayIndex = 0;
        bool inside = false;
        for (int i = 0; i < source.Length; i++)
        {
            char let = source[i];
            if (let == '<')
            { inside = true; continue; }
            if (let == '>') { inside = false; continue; }
            if (!inside) { array[arrayIndex] = let; arrayIndex++; }
        }
        return new string(array, 0, arrayIndex);
    }
公共静态字符串stripTagScharray(字符串源)
{
char[]数组=新字符[source.Length];
int-arrayIndex=0;
bool-inside=false;
for(int i=0;i
因此我通过如下更改原始代码来解决此问题: 因为我只想转换一些列,所以我发现它工作得很好。这将确保每个记录在Excel中按行分隔,并将Html转换为纯文本,允许用户在Excel中添加列过滤器

我希望这能帮助其他有类似问题的人

 GridView gv = new GridView();
        var From = RExportFrom;
        var To = RExportTo;
            if (RExportFrom == null || RExportTo == null)
            {
                /* The actual code to be used */
                gv.DataSource = db.Referrals.OrderBy(m =>m.Date_Logged).ToList();
            }
            else
            {
                gv.DataSource = db.Referrals.Where(m => m.Date_Logged >= From && m.Date_Logged <= To).OrderBy(m => m.Date_Logged).ToList();
            }

            gv.DataBind();

        foreach (GridViewRow row in gv.Rows)
        {
            if (row.Cells[20].Text.Contains("&lt;"))
            {
                row.Cells[20].Text = Regex.Replace(row.Cells[20].Text, "&lt;(?<tag>.+?)(&gt;|>)", " ");
            }

            if (row.Cells[21].Text.Contains("&lt;"))
            {
                row.Cells[21].Text = Regex.Replace(row.Cells[21].Text, "&lt;(?<tag>.+?)(&gt;|>)", " ");
            }

            if (row.Cells[22].Text.Contains("&lt;"))
            {
                row.Cells[22].Text = Regex.Replace(row.Cells[22].Text, "&lt;(?<tag>.+?)(&gt;|>)", " ");
            }

            if (row.Cells[37].Text.Contains("&lt;"))
            {
                row.Cells[37].Text = Regex.Replace(row.Cells[37].Text, "&lt;(?<tag>.+?)(&gt;|>)", " ");
            }

            if (row.Cells[50].Text.Contains("&lt;"))
            {
                row.Cells[50].Text = Regex.Replace(row.Cells[37].Text, "&lt;(?<tag>.+?)(&gt;|>)", " ");
            }

        }
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=Referrals " + DateTime.Now.ToString("dd/MM/yyyy") + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("Content-Type", "application/vnd.ms-excel");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gv.RenderControl(htw);
        //This code will export the data to Excel and remove all HTML Tags to pass everything into Plain text.
        //I am using HttpUtility.HtmlDecode twice as the first instance changes null values to "Â" the second time it will run the replace code.
        //I am using Regex.Replace to change the headings to more understandable headings rather than the headings produced by the Model.
        Response.Write(HttpUtility.HtmlDecode(sw.ToString())
            .Replace("Cover_Details", "Referral Detail")
            .Replace("Id", "Identity Number")
            .Replace("Unique_Ref", "Reference Number")
            .Replace("Date_Logged", "Date Logged")
            .Replace("Logged_By", "File Number")
            .Replace("Date_Referral", "Date of Referral")
            .Replace("Referred_By", "Name of Referrer")
            .Replace("UWRules", "Underwriting Rules")
            .Replace("Referred_To", "Name of Referrer")
            );
        Response.Flush();
        Response.End();
        TempData["success"] = "Data successfully exported!";
        return RedirectToAction("Index");
    }
GridView gv=new GridView();
var-From=RExportFrom;
var To=RExportTo;
if(RExportFrom==null | | RExportTo==null)
{
/*要使用的实际代码*/
gv.DataSource=db.referels.OrderBy(m=>m.Date_Logged).ToList();
}
其他的
{
gv.DataSource=db.references.Where(m=>m.Date\u Logged>=From&&m.Date\u Logged m.Date\u Logged)。ToList();
}
gv.DataBind();
foreach(gv.Rows中的GridViewRow行)
{
if(row.Cells[20].Text.Contains(“”)
{
row.Cells[20]。Text=Regex.Replace(row.Cells[20]。Text,“(?。+)(|>)”,”;
}
if(row.Cells[21].Text.Contains(“”)
{
row.Cells[21]。Text=Regex.Replace(row.Cells[21]。Text,“(?。+)(|>)”,”;
}
if(row.Cells[22].Text.Contains(“”)
{
row.Cells[22]。Text=Regex.Replace(row.Cells[22]。Text,“(?。+)(|>)”,”;
}
if(row.Cells[37].Text.Contains(“”)
{
row.Cells[37]。Text=Regex.Replace(row.Cells[37]。Text,“(?。+)(|>)”,”;
}
if(row.Cells[50].Text.Contains(“”)
{
row.Cells[50]。Text=Regex.Replace(row.Cells[37]。Text,“(?。+)(|>)”,”;
}
}
Response.ClearContent();
Response.Buffer=true;
Response.AddHeader(“内容处置”、“附件;文件名=引用”+DateTime.Now.ToString(“dd/MM/yyyy”)+“.xls”);
Response.ContentType=“应用程序/ms excel”;
Response.ContentEncoding=System.Text.Encoding.UTF8;
AddHeader(“内容类型”、“应用程序/vnd.ms excel”);
响应。Charset=“”;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
gv.渲染控制(htw);
//此代码将数据导出到Excel并删除所有HTML标记,以将所有内容传递到纯文本中。
//我正在使用HttpUtility.HtmlDecode两次,因为第一个实例在第二次运行替换代码时将null值更改为“掴”。
//我使用Regex.Replace将标题更改为更易于理解的标题,而不是模型生成的标题。
Write(HttpUtility.HtmlDecode(sw.ToString())
.替换(“封面详情”、“转介详情”)
.替换(“Id”、“标识号”)
.替换(“唯一参考号”、“参考号”)
.替换(“记录日期”、“记录日期”)
.替换(“记录者”、“文件号”)
.替换(“转介日期”、“转介日期”)
.替换(“转介人”、“转介人姓名”)
.替换(“保险规则”、“承保规则”)
.替换(“推荐人”、“推荐人姓名”)
);
Response.Flush();
Response.End();
TempData[“success”]=“数据已成功导出!”;
返回操作(“索引”);
}

不确定这是否有用,但在桌面应用程序中,您可以在代码中创建richtext控件(不必可见)。将richtext加载到控件中,然后从控件中取出计划文本