NET打开含有CSV文本用Excel

NET打开含有CSV文本用Excel

本文介绍了从ASP.NET打开含有CSV文本用Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在转换旧的asp.net Intranet应用程序到.NET 4.5。有一个应该是打开包含CSV数据导入Excel字符串我有麻烦的部分。

无论我如何尝试做到这一点,由此产生的Excel文档中包含的网站,而不是我提供的文本的内容。

在一个额外的说明,我现在有一个web项目的ASPX,而公用事业/业务类位于另一个。我不知道这是否是问题的一部分。

请帮我出这个问题。

CSV示例:

 字符串文本=\\年度\\,\\让\\,\\模式\\,\\长\\\\ n \\1997年\\,\\福特\\,\\E350 \\,\\2.34 \\\\ n \\2000 \\,\\水星\\,\\熟女\\,\\2.38 \\

code样品:

 私有静态无效OpenCsvFile(字符串文本,字符串名称)
{
    VAR的HttpContext = HttpContext.Current;
    httpContext.Response.Clear();
    httpContext.Response.ContentType =应用程序/ vnd.ms-EXCEL;
    httpContext.Response.Charset =;
    httpContext.Response.AppendHeader(内容处置,附件;文件名=+名字+名为.csv);
    httpContext.Response.Write(文本);
    httpContext.Response.Flush();
    httpContext.ApplicationInstance.CompleteRequest();
}

更新:生成的文件包含网站的内容,而不是CSV文本

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// EN
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
  <头ID =头像1>
    <标题>
      我的网站
    < /标题>
    < META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= ISO-8859-1/>
    <链接HREF =CSS / style.css文件类型=文/ CSS称号=网站的rel =stylesheet属性/>
  < /头>
  <身体GT;
    <形式方法=邮报行动=Main.aspxID =form1的NAME =form1的>
      <表格边框=0CELLSPACING =0的cellpadding =0ID =容器>
        < TD类=标志的onclick =window.location的='Main.aspx';>
          < D​​IV CLASS =logoDisplay>
            < IMG SRC =图像/ logo.gifALT =WIDTH =280像素HEIGHT =67px/>
          < / DIV>
        < / TD>
...等


解决方案

答案是重新启动您的计算机。
是的,它的工作,现在没有任何进一步的code的变化。我简直不敢相信我花了这么长时间试图让这个才能正常工作。

感谢您想帮助,蒂姆。

I'm converting an older asp.net intranet application over to .net 4.5. One of the parts I'm having trouble with is supposed to open a string containing CSV data into Excel.

No matter how I attempt to accomplish this, the resulting excel document contains the contents of the website rather than the text I supplied.

On an additional note, I currently have the aspx in a web project, while the utility/business classes reside in another. I don't know if that is part of the problem.

Please help me out with this issue.

CSV sample:

string text = "\"Year\", \"Make\", \"Model\", \"Length\"\n\"1997\", \"Ford\", \"E350\", \"2.34\"\n\"2000\", \"Mercury\", \"Cougar\", \"2.38\""

Code Sample:

private static void OpenCsvFile(string text, string name)
{
    var httpContext = HttpContext.Current;
    httpContext.Response.Clear();
    httpContext.Response.ContentType = "application/vnd.ms-excel";
    httpContext.Response.Charset = "";
    httpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
    httpContext.Response.Write(text);
    httpContext.Response.Flush();
    httpContext.ApplicationInstance.CompleteRequest();
}

Update: The resulting file contains the website contents rather than the CSV text.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head id="Head1">
    <title>
      "My WebSite"
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="css/style.css" type="text/css" title="WebSite" rel="stylesheet" />
  </head>
  <body>
    <form method="post" action="Main.aspx" id="form1" name="form1">
      <table border="0" cellspacing="0" cellpadding="0" id="container">
        <td class="logo" onclick="window.location='Main.aspx';">
          <div class="logoDisplay">
            <img src="images/logo.gif" alt="" width="280px" height="67px" />
          </div>
        </td>
... etc
解决方案

The answer is to reboot your PC.Yes, it's working now without any further code changes. I just can't believe I spent so long trying to get this to work properly.

Thank you for trying to help, Tim.

这篇关于从ASP.NET打开含有CSV文本用Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:53