简体   繁体   English

Microsoft.Office.Interop.Excel - 用分号分隔符打开 csv

[英]Microsoft.Office.Interop.Excel - opening csv with semicolon delimiter

I have read Microsoft.Office.Interop.Excel - *.csv file opening but it does not work for me and I don't know why.我已阅读Microsoft.Office.Interop.Excel - *.csv 文件打开但它对我不起作用,我不知道为什么。

First I tried only using the optional parameters I need like this:首先,我只尝试使用我需要的可选参数,如下所示:

Excel.Application xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(path, Delimiter:";");

It opens the csv but separates at the comma.它打开 csv 但以逗号分隔。

Then I tried like in the referenced thread above and it does not separate at all.然后我在上面引用的线程中尝试过,它根本没有分开。

Excel.Application xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(path,0,false,5, "","",false,Excel.XlPlatform.xlWindows,";",true,false,0,true,false,false);

Here is some sample data I used:这是我使用的一些示例数据:

2,138;2,066;2,022;2,076;0,297;;;0,770;1,898;1,864;1,798;1,859;1,715;20,032;8,599;0,039;;;0,336;Iteration: 5 of 500
0,298;0,295;0,298;0,297;0,297;;;0,302;0,296;0,296;0,297;0,297;0,299;0,300;0,296;0,037;;;0,409;Iteration: 6 of 500
0,297;0,299;0,296;0,297;0,311;;;0,295;0,295;0,295;0,295;0,295;0,295;0,306;0,306;0,039;;;0,372;Iteration: 7 of 500
0,299;0,298;0,295;0,297;0,295;;;0,294;0,307;0,295;0,295;0,296;0,297;0,296;0,295;0,040;;;0,368;Iteration: 8 of 500

Has anyone an idea what im doing wrong?有谁知道我做错了什么?

When I open the csv file with a double click (excel standard program to open csv) it formats it as I want.当我双击打开 csv 文件(用于打开 csv 的 Excel 标准程序)时,它会根据需要对其进行格式化。 With the semicolon as delimiter.以分号作为分隔符。 I probably have set it in the properties once.我可能已经在属性中设置过一次。

Try OpenText :尝试OpenText

Excel.Application ex = new Excel.Application();

Excel.Workbooks wbs = ex.Workbooks;

wbs.OpenText(path,
    DataType:Excel.XlTextParsingType.xlDelimited, 
    Semicolon: true);

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.workbooks.open?view=excel-pia https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.workbooks.open?view=excel-pia

Format Object格式对象

Optional Object.可选对象。 If Microsoft Excel is opening a text file, this argument specifies the delimiter character, as shown in the following table.如果 Microsoft Excel 正在打开文本文件,则此参数指定分隔符,如下表所示。 If this argument is omitted, the current delimiter is used.如果省略此参数,则使用当前分隔符。

... ...

Delimiter Object分隔符对象

Optional Object.可选对象。 If the file is a text file and the Format argument is 6, this argument is a string that specifies the character to be used as the delimiter.如果文件是文本文件并且 Format 参数为 6,则此参数是一个字符串,用于指定要用作分隔符的字符。 For example, use Chr(9) for tabs, use "," for commas, use ";"例如,对制表符使用 Chr(9),对逗号使用“,”,使用“;” for semicolons, or use a custom character.对于分号,或使用自定义字符。 Only the first character of the string is used.仅使用字符串的第一个字符。

So your open statement should be: xlWorkBook = xlApp.Workbooks.Open(path, Format:6, Delimiter:";");所以你的 open 语句应该是: xlWorkBook = xlApp.Workbooks.Open(path, Format:6, Delimiter:";");

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

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