VB.NET 导出到 Excel,字符串数据不包含在引号中
是否有可导出的 Excel 文本文件格式 (XlFileFormat) 不会在字符串两边加上引号?我试过xlTextWindows。也许我还缺少一个额外的设置?
大多数时候,我导出的数据集都是 CSV 格式,但我仍然想使用 Excel 类型导出(说来话长),但实际上它只是一小块文本。否则,我只会导出到文本文件;在这一个案例中试图避免这种情况。
[编辑] 在本例中,我有一个 Excel 工作簿,其中只有一个工作表,其中一个单元格(无标题)包含以下文本:
[报告]
保存后,文本文件包含:
“[报告]”
因为它是一个文本字段,这就是 Excel 将其保存到文本文件时所做的事情。
为此,它不能包含任何周围的引号或任何其他字符。
我可能最终会使用 FileStream 和 StreamWriter,但想看看是否可以避免这种情况。
[其他编辑]
该应用程序的功能:
循环遍历数据表的行,并将每列的值放入 Excel 工作簿工作表单元格中。
当您希望它真正成为 Excel 文件或 .CSV 文件时,这非常有用。我有一个实例,我需要将单个单元格的文本保存到文本文件中,Excel 坚持用引号将文本引起来。我不知道其他人如何,但是当我在记事本中保存某些内容时,我不添加引号。想要读取此文件的应用程序也不喜欢用引号括起来的文本。如果引号“碰巧”出现在文本块中,则无需转义引号,但绝对不应该添加它们。不应该添加任何东西。它应该只是一个与数据表中格式相同的文本块。
使用保存 Excel 工作簿
ExcelWorkbook.SaveAs
Is there an Excel text file format (XlFileFormat) to export that will not put quotation marks around strings? I've tried 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. 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:
[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.
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.
[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.
This works great when you want it to truely be an Excel file or a .CSV file. 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. 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.
Save the Excel workbook using
ExcelWorkbook.SaveAs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过文本(制表符分隔):
Have you tried Text (Tab delimited):
看一下 SpreadsheetML。我这里有一个样本:
在 ASP.NET 中生成 Excel 文件
它是 xml ,因此您可以使用 CDATA 部分来避免用
"
实体替换引号。Take a look at SpreadsheetML. I have a sample here:
Generating an Excel file in ASP.NET
It's xml, so you could use a CDATA section to avoid the need to replace quotes with the
"
entity.还没有尝试过这个,但它可能会有所帮助
如果您在几个丑陋的步骤中尝试这个会怎么样..
(根据您的需要。使用 CSV 扩展)
然后,它可能会跳过将引号添加到已保存的 XLS(S) 文件的单元格中的文本。
Haven't tried this, but it may help
What if you try this in a few ugly steps..
(as you require. Use the CSV extensions)
Then it might skip adding the quotes to the text in cells in the XLS(S) file it has saved.