0

Hy everyone i d like to ask some help for the next one .

i want to loadt a txt file into excel in c# with the workbooks.opentext method ! it runs well but in the text data there is some numbers wich contains "," character and thats the main separator of the whole text let me show u

'1200000','29,8','DUPAREC PAPÍRGYŰJTŐ ÉS FELD. KFT','

and the problem is in the second column .

Is there anyone who have any idea how to avoid this problem? my code is the next one

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
int[,] fieldInfo = new int[4, 2] { { 1, 2 }, { 2, 4 }, { 3, 2 }, { 4, 2 } };
object oFieldInfo = fieldInfo;
xlApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
xlApp.Workbooks.OpenText(
    filepath,Excel.XlPlatform.xlWindows,
    1,
    Excel.XlTextParsingType.xlDelimited,
    Excel.XlTextQualifier.xlTextQualifierNone,
    false,
    false,
    false,
    true,
    false,
    false,
    missing,
    oFieldInfo,
    missing,
    missing, 
    missing, 
    missing, 
    missing 
        );

Best regards !

Smith!

4

1 回答 1

1

As your character to qualify a text is ' (single quote) you have to pass Excel.XlTextQualifier.xlTextQualifierSingleQuote as parameter.

See the Excel developer reference for more information.

The code below should make your import work.

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
int[,] fieldInfo = new int[4, 2] { { 1, 2 }, { 2, 4 }, { 3, 2 }, { 4, 2 } };
object oFieldInfo = fieldInfo;
xlApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
xlApp.Workbooks.OpenText(
    filepath,Excel.XlPlatform.xlWindows,
    1,
    Excel.XlTextParsingType.xlDelimited,
    Excel.XlTextQualifier.xlTextQualifierSingleQuote,
    false,
    false,
    false,
    true,
    false,
    false,
    missing,
    oFieldInfo,
    missing,
    missing, 
    missing, 
    missing, 
    missing 
        );

Because you defined the second column to be date time formatted in your fieldInfo array '29,8' will be displayed as 29. August in Excel.

You should replace {...,{2,4},...} with {...,{2,1},...} to let Excel parse the number as a number.

于 2013-09-16T13:19:46.733 回答