使用 asp.net 创建 csv 并在 Excel 中打开

发布于 2024-09-28 21:41:37 字数 747 浏览 3 评论 0原文

我正在使用 Asp.net 创建一个 csv 文件,用户可以直接在 Excel 中打开该文件。我希望能够显示下载弹出窗口,并让用户选择“使用 Excel 打开”,这将在 Excel 中打开文件。

创建 csv 的代码:

Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.csv", "test"));
Response.ContentType = "text/csv";
Response.Write("Year, Make, Model, Length\n1997, Ford, E350, 2.34\n2000, Mercury, Cougar, 2.38");
Response.End();

Excel 需要了解“年份”、“制造”等都应该是不同的列。这似乎不适用于我正在创建的 csv,所有内容都在同一列中。它显示如下:

http://oi54.tinypic.com/2evyb0k.jpg 使用 Excel 的方法是使用“文本导入向导”。然后所有内容都会像它应该的那样显示在不同的列中。

正如我所说,我需要的是创建一个电子表格(不需要是 csv),用户应该很容易在 Excel 或他们用来开始使用它的任何东西中打开它。你会如何解决这个问题?谢谢!

I'm using Asp.net to create a csv file that the user can open directly in excel. I want to make it possible to show the download popup and for the user to choose "Open with Excel" and that will open the file in excel.

The code to create the csv:

Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.csv", "test"));
Response.ContentType = "text/csv";
Response.Write("Year, Make, Model, Length\n1997, Ford, E350, 2.34\n2000, Mercury, Cougar, 2.38");
Response.End();

Excel needs to understand that "Year", "Make", etc should all be different columns. That doesn't seem to work with the csv I'm creating, everything is just in the same column. It shows up like this:

http://oi54.tinypic.com/2evyb0k.jpg

The only way to get it working with excel is to use the "Text Import Wizard". Then everything shows up in different columns like it should.

As I said what I need is to create a spreadsheet (it doesn't need to be csv), it should just be easy for the user to open in excel or whatever they are using to start working with it. How would you solve this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2024-10-05 21:41:37

不知道为什么它不能那样工作 - 它对我来说是这样,但至少尝试引用数据:

Response.Write("\"Year\", \"Make\", \"Model\", \"Length\"\n\"1997\", \"Ford\", \"E350\", \"2.34\"\n\"2000\", \"Mercury\", \"Cougar\", \"2.38\"");

Excel还可以导入XML,谷歌“<?mso-application progid =“Excel.Sheet”?>”对于格式。或者将 Excel 电子表格另存为 XML 来查看示例。 Excel XML 文件可以命名为“.xls”,它将能够直接打开它们。

最后,您可以使用 .NET 中的 NPOI 以本机格式操作 Excel 电子表格: http://npoi.codeplex.com/

对于 XML,您还可以使用我编写的包装类,它将其封装在一个简单的 C# 对象模型中:http:// /snipt.org/lLok/

这是使用该类的一些伪代码:

XlsWorkbook book = new XlsWorkbook();
XlsWorksheet ws = new XlsWorksheet();
ws.Name = "Sheet Name";

XlsRow row;
XlsCell cell;

foreach (datarow in datasource) {
  row = new XlsRow();   
  foreach (datavalue in datarow) {
    cell = new XlsCell(datavalue.ToString());
    cell.DataType = datavalue is numeric ? DataTypes.Numeric | DataTypes.String;
    row.Cells.Add(cell);
   }
   ws.Rows.Add(row);
}
wkb.Worksheets.Add(ws);
Response.Write(wkb.XmlOutput());

或者只是创建您自己的方法,这是基础知识。

以这种方式创建标头:

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<?xml version=\"1.0\"?>\n");
    sb.Append("<?mso-application progid=\"Excel.Sheet\"?>\n");
    sb.Append(
      "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
    sb.Append("xmlns:o=\"urn:schemas-microsoft-com:office:office\" ");
    sb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
    sb.Append("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
    sb.Append("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");
    sb.Append(
      "<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
    sb.Append("</DocumentProperties>");
    sb.Append(
      "<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
    sb.Append("<ProtectStructure>False</ProtectStructure>\n");
    sb.Append("<ProtectWindows>False</ProtectWindows>\n");
    sb.Append("</ExcelWorkbook>\n");

从那里,通过使用以下基本结构创建节点来添加到输出字符串:

<Worksheet ss:Name="SheetName">
  <Table>
    <Row>
      <Cell>
        <Data ss:Type="DataType">Cell A1 Contents Go Here</Data>
      </Cell>
    </Row>
  </Table>
</Worksheet>

中,每个 元素都会创建一个新专栏。为电子表格的每个行添加新的 DataType 可以是“String”或“Number”。

Not sure why it doesn't work like that - it does for me, but at a minimum try quoting the data:

Response.Write("\"Year\", \"Make\", \"Model\", \"Length\"\n\"1997\", \"Ford\", \"E350\", \"2.34\"\n\"2000\", \"Mercury\", \"Cougar\", \"2.38\"");

Excel can also import XML, google "<?mso-application progid="Excel.Sheet"?>" for the format. Or just save an excel spreadsheet as XML to see an example. Excel XML files can be named ".xls" and it will be able to open them directly.

Finally, you can use NPOI in .NET to manipulate excel spreadsheets in the native format: http://npoi.codeplex.com/

For XML you can also use a wrapper class I wrote that encapsulates this in a simple C# object model: http://snipt.org/lLok/

Here's some pseudocode for using the class:

XlsWorkbook book = new XlsWorkbook();
XlsWorksheet ws = new XlsWorksheet();
ws.Name = "Sheet Name";

XlsRow row;
XlsCell cell;

foreach (datarow in datasource) {
  row = new XlsRow();   
  foreach (datavalue in datarow) {
    cell = new XlsCell(datavalue.ToString());
    cell.DataType = datavalue is numeric ? DataTypes.Numeric | DataTypes.String;
    row.Cells.Add(cell);
   }
   ws.Rows.Add(row);
}
wkb.Worksheets.Add(ws);
Response.Write(wkb.XmlOutput());

Or just create your own method, here are the basics.

Create the header this way:

    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<?xml version=\"1.0\"?>\n");
    sb.Append("<?mso-application progid=\"Excel.Sheet\"?>\n");
    sb.Append(
      "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
    sb.Append("xmlns:o=\"urn:schemas-microsoft-com:office:office\" ");
    sb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
    sb.Append("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
    sb.Append("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");
    sb.Append(
      "<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
    sb.Append("</DocumentProperties>");
    sb.Append(
      "<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
    sb.Append("<ProtectStructure>False</ProtectStructure>\n");
    sb.Append("<ProtectWindows>False</ProtectWindows>\n");
    sb.Append("</ExcelWorkbook>\n");

From there, add to your output string by creating nodes using this basic structure:

<Worksheet ss:Name="SheetName">
  <Table>
    <Row>
      <Cell>
        <Data ss:Type="DataType">Cell A1 Contents Go Here</Data>
      </Cell>
    </Row>
  </Table>
</Worksheet>

Within a <Row>, each <Cell> element creates a new column. Add a new <Row> for each spreadsheet row. The DataType for <Data> can be "String" or "Number".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文