我写了使用一个控制台应用程序逗号分隔的文件,比使用过程中打开该文件。这是倾销查询结果导入Excel的一个快速和肮脏的方式。
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.
一会儿这个工作得很好,但最近我开始您试图打开'blah.csv的文件,是在一个不同的格式通过文件扩展名指定。
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".
和比点击是后
Excel检测到blah.csv是SYLK文件中,但不能加载它。或者该文件有错误或它不是一个SYLK文件格式。单击确定以尝试不同的格式打开文件。
按确定,打开它,并显示正常。
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.
我的代码来打开文件:
ProcessStartInfo info = new ProcessStartInfo(); info.FileName = filePath; info.UseShellExecute = true; Process.Start(info);如果我打开记事本+ +文件,并显示所有字符,它只是显示与CR LF正规CSV行结束。
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:
标题1,heading2,heading3 CRLF
heading1,heading2,heading3 CRLF
推荐答案看看这里: support.microsoft/kb/323626
似乎有ID作为标题行的前两个字符是问题 - 从Excel在我看来,漂亮的怪异行为。
it seems having ID as the first two chars on the header row is the issue - pretty bizarre behaviour from Excel in my opinion.
更多推荐
从控制台生成/ CSV开放
发布评论