从控制台生成/打开 CSV - 文件格式错误错误

发布于 2024-10-04 13:12:11 字数 680 浏览 0 评论 0原文

我正在使用控制台应用程序编写一个逗号分隔的文件,而不是使用 Process 打开该文件。这是一种将查询结果转储到 Excel 中的快速但肮脏的方法。

有一段时间,这工作得很好,但最近我开始收到“您尝试打开'blah.csv'的文件,其格式与文件扩展名指定的格式不同”。

单击“是”后,

Excel 检测到 blah.csv 是 SYLK 文件,但无法加载它。文件有错误或不是 SYLK 文件格式。单击“确定”尝试以其他格式打开该文件。

按“确定”将其打开并正确显示。

我在网络世界中看到了一些解决方案,添加了内容处置标头,但由于我使用进程来打开它,所以我无法应用该修复。

我打开文件的代码:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = filePath;
info.UseShellExecute = true;
Process.Start(info);

如果我在 Notepad++ 中打开文件并显示所有字符,它只会显示为带有 CR LF 行结尾的常规 CSV。

经过一番调查,看起来标题行正在触发错误。如果我只是在标题前写一个空行,错误就会消失。标题如下所示:

heading1,heading2,heading3 CRLF

I am writing out a comma separated file using a console app, and than using Process to open the file. It's a quick and dirty way of dumping results of a query into excel.

for a while this worked fine, but lately i started getting "The file you are trying to open 'blah.csv', is in a different format than specified by the file extension".

and than after clicking "Yes"

Excel has detected that blah.csv is a SYLK file, but cannot load it. Either the file has errors or it is not a SYLK file format. Click OK to try to open the file in a different format.

Pressing OK opens it, and displays correctly.

I see some solutions for this in web world with adding content-disposition header, but since i am using a Process to open it, i can't apply that fix.

my code to open the file:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = filePath;
info.UseShellExecute = true;
Process.Start(info);

if i open the file in Notepad++ and show all chars, it just shows as regular CSV with CR LF line endings.

after some investigation, it looks like the headings line is triggering the error. If i simply write a empty line before the headings, the error goes away. the headings look like this:

heading1,heading2,heading3 CRLF

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

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

发布评论

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

评论(4

兔小萌 2024-10-11 13:12:11

看看这里: http://support.microsoft.com/kb/323626

似乎有ID 作为标题行的前两个字符是问题所在 - 在我看来,Excel 的行为非常奇怪。

Have a look here: http://support.microsoft.com/kb/323626

it seems having ID as the first two chars on the header row is the issue - pretty bizarre behaviour from Excel in my opinion.

離人涙 2024-10-11 13:12:11

CSV 以两个字符 ID 开头。如果用双引号将字符括起来,它应该可以正常工作。

The CSV starts with the two characters ID. If you surround the characters with double quotes it should work fine.

平安喜乐 2024-10-11 13:12:11

(已编辑)
我遇到了类似的问题 - 我遇到了完全相同的错误,但就我而言,这是在频繁使用几个月后突然发生的。
我想知道问题是否真的是“ID”列标题,微软的解决方案没有解释为什么,正如OP所说,“有一段时间这工作正常”,然后变成了一个问题。

我发现在我的场景中,有一些来自上游系统的错误数据,其中在某人的名字中添加了逗号,这使得 .csv 文件无效。这导致了“文件格式错误”,但让我走上了错误的路径,“[文件名]是一个 SYLK 文件,但无法加载它”,

希望有一天这可能对其他人有所帮助。

(EDITED)
I had a similar issue - I was getting the exact same error, but in my case this had suddenly occurred after months of frequent use.
I wondered if the problem really is the "ID" column title, Microsofts solution doesn't explain why, as the OP stated, "for a while this worked fine" and then became a problem.

I found in my scenario there was some erroneous data coming from an upstream system where a comma had been added in someone's first name, and this was invalidating the .csv file. This caused the 'file is in wrong format error' but sent me down the wrong path with the ' [filename] is a SYLK file, but cannot load it'

hope this might help someone else one day.

守望孤独 2024-10-11 13:12:11

SYLK file format is not valid error occurs when the CSV file has first two characters are in the uppercase I and D. It can be resolved by the following steps:
Open the file in Notepad and hit apostrophe (’) key before the 1st character.
Save the file and reopen MS Excel.

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