简体   繁体   English

VB.NET导出到Excel,字符串数据未用引号括起来

[英]VB.NET Export to Excel with string data not enclosed in quotation marks

Is there an Excel text file format (XlFileFormat) to export that will not put quotation marks around strings? 是否有要导出的Excel文本文件格式(XlFileFormat)不会在字符串周围加上引号? I've tried xlTextWindows. 我试过xlTextWindows。 Maybe there is an additional setting I'm missing? 也许我还缺少一个额外的设置?

Most of the time, the datasets I'm exporting are in CSV, but I have a situation where I still want to export with an Excel type (long story) but really it is just a small blob of text. 大多数情况下,我导出的数据集都是CSV格式,但我仍然希望使用Excel类型(长篇故事)导出,但实际上它只是一小块文本。 Otherwise, I'd just export to a text file; 否则,我只是导出到一个文本文件; trying to avoid that for this one case. 试图避免这种情况。

[Edit] In this case, I have an Excel workbook with a single worksheet, with one cell (No header) with the text: [编辑]在这种情况下,我有一个带有单个工作表的Excel工作簿,其中一个单元格(无标题)包含文本:

[Reports] [报告]

When saved, the text file contains: 保存后,文本文件包含:

"[Reports]" “[报告]”

because it is a text field and that is what Excel does when it saves it to a text file. 因为它是一个文本字段,这是Excel将其保存到文本文件时所执行的操作。

For this purpose, it cannot contain any surrounding quotation marks or any other characters. 为此,它不能包含任何周围的引号或任何其他字符。

I will probably end up using a FileStream and StreamWriter, but would like to see if this can be avoided. 我可能最终会使用FileStream和StreamWriter,但是想知道是否可以避免这种情况。

[Additional Edit] [附加编辑]

What the app does: 该应用程序的功能:

It Loops through the rows of a datatable and place the value for each column in an Excel workbook sheet cell. 它循环遍历数据表的行,并将每个列的值放在Excel工作簿表单元格中。

This works great when you want it to truely be an Excel file or a .CSV file. 当您希望它真正成为Excel文件或.CSV文件时,这非常有用。 I have an instance where I need to save a single cell's text to a text file, Excel insists on enclosing the text with quotation marks. 我有一个实例,我需要将单个单元格的文本保存到文本文件,Excel坚持用引号括起文本。 I don't know about anyone else, but when I save something in Notepad, I don't add quotation marks. 我不知道其他人,但是当我在记事本中保存一些东西时,我不添加引号。 The application that wants to read this file, doesn't like the text enclosed in quotation marks either. 想要读取此文件的应用程序也不喜欢用引号括起来的文本。 There is no need to escape quotation marks if they 'happen' to be in the chunk of text, but they should never be added. 如果它们“碰巧”出现在文本块中,则无需转义引号,但不应添加引号。 Nothing should be added. 什么都不应该添加。 It should just be a block of text in the same format it was in the Datatable. 它应该只是一个文本块,其格式与Datatable中的格式相同。

Save the Excel workbook using 使用保存Excel工作簿

ExcelWorkbook.SaveAs ExcelWorkbook.SaveAs

Have you tried Text (Tab delimited): 你试过Text(制表符分隔): 在此输入图像描述

Haven't tried this, but it may help 没有试过这个,但它可能有所帮助

What if you try this in a few ugly steps.. 如果你在一些丑陋的步骤中试试这个怎么办?

  • Save data as CSV - direct to text with Comma separators, no quotes (as you require. Use the CSV extensions) 将数据保存为CSV - 使用逗号分隔符直接显示文本,不带引号(根据需要使用。使用CSV扩展名)
  • Load CSV in Excel via Code 通过代码在Excel中加载CSV
  • Save the file in Excel XLS(S) format via code 通过代码以Excel XLS(S)格式保存文件

Then it might skip adding the quotes to the text in cells in the XLS(S) file it has saved. 然后,它可能会跳过将引号添加到它已保存的XLS(S)文件中的单元格中的文本。

Take a look at SpreadsheetML. 看看SpreadsheetML。 I have a sample here: 我在这里有一个样本:
Generating an Excel file in ASP.NET 在ASP.NET中生成Excel文件

It's xml, so you could use a CDATA section to avoid the need to replace quotes with the " 它是xml,所以你可以使用CDATA部分来避免用“ "代替引号" entity. 实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM